Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming...

Preview:

DESCRIPTION

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; }

Citation preview

Passing Function Passing Function Arguments by Arguments by

Reference :Reference :A Sample Lesson for CS 1 A Sample Lesson for CS 1

using theusing the C Programming Language C Programming Language

Andy D. DighAndy D. DighFriday, May 29th, 1998Friday, May 29th, 1998

So far, all the function calls we’ve So far, all the function calls we’ve seen have passed one or more seen have passed one or more arguments into a function using a arguments into a function using a method we call method we call pass by valuepass by value..

Consider our call to function Consider our call to function find_max:find_max:

Passing Function Arguments by Passing Function Arguments by ValueValue

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: Num2:7 4

Now, once function find_max is called we have:

In Our Main Program:

Num2:7Num1: 4

In find_max:

7x: 4y: Temp:

Max: ?

?

Now, the maximum is determined in our subprogram and stored in Temp:

In Our Main Program:

Num2:7Num1: 4

In find_max:

7x: 4y: Temp:

Max: ?

7

Now, the maximum is determined in our subprogram and stored in Temp:

In Our Main Program:

Num2:7Num1: 4

In find_max:

7x: 4y: Temp:

Max: ?

7

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:7Num1: 4 Max: 7

The functions we’ve seen with pass The functions we’ve seen with pass by value have also always returned a by value have also always returned a singlesingle value. value.

This has corresponded nicely to our This has corresponded nicely to our understanding of functions from understanding of functions from mathematics.mathematics.

Passing Function Arguments by Passing Function Arguments by ValueValue

F(x) = x2 + 3

F(2) = 22 + 3 = 7

They’ve also never modified a They’ve also never modified a variable in the calling function.variable in the calling function.

But, sometimes we aren’t always But, sometimes we aren’t always interested in cranking out one result interested in cranking out one result in our subprograms. That is, we in our subprograms. That is, we want our subprograms to produce want our subprograms to produce multiple values.multiple values.

Passing Function Arguments by Passing Function Arguments by ValueValue

In effect, we want to be able to alter In effect, we want to be able to alter a variable in the calling function. This a variable in the calling function. This is where is where pass by referencepass by reference comes in comes in handy. handy.

ProblemProblem: Write a subprogram to : Write a subprogram to swap the contents of two variables in swap the contents of two variables in the calling function.the calling function.

Passing Function Arguments by Passing Function Arguments by ReferenceReference

We want to input two integer variables We want to input two integer variables num1num1 and and num2num2 to a function to a function swap,swap, swap their contents, and return swap their contents, and return these changes to the calling these changes to the calling function.function.

Specification for SwapSpecification for Swap

y:

x: 5

3 y:

x: 3

5

Swap

Function

Your first thought(a common one) :

x = y;

y = x;

X

The Swapping AlgorithmThe Swapping Algorithm

Y

Algorithm for SwapAlgorithm for Swap

A temporary bucket is mandatory to make this algorithm work.

temp = x;

x = y;

y = temp;

X

Temp

Y

•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 AlgorithmCoding Our Swap Algorithm

Coding Our Swap AlgorithmCoding Our Swap AlgorithmFor instance, the following function will not work:

void swap (int x, int y) /* WRONG */{

int temp;

temp = x;x = y;y = temp;

}

•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 AlgorithmCoding Our Swap Algorithm

Coding Our Swap AlgorithmCoding 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:

x: y: Temp:Num1 Address

Num2 Address

Coding Our Swap AlgorithmCoding Our Swap Algorithm

void swap (int *x, int *y) /* Switch *x and *y */

{

int temp;

temp = *x; *x = *y; *y = temp;

}

Coding Our Swap AlgorithmCoding 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 PointsSummary 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.

Recommended