### Reference variables as parameters: pass-by-reference

• Reference variables as parameters (of a function)

• Fact:

• A reference variable p can be assigned with an address of some variable

• Therefore:

 A reference variable p that is a parameter of some function, can also be initialized (= assigned) with an address of some variable !!!

• Example: a function with an "double *" reference variable as a parameter:

 ``` void func( double *p ) { printf( " p = %u\n, p ); // Print the parameter p printf( "*p = %lf\n, *p ); // Print the variable pointed to by p } ```

Explanation:

• The parameter variable p is defined as "double *p"

 That means that *p is an double typed variable (That's why we print *p using unsigned %lf as a unsigned double) Which means that p can store an address of an int typed variable (That's why we print p using unsigned %u as an unsigned integer)

• Within the function body, we print:

 p (this is the value in the variable p - as we know, it must be an address of an double typed variable) *p (this is the value in the double typed variable that is pointed to by p).

• Passing the address of a variable to a reference parameter variable:

• Because the parameter variable p has the type "double *", we must pass (= initialize) it with an address of an double typed variable

Example:

 ``` int main(int argc, char *argv[]) { double x1 = 4, x2 = 88; printf("Value of &x1 = %u\n", &x1 ); printf("Value of x1 = %lf\n", x1 ); func( &x1 ); // Pass address of x1 to p putchar('\n'); printf("Value of &x2 = %u\n", &x2 ); printf("Value of x2 = %d\n", x2 ); func( &x2 ); // Pass address of x2 to p } ```

Output of this program:

 ``` Value of &x1 = 4290769472 Value of x1 = 4.000000 p = 4290769472 *p = 4.000000 Value of &x2 = 4290769464 Value of x2 = 88.000000 p = 4290769464 *p = 88.000000 ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   gcc ref-param1.c To run:          ./a.out

• Explanation:

• The program execution will first create (allocate) the local variables:

• When program execution reaches the function call, it first creates the parameter variable p and then passes (= copies) the address of i1 (= 5004) to the parameter variable p:

• Now you can clearly see that:

• p contains the value 5004

(Note: the actual value may be different in the C program because the reserved memory location is different - but the explanation is the same)

• *p is an alias for the variable a --- which contains the value 4:

• Alias: a different name for the same variable

• Jargon: Alias

 Alias = a different name to refer to the same variable (memory location)

• You should have seen this phenomenon in Java before...

Example:

 ``` BankAccount x = new BankAccount(); BankAccount y; y = x; // y is now an alias for x ```

• You have just seen how to make an alias in C:

 ``` double x1; double *p; p = &x1; // *p is now an alias for x1 ```

• Emulating the "Pass-by-reference" mechanism using reference parameter variables

• Recall that parameter passing mechanism used in C is:

 Pass-by-value

• In this webpage ( click here), we have used the following example to verify this fact:

 ``` void update( double a ) // a is passed by value { printf( "Inside update - before changing a, a = %lf\n", a); a = a + 1; // Cannot update a in main !!! printf( "Inside update - AFTER changing a, a = %lf\n", a); } int main( int argc, char* argv[] ) { double a = 3.14; printf( "Inside main - before calling update(a), a = %lf\n", a); update ( a ); // a is passed by value !!! printf( "Inside main - AFTER calling update(a), a = %lf\n", a); } ```

Result:

 ``` Inside main - before calling update(a), a = 3.140000 Inside update - before changing a, a = 3.140000 Inside update - AFTER changing a, a = 4.140000 Inside main - AFTER calling update(a), a = 3.140000 Unchanged !!! ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   gcc pass-by-value1.c To run:          ./a.out

• Important facts about the C programming language:

 The C programmming language does not provide the pass-by-reference parameter passing mechanism However, we can use reference parameter variables to emulate (mimic) the effect of the pass-by-reference mechanism.

• How to pass a parameter by reference in C:

1. Use a reference parameter variable in the function definition

Also:

 Make sure that you apply the dereference operator * on the reference parameter variable inside the function body. The dereference operator * will allow you to access the original copy (because *p is an alias of the original variable !)

2. In the function invocation, pass the reference of the actual parameter to the parameter variable.

• Example: pass a variable by-reference using reference parametervariables:

 ``` void update( double *a ) // 1. Use a reference parameter variable { // Important: // a = address of the actual parameter variable // *a = an alias of the actual parameter variable !!! printf( "Inside update - before changing *a, *a = %lf\n", *a); *a = *a + 1; // We CAN update a in main !!! printf( "Inside update - AFTER changing *a, *a = %lf\n", *a); } int main( int argc, char* argv[] ) { double a = 3.14; printf( "Inside main - before calling update(a), a = %lf\n", a); update ( &a ); // pass the reference to a !!! printf( "Inside main - AFTER calling update(a), a = %lf\n", a); } ```

Result:

 ``` Inside main - before calling update(a), a = 3.140000 Inside update - before changing *a, *a = 3.140000 Inside update - AFTER changing *a, *a = 4.140000 Inside main - AFTER calling update(a), a = 4.140000 (Changed !!!) ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   gcc pass-by-ref1.c To run:          ./a.out

• Explanation:

• The program execution will first create (allocate) the local variable a:

• When program execution reaches the function call, it first creates the parameter variable a (notice that it has the same name, but I have colored the parameter variable green to show the difference) and then passes (= copies) the address of actual parameter a (= 5000) to the parameter variable a:

• Now you can clearly see that:

• *a is an alias for the actual variable a inside the main function:

Therefore:

 The actual variable (a inside the main function) is updated !!!