may anybody tell me what is the output of this function and how?? does the type of function(void) affect on the output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

may anybody tell me what is the output of this function and how?? does the type of function(void) affect on the output?

void function (int x) { x=8; } int main() { int x=3; func(x); cout<<x; return 0; }

22nd Dec 2021, 6:25 AM
Nariman Tajari
Nariman Tajari - avatar
1 Answer
+ 3
The function assigns 8 to a local parameter. At the end of the function, the local parameter x is deleted, and thus the function does effectively nothing. The output will therefore be 3, which is the value x was given in main. Note: Do not get confused that the variable and parameter name have the same letter. They are entirely different entities from the program's point of view. The return type of the function may play a role, if for example its result would be assigned to x in main: int func(int x) { x = 8; return x; } int main() { int x = 3; x = func(x); cout << x; } Now, x in main would be rewritten with 8 returned from func.
22nd Dec 2021, 7:37 AM
Agnes Ahlberg
Agnes Ahlberg - avatar