Hello,

Could anyone explain details about three different case and output. Here is the code and output:

#include <stdio.h>


void foo(int n)
{
n = 6;
}

void fooWithRef(int& n)
{
n = 6;
}

int main()
{
int n;
foo(n);
printf("Case_1 Result: %d \n", n);

n = 4;
foo(n);
printf("Case_2 Result: %d \n", n);

fooWithRef(n);
printf("Case_3 Result: %d", n);
}

Output:
Case_1 Result: 134521641
Case_2 Result: 4
Case_3 Result: 6

Please discuss the reasons about the different outputs.

Thanks.

In case 1 n is uninitialized, so you're printing whatever random garbage was at that memory location. In case 2 n is initialized, but once again foo() doesn't change it, so the value remains 4. In case 3, fooWithRef() does change the value of n, so you get the new value.

The difference is that foo() makes a copy of the value you pass while fooWithRef() creates an alias to the object. It can help to see that function parameters are not the same thing as the arguments by not naming them the same:

void foo(int value)
{
    value = 6;
}
void fooWithRef(int& alias)
{
    alias = 6;
}

In practice, what's happening is closer to this:

int main()
{
    int n;

    {
        int __temp = n;
        foo(__temp);
    }
    printf("Case_1 Result: %d \n", n);

    n = 4;
    {
        int __temp = n;
        foo(__temp);
    }
    printf("Case_2 Result: %d \n", n);

    {
        int& __temp = n;
        fooWithRef(__temp);
    }
    printf("Case_3 Result: %d", n);
}

So as long as you understand that a reference is an alias for an object and changes to the alias also change the original, while a non-reference doesn't alias the object and only copies the value, the behavior is perfectly reasonable. :)

Hi,
Thanks for reply.
I have some ideas about it. Please let me know is it right or not.
case_1: n is uninitialize and the foo() does not return any value. So it prints a garbage value.
case_2: n is initialize but foo() does not return any value. so it prints the initialized value.
case_3: The fooWithRef() defines n as a int reference. So it takes the n object's reference and replace value by 6. The result comes 6, because the address contains a valid value instead of garbage value.

Am I right? Any comment appreciated.

Two nitpicks:

  1. We're not talking about return values here, we're talking about parameters and arguments.
  2. A reference is not the same thing as a pointer (though it may be implemented as a pointer under the hood).

Case_1 isn't really accurate when you say foo() doesn't return a value SO it prints a garbage vaule, because functions often change the value of a variable directly without ever returning a value. This is accomplished by using pointers and refereneces, or by directly changing a global variable. So the fact that foo() doesn't return anything isn't of much relevence.

I'm aggred with you for case_1 (MandrewP). What do you think about Case_2 and Case_3?

Here are the two key questions that you need to understand for case_1 & 2:

1). In case_1 and case_2, the value of n is changed, but the function did not change it. Why then did n change?
2). The value of n was passed to the function, so why didn't the function change the printed out value of n?

Keep in mind that the function returns no value and this is irrelevant as to why n is not changed when printed out. In case_3 the function doesn't return a value either, yet it changes n. So what's going on? You need to know and be able to articulate it clearly in your own words.

So think more about it and take another crack at answering it. We can help you out from there. These are key points that you really need to clearly grasp, for it will help you when you are using functions. Otherwise, you will be struggling when trying to use functions. Might as well get it clear now.

This article has been dead for over six months. Start a new discussion instead.