Thread: Function arguments

  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,210
    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,336
    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
    28,413
    Perhaps more accurately, parameter names are not significant to the function signature, though of course you should still use meaningful names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 03: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