Home > Back-end >  When a custom function has no return value function function
When a custom function has no return value function function

Time:09-22

# include
Int p=20;
Void f (int a)
{
int b;
B=+;
P +=b;
}
Int main (void)
{
Int a=1, b=5;
F (a);
P +=b;
Printf (" % d, % d, % d ", a, b, p);
return 0;
}

Why f () function has no return value, but can change the value of the variable p, not to say that a custom function can not be returned numerical?
For help

CodePudding user response:

P is a global variable which function can be changed

CodePudding user response:

F () function has no return value, but in the body of the function to modify a global variable p, this is the role of f () function, it is important to note that although the f () to modify parameter a is to go, but didn't actually change the value of the argument a at this time a value is 1, so the f () function parameter of the transmission way is called the reference ,
For the reference of function parameters and the reference, please refer to the
"C + + function array parameter"
Wish I could help you!

CodePudding user response:

Return value to change it doesn't matter, you can hand in the address change value of

CodePudding user response:

Function return value just according to your requirements to operate, perhaps to simple, perhaps to get success, but these are according to your requirements, you can write no, then just finish operation
Variables are divided into local and global variables, usually use local variables in the function, also is the function of their definitions, the lifecycle of the local variable from the function call start to finish, so if there is a need to other function will return the value of this variable to other functions the acquisition, the variable amount and end of the program after the die
And you here, p is defined as global variables, global variables, life cycle for the entire program is running cycle, and can be so function call, the changed value variables still exist, so there will not die, and other functions can be used directly, so there is no return value, the role of this function is also changing the value of this variable
  • Related