220 likes | 340 Views
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language. Andy D. Digh Friday, May 29th, 1998. Passing Function Arguments by Value.
E N D
Passing Function Arguments by Reference :A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998
Passing Function Arguments by Value So far, all the function calls we’ve seen have passed one or more arguments into a function using a method we call pass by value. Consider our call to function find_max: max = find_max (num1, num2);
int find_max (int x, int y) /* Input : The values of two integers, x and y */ /* Output : Returns the larger of the two input values */ { int temp; if (x > y) temp = x; else temp = y; return temp; }
Pictorially here showing the pass by value we have: In Our Main Program: Num1: 7 Num2: 4
Now, once function find_max is called we have: In Our Main Program: 7 Num2: 4 Max: ? Num1: In find_max: y: 4 Temp: ? x: 7
Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: 7 Num2: 4 Max: ? Num1: In find_max: y: 4 Temp: 7 x: 7
Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: 7 Num2: 4 Max: ? Num1: In find_max: y: 4 Temp: 7 x: 7
In Our Main Program: 7 7 Num2: 4 Max: Num1: Once our function returns our result, the function data area is always erased and will be recreated when the procedure is called again.
Passing Function Arguments by Value • The functions we’ve seen with pass by value have also always returned a single value. • This has corresponded nicely to our understanding of functions from mathematics. F(x) = x2 + 3 F(2) = 22 + 3 = 7
Passing Function Arguments by Value • They’ve also never modified a variable in the calling function. • But, sometimes we aren’t always interested in cranking out one result in our subprograms. That is, we want our subprograms to produce multiple values.
Passing Function Arguments by Reference • In effect, we want to be able to alter a variable in the calling function. This is where pass by reference comes in handy. • Problem: Write a subprogram to swap the contents of two variables in the calling function.
Specification for Swap We want to input two integer variables num1 and num2 to a function swap, swap their contents, and return these changes to the calling function. x: 5 x: 3 Swap Function y: 3 y: 5
The Swapping Algorithm Your first thought (a common one) : x = y; y = x; X Y
Algorithm for Swap A temporary bucket is mandatory to make this algorithm work. temp = x; x = y; y = temp; X Y Temp
Coding Our Swap Algorithm • Now, we might consider calling this function by: • swap (num1, num2); • Since C passes arguments to functions by value only, there is no direct way for the called function to alter a variable in the calling function.
Coding Our Swap Algorithm For instance, the following function will not work: void swap (int x, int y) /* WRONG */ { int temp; temp = x; x = y; y = temp; }
Coding Our Swap Algorithm • Because of pass by value, swap can’t effect the arguments num1 and num2 in the routine that called it. • The function we just saw only swaps copies of num1 and num2.
Coding Our Swap Algorithm • The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed: • swap (&num1, &num2); • Since the operator & produces the address of a variable, &num1 is a pointer to num1. • In swap itself, the parameters are declared to be pointers, and the operands are accessed indirectly through them.
Pictorially, we want: In Our Main Program: Num2: Num1: In swap: y: Temp: x: Num2 Address Num1 Address
Coding Our Swap Algorithm void swap (int *x, int *y) /* Switch *x and *y */ { int temp; temp = *x; *x = *y; *y = temp; }
Coding Our Swap Algorithm • WARNING: Coding this algorithm using global variables is not an acceptable alternative to avoid passing pointers to the values to be changed. • Remember the only variables our functions should use are those in the argument list or those declared locally.
Summary of Key Points • Functions can modify the variables in the calling function using the pass by reference method. • The pass by reference method involves passing pointers to the values to be changed. • The swap algorithm is a very common application of the pass by reference method. We’ll see it again in a few weeks when coding sorting algorithms.