what should this output?

Hi


1
2
3
4
5
6
7
8
9
10
11
12
void f1( void )
{ 
int x = 5;
f2(x);
cout << x <<endl;
}

void f2(int x)
{
x += 5;
cout << x <<endl;
}


Im studying for an upcoming test and one of the practice problems is write the output of this code, I think it should be:

10
10

but im not 100% sure, can anyone clarifying the process that is going on here?
Last edited on
@Omar Alamy

Why not just create a simple program with those two functions, and check out the results? You learn more by doing, than reading..
I'll just let you know that you're incorrect. You can run the program to see the output, if you wanted... You'll want to look up "copy by value" to see why. (Using copy by reference would give the output that you suggested.)
Last edited on
I assume this is the missing function:

1
2
3
4
int main()
{
  f1();
}


In which case the output will be
10
5


The steps are as follows:
Call f1.
f1 creates variable x, set to 5.
f1 calls f2, passing in 5.
f2 adds 5 to what was passed in (which has NO EFFECT on the x in f1), and outputs that value (10).
f2 ends.
f1 outputs the variable x it has, which has a value 5.
f1 ends.
ahhh, makes sense, thanks.
Topic archived. No new replies allowed.