Summary of parameter passing in C++ C++ has three modes of parameter passing. (1) pass by value sample prototype: void foo(int x); // x is passed by value if I have declared: int a; sample function calls: foo(a); foo(a + 1); foo(5); (2) C style pass by reference (using pointers; sometimes called "simulated pass by reference" sample prototype: void foo (int *x); // x is C-style pass by ref if I have declared int a; sample function call: foo(&a); BUT: foo (&(a+1);) // ILLEGAL foo (&(5)); // ILLEGAL That is, if the actual parameter is a int literal, or an int expression, I can only pass it by value. (3) C++ style pass by reference (sometimes called "true pass by reference"). function prototype: void foo(int &x); for int a; sample function call: foo (a); // x becomes an alias for a. In addition there are two extra things to know: (4) When you pass the name of an array to a function, you are passing a pointer to the 0th element. This is equivalent to C-style pass-by-reference. For example: void sort(int *a, int count); ... int a[10]; ... sort(a,10); // NEVER sort (a[], 10); Equivalent: void sort(int a[], int count); but you STILL do: sort (a, 10); // NEVER sort(a[], 10); (5) If you pass a string literal to a function, it should be passed as a const char * const For example: void printVariable(const char * const name, int value); void printVariable(const char * const name, int value) { cout << name << "=" << value; } ... int a=3; int b=7; printVariable("a",a); printVariable("b",b); It is passed by reference, but because we never want a string literal to be capable of being modified, we put the "const" qualifiers on it. That prevents us from changing where the variable points, and it prevents us from changing the thing pointed to. By the way ========== void foo(const char * const s) // ===== ..... Which one (the ===== const or the ..... const) prevents you from modifying where s points? Which one (the ===== const or the ..... const) prevents you from modifying what s points to? This page provides a summary of all four parameter passing modes. ================================================================ I. the "calling function" vs. the "called function" First, you must distinguish between the calling function and the called function. For example: ***************** void foo(int x); int bar(int y); int main(void) { int a = 2; foo (a); return 0; } void foo(int x) { cout << bar(x) << endl; } int bar(int a) { return a * 3; } *************** For the function call: foo(a); main is the "calling function", and foo is the "called function". For the function call: bar(x); foo is the "calling function", and bar is the "called function" II. Pass by value. In pass by value, the formal parameter is a local variable in the called function that gets initialized with a COPY OF the actual parameter from the calling function. For example, in the program segment: ***************** void fum(int x); int main(void) { int a = 2; fum (a); cout << "a = " << a << endl; return 0; } void fum(int x) { x = -x; cout << "x=" << x << endl; } *************** Here, the formal parameter x of function fum is passed by value. The value of the actual parameter a in main function is 2. When fum is called, the formal parameter x is initialized with the value 2. It is then changed to -2 by the line: x = -x; However, the value of a in the main program is unaffected, because with pass by value, the actual parameter just "passes its value" to the formal parameter, and after that, the two parameters go their separate ways. The analogy I use is that it is like a relay race, where the baton is passed from runner to another. The baton is like the value of the actual parameter. So: the output of the above program is as follows (note spacing). x=-2 a = 2 Note that it DOESN'T CHANGE ANYTHING if the formal and actual parameters happen to have the same name (just like two runners in a relay race can both be named Chris and they are still independent separate people.) III. C-style pass by reference. ***************** void fiddle(int *x); int main(void) { int a = 2; fiddle (&a); cout << "a = " << a << endl; return 0; } void fiddle(int *x) { (*x) = -(*x); cout << "x=" << hex << x << endl; cout << "(*x)=" << (*x) << endl; } *************** My output will be something like (the first line is aprox; the second two are exact): x=0xFFCA3010 (*x)=-2 a = -2 ************ III. C++-style pass by reference. ***************** void foodle(int &x); int main(void) { int a = 2; fiddle (a); cout << "a = " << a << endl; return 0; } void foodle(int &x) // x becomes an alias for the actual param { x = -x; cout << "x=" << x << endl; } *************** My output will be something like (the first line is aprox; the second two are exact): x=-2 a = -2 ************