Function Arguments...?

This is a discussion on Function Arguments...? within the C++ Programming forums, part of the General Programming Boards category; Could someone please explain to me what the following text in the bold does: e.g. exampleFunction (int square, int circle) ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    12

    Function Arguments...?

    Could someone please explain to me what the following text in the bold does:

    e.g.

    exampleFunction(int square, int circle)

    Why wouldnt you just use this:

    exampleFunction()
    {
    int square;
    int circle;
    }

  2. #2
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    those are the parameters. they are used to pass values to the function

    example:
    Code:
    int squared(int x)
    {
       return x*x;
    }
    now you can call it like:
    squared(2)
    and it will return 4.
    if you could send it the value (2), it wouldn't be able to return the result (4) because it would have nothing to work with.

    they're like local variables (local to the function), only when you call the function you can give them a value.
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    In addition to what MadHatter, have said:

    Function's parameters is the functions way to access variables that are declared outside the function itself...

    you can also use global variables, or data members if you are making a class or struct.
    none...

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Thanks guys, that helped alot

    EDIT: MadHatter: Does that mean x becomes 2?

    So basically this is to mak the function more flexible? I get ya
    Last edited by Eminence; 12-30-2002 at 08:31 PM.

  5. #5
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    yes, in this case x becomes 2. it's sort of like this...

    Code:
    //function
    int func(int x)
    {
       return x*x;
    }
    
    //function call
    int someVar = func(7);
    
    /*This would be the same as*/
    
    //function
    int func()
    {
       return 7*7;
    }
    
    //function call
    int someVar = func();
    so using parameters makes it so you can use any value.
    hopefully that helped a little, rather than confusing you more...
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by ammar
    you can also use global variables
    I can't believe you're telling a newbie that he can use global variables. Why don't you just tell him to use void main?

    Eminence, don't use global variables. It's bad for a number of reasons.
    Code:
    // Bad
    int num;
    int square(int x);
    int main()
    {
    	cout << num << " squared is " << square(num);
    }
    
    // Good
    int square(int x);
    int main()
    {
    	int num;	// notice that all of the variables are declared within functions, in this case main
    	cout << num << " squared is " << square(num);
    }
    This may be a simple example, but when you get to coding examples it will be very difficult to debug a program that uses global variables. The only global identifiers you should use are constants. Also, global variables are diametrically opposed to the concept of data encapsulation. In short, don't use global variables.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

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