Home > Back-end >  function
function

Time:10-04

#include
Int a1=300, a2=400;
Void sub1 (int x, int y)
{a1=x;
X=y;
Y=a1;
}
Int main ()
{
Int a3=100, a4=200;
Sub1 (a3, a4);
Sub1 (a1, a2);
Printf (" % d, % d, % d, % d \ n ", a1, a2, a3, a4);
return 0;
}
Please answer me why the output is 100400100200

CodePudding user response:

Because sub1 calls this function is the value, that is to say, this function is to obtain a copy of the parameter value, modify the copy function, but does not affect the caller of the actual parameter passed to it,

CodePudding user response:

Void sub1 (int x, int y)
{a1=x;
X=y;
Y=a1;
}
Function:
Void sub1 (int * x, int * y)
{
A1=* x;
* x=* y;
* y=a1;
}
The main function of such call
Sub1 (& amp; A3, & amp; A4);

CodePudding user response:

A1 is a global variable, the value of the a1 is the last time for the assignment of values,
First is 300 assignment a1, 100, the second is a1 assigned to a1, namely or saved 100.
A2, a3, a4 values, the same is the cause of the parameters of the exchange is a variable rather than address,