So I have two questions, the first is, why am I getting 0 back for my b value? I constructed the code below, and I can get it to return e=9 and g=30. |
You are confusing the term "return" with something else.
A function "returns" exactly 1 thing (or nothing, if the function returns void). Whatever value gets returned sort of "replaces" the function call in the calling code.
Example:
Here, fu is called, and whatever it
returns kind of replaces it in that line of code. So if fu returns 5, it's be like
int foo = 5;
.... the returned value would be assigned to 'foo'.
This has absolutely nothing to do with passing by value or by reference. Those are parameters, and the return value is something completely different.
Now when you pass a parameter by value... it is like making a copy of that value. The parameter itself is a new variable. So again with the above
int foo = fu(x,y,z);
example...
x and z are being passed by reference. So inside of fu, 'a'
IS x and 'c'
IS z. Changing x inside of fu will also change a in main because the vars are one and the same. Ditto for z<->c.
b, however, is passed by value, and therefore is a copy. But it's still a variable... so fu can still make changes to b like normal -- the difference is that those changes will
not be made to 'y' because they are two different variables.
So back to your example:
Here:
- x and y are passed by reference. So any changes made to a,c will also change x,y.
- 10 is passed by value. So b is a new variable that equals 10. Changes can still be made to b, but they will not change '10'.
- whatever value fu returns will be sent to cout, and will be printed on the screen.
The second question I have is, since the question asks the value of the function fu, and the function says return b;, does that mean I should return b=0 or b=10? |
You return whatever value is stored in the 'b' variable.
Also, is it normal for it to be returning a=9 as an int? I thought it should return as a double, but it's giving me an int for the answer. |
What makes you think it's an int? 'a' is clearly a double.