0
How come this function can return a value without the return statement?
#include <iostream> using namespace std; void func(int&a){ a=5; } // no "return a;" statement, yet still returns the value 5 back to main()... int main() { int x = 2; func(x); cout<< x; return 0; } // Outputs 5. Do formal parameters with reference(s) to the actual argument's variable(s) NOT need return statements because both the reference and argument variable are pointing to the same place in the computer's memory?
2 Respuestas
+ 1
In the function you have declared the function to take some parameter yes, but the parameter is already fixed in your case. void func(int&a) {
int a=5
}
so even wen calling it down to the main it will still carry with it all the attributes u had provided it with
Its just like telling somebody //this is a black pen
You already have specified that the pen is black hence it cant be red nor blue if that person wanted to change it to a different color
So in ur case uve told the main that func takes in a parameter and that parameter is already initialized to 5 so u cant change it
+ 1
https://code.sololearn.com/cgOY9vXGw9rv/?ref=app
Take a look at this