Download presentation
Presentation is loading. Please wait.
Published byEugene Small Modified over 8 years ago
1
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998
2
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: Passing Function Arguments by Value max = find_max (num1, num2);
3
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; }
4
Pictorially here showing the pass by value we have: In Our Main Program: Num1:Num2:74
5
Now, once function find_max is called we have: In Our Main Program: Num2:7 Num1: 4 In find_max: 7x: 4y:Temp: Max:? ?
6
Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: Num2:7 Num1: 4 In find_max: 7x: 4y:Temp: Max:? 7
7
Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: Num2:7 Num1: 4 In find_max: 7x: 4y:Temp: Max:? 7
8
Once our function returns our result, the function data area is always erased and will be recreated when the procedure is called again. In Our Main Program: Num2:7 Num1: 4Max: 7
9
n The functions we’ve seen with pass by value have also always returned a single value. n This has corresponded nicely to our understanding of functions from mathematics. Passing Function Arguments by Value F(x) = x 2 + 3 F(2) = 2 2 + 3 = 7
10
n They’ve also never modified a variable in the calling function. n 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 Value
11
n In effect, we want to be able to alter a variable in the calling function. This is where pass by reference comes in handy. n Problem: Write a subprogram to swap the contents of two variables in the calling function. Passing Function Arguments by Reference
12
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. Specification for Swap y: x:5 3 y: x:3 5 Swap Function
13
Your first thought (a common one) : x = y; y = x; X The Swapping Algorithm Y
14
Algorithm for Swap A temporary bucket is mandatory to make this algorithm work. temp = x; x = y; y = temp; X Temp Y
15
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
16
For instance, the following function will not work: void swap (int x, int y) /* WRONG */ { int temp; temp = x; x = y; y = temp; }
17
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
18
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.
19
Pictorially, we want: In Our Main Program: Num2: Num1: In swap: x: y:Temp: Num1 Address Num2 Address
20
Coding Our Swap Algorithm void swap (int *x, int *y) /* Switch *x and *y */ { int temp; temp = *x; *x = *y; *y = temp; }
21
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.
22
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.