<C Primer Plus >2 Altering Variables in the Calling Function

 1 #include <stdio.h>
 2 int diff(int x, int y);//the first form
 3 void interchange(int *u, int *v);//the second form
 4 int main(void){
 5     int x = 9;
 6     int y = 5;
 7     int z;
 8     printf("The Originally x = %d and y = %d\n", x, y);//alter variables in the calling function
 9     interchange(&x, &y);
10     z = diff(x, y);//need a value for some calculation or action
11     printf("Now  x = %d and y = %d\n", x, y);
12     printf("z = %d\n", z);
13     return 0;
14 }
15 
16 int diff(int x, int y){
17     return(x - y);
18 }
19 
20 void interchange(int *u,int *v){
21     int temp;
22 
23     temp = *u;
24     *u = *v;
25     *v = temp;
26 }

int function1(int x);

int function2(int *ptr);

Remeber:

1 Use the first form if the function needs a value for some calculation or action;

2 Use the second form if the function needs to alter variables in the calling function.

  One function does not have direct access to variables declared in another function. If you do need one function to acess another function's data,you can use pointer function arguments.

posted @ 2017-03-19 15:30  Micheal_you  阅读(95)  评论(0编辑  收藏  举报