Pass by value question

I'm doing a practice problem for C++ and the question is asking me what will the function below(int fu) return after the call fu(e,f,g) is made. e=3.0, f=10,g=3. It also asks for the value of e,f, and g are after the call.

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. The f value however, shouldn't it remain unchanged because it is pass by value? I thought the answer would be 10, but it's returning 0.

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? Since in the function b=0, but outside the function it shouldn't change as 10?

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int fu(double& a, int b, int& c){
    if (a<(b/c)){
        a=(b*c);
        c=b;
    }
    else if(c!=0){
        c=a*b;
        a*=a;
        b=0;
    }
    else{
        a=1;
        c=a*b;
    }
    return b;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Example program
#include <iostream>
#include <string>
int fu(double& a, int b, int& c);

int main()
{
    double x=3;
    int y=3;
     std::cout<<fu(x, 10, y);
     return 0;
    

}
int fu(double& a, int b, int& c){
    if (a<(b/c)){
        a=(b*c);
        c=b;
    }
    else if(c!=0){
        c=a*b;
        a*=a;
        b=0;
    }
    else{
        a=1;
        c=a*b;
    }
    return b;
   
}
Last edited on
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:

 
int foo = fu(x,y,z);

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:
 
std::cout<<fu(x, 10, y);


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.
closed account (EwCjE3v7)
why am I getting 0 back


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Example program
#include <iostream>
#include <string>
int fu(double& a, int b, int& c);

int main()
{
    double x=3;
    int y=3;
     std::cout<<fu(x, 10, y);
     return 0;
    

}
int fu(double& a, int b, int& c){
    if (a<(b/c)){ // fails because 3 is not less then 3
        a=(b*c);
        c=b;
    }
    else if(c!=0){ // goes to this next cuz the first failed
        c=a*b; // y now equals 30
        a*=a; // x now equals 9
        b=0; // b equals 0
    }
    else{
        a=1;
        c=a*b;
    }
    return b;
   
}


Note a and c are references(another name) to x and y
Well, the first thing is to figure out which case your code will fall into. The first if statement says if (a < (b/c)). 'a' is a double, but b and c are both integers. That means the result of b/c is also an integer, so 10/3 = 3. Since 3 is not less that 3, you don't process the first if case. Second, if c != 0. Since c==3, you can process that block.

In that case, c==30, a==9 since those are both passed by reference, e and g are both updated in place. b on the other hand is passed by value, so f remains as 10, but the function returns the local value 0 at the end.
Since learning by example helps... let's step through this and show you exactly what's happening:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
    double e = 3.0;
    int f = 10;
    int g = 3;
    std::cout<<fu(e,f,g);       // e,f,g per your example
    return 0;
}

int fu(double& a, int b, int& c){ // <- for this call, a IS e, b=f, and c IS g
    if (a<(b/c)){  // <- (3.0 < (10/3))... this is false... 
        // .. so nothing in this block matters, because it is not executed
    }
    else if(c!=0){  // <- else if c!=0 ... this is true (c==3), so this block will execute
        c=a*b;  // c=10*3.0 ... so c=30.  And since c IS g... this means that g in main is also 30 now
        a*=a;   // a=9.0 ... and since a IS e... this means that e in main is also 9.0 now
        b=0;    // b=0 ... but since b is passed by value, this does not change anything in main.
                //    it only changes 'b' inside of fu
    }
    else{
        // .. this block is not executed so it doesn't matter
    }
    return b; // <- since b==0, we are returning 0
   
}

// Overall result of the function:
// e = 9.0
// f = 10  (unchanged)
// g = 30
// return 0  (so 0 is printed, since that is what was sent to cout) 
Last edited on
Opps didn't see all the new messages. Reading them now.

I understand now. Thanks a lot Disch, you explained it very well!
Last edited on
Topic archived. No new replies allowed.