parameters

1
2
3
4
5
6
7
8
9
10
11
12
int add(int x, int y)
{
    return x + y;
}

int main()
{
    using namespace std;
    // It is the caller of add() that decides the exact values of x and y
    cout << add(4, 5) << endl; // x=4 and y=5 are the parameters
    return 0;
}


How does the compiler know to take x and y to be 4, 5 since they are both part or separate functions.

The out-put from this is 9.

it works that out by (x+y) this is all writen in one function; int add (x+y) {}

in int main() {} x and y get the values 4 and 5. How does the compiler know to do this, where is the instruction?

Last edited on
Sorry, I do not quite understand the question... can you please express it in a more verbose way?
due to the function invoking statement
add(4, 5)
tells the function add what x & y equal
int add(int x, int y)

To change the values sent, you change the calling statement
Example:
add(500, 444)
would return 944.

Take a look at http://www.cplusplus.com/doc/tutorial/functions/

You may also want to look at http://www.cplusplus.com/doc/tutorial/program_structure/ for some help with program structure.
Last edited on
ah thank you. You will have to bare with me, I know I am picking this up rather slowly, it is my first day.
Topic archived. No new replies allowed.