Altering Variables in the Calling Function

I l @ ve RuBoard

Altering Variables in the Calling Function

Sometimes you want one function to make changes in the variables of a different function. For example, a common task in sorting problems is interchanging the values of two variables. Suppose you have two variables called x and y and you want to swap their values. The simple sequence

 x = y; y = x; 

does not work because, by the time the second line is reached, the original value of x has already been replaced by the original y value. An additional line is needed to temporarily store the original value of x .

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

Now that the method works, you can put it into a function and construct a driver to test it. To make clear which variables belong to main() and which belong to the interchange() function, Listing 9.14 uses x and y for the first, and u and v for the second.

Listing 9.14 The swap1.c program.
 /* swap1.c -- first attempt at a swapping function */ #include <stdio.h> void interchange(int u, int v); /* declare function */ int main(void) {     int x = 5, y = 10;     printf("Originally x = %d and y = %d.\n", x , y);     interchange(x, y);     printf("Now x = %d and y = %d.\n", x, y);     return 0; } void interchange(int u, int v)  /* define function  */ {     int temp;     temp = u;     u = v;     v = temp; } 

Running the program gives these results:

 Originally x = 5 and y = 10. Now x = 5 and y = 10. 

Oops! The values didn't get switched! Let's put some print statements into interchange() to see what's gone wrong (see Listing 9.15).

Listing 9.15 The swap2.c program.
 /* swap2.c -- researching swap1.c */ #include <stdio.h> void interchange(int u, int v); int main(void) {     int x = 5, y = 10;     printf("Originally x = %d and y = %d.\n", x , y);     interchange(x, y);     printf("Now x = %d and y = %d.\n", x, y);     return 0; } void interchange(int u, int v) {     int temp;     printf("Originally u = %d and v = %d.\n", u , v);     temp = u;     u = v;     v = temp;     printf("Now u = %d and v = %d.\n", u, v); } 

Here is the new output:

 Originally x = 5 and y = 10. Originally u = 5 and v = 10. Now u = 10 and v = 5. Now x = 5 and y = 10. 

Nothing is wrong with interchange() ; it does swap the values of u and v . The problem is in communicating the results to main() . As we pointed out, interchange() uses different variables from main() , so interchanging the values of u and v has no effect on x and y ! Can you somehow use return ? Well, you could finish interchange() with this line:

 return(u); 

Then change the call in main() to this:

 x = interchange(x,y); 

This change gives x its new value, but it leaves y in the cold. With return you can send just one value back to the calling function, but you need to communicate two values. It can be done! All you have to do is use pointers.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net