Function arguments

This is a discussion on Function arguments within the C++ Programming forums, part of the General Programming Boards category; I'm just wondering how exactly function arguments work. I know that the syntax for declaring a function is Code: returntype ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40

    Question Function arguments

    I'm just wondering how exactly function arguments work. I know that the syntax for declaring a function is
    Code:
    returntype functionname ( variabletype variablename, etc);
    and to use it is
    Code:
    functionname (argumenttype argument, etc);
    but I would like to know, when you declare a function void playgame (int numberofenemies, int enemylevel); and then use it playgame (2, 3); does that make numberofenemies = 2and enemylevel = 3? And slightly more importantly, can I use that in a similar way to have the user choose x = bla1 and y = bla2 and then use the function playgame (y, x) and then have that make numberofenemies = y and enemylevel = x?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If I understand the question, yes, although to use a function (ie. call a function) you don't specify its argument type in the call.

    If might be good if you could give us a code example demonstrating what you want to do.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And if I understand your second question correctly, then yes: you can use variables in a function call. The function will see the value of those variables (but won't be able to change the variables themselves).

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Someone asked something just like this a day or two ago.
    Variable names are meaningless. These two functions are EXACTLY the same:
    Code:
    int Subtract( int a, int b )
    {
        return (a - b);
    }
    
    int Subtract( int x, int y )
    {
        return (x - y);
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Perhaps more accurately, parameter names are not significant to the function signature, though of course you should still use meaningful names.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  4. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 03:26 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21