Reputation: 6137
I have a code that does something like following example, and I'm not sure if this is correct because the executable runs as expected.
// source.cpp
void compute_x(int& ref)
{
ref = 0;
}
void f(int x)
{
int local = x;
local = 1;
if (local)
{
return copute_x(local);
}
else return;
}
int main()
{
f(2);
return 0;
}
the code runs but, Is variable local
valid once f
returns?
Upvotes: 0
Views: 43
Reputation: 136
No.the variable local is not valid as its scope is inbetween the braces of the f function defination ...after that It has no existence
Upvotes: 0
Reputation: 4770
The variable local
goes out of scope after f
returns.
After your edit: but the return value is returned from the function and subsequently returned from main
.
Upvotes: 1