0
#include <stdio.h>

void set_b_to_a(int, int);

int main()
{
    int a, b;
    a=1;
    b = 15;
    set_b_to_a(a, b);
    printf("%d", b);
}

void set_b_to_a(int a, int b)
{
    b=a;
}

It should return b=1. BUT it returns b=15! What is wrong? Is the problem that it's a void function, meaning it won't return anything? Can someone explain? Thanks.

ya23
  • 14,226
  • 9
  • 46
  • 43
  • 1
    No, void function is fine. Your code should print 1 for sure. Very strange if it does not – har07 Nov 25 '13 at 14:09

2 Answers2

3

The problem is that the parameters are passed by value to your function, so their original values are copied to new local variables inside the function body. The function body is a world apart, so to speak (actually, it is a scope apart), and in that scope the local copies of the value parameters are the only ones which exist.

Outside this small world, i.e. when the function ends, you have your original variables back - because you only sent copies of them to the function.

In contrast, parameters passed by reference will give the behaviour you want. In C++, you can achieve that by using the following signature:

void set_b_to_a(int& a, int& b);
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • 1
    +1 Correct, assuming it's C++. In C you need to use pointers. – ya23 Nov 25 '13 at 14:15
  • "Outside this small world, i.e. when the function ends, you have your original variables back - because you only sent copies of them to the function." So what is the point of the function if it does nothing at all? I mean no changes to the variables, so what is the point of using the function? – SasukeItachi UchihaClan Nov 25 '13 at 14:26
  • Another question! The functions we use are not by reference (we don't say getchar(&c)! But still, what we say is passed to the function, and then the function returns what it returns , I mean it does the job of "call by reference" without using &s, can someone explain? – SasukeItachi UchihaClan Nov 25 '13 at 14:41
  • 1
    @user3032632 The particular function you constructed has no point at all. It creates some variables, changes their values, and then destroys them. There's no point to that. Instead, write a function that does something useful. – David Schwartz Nov 25 '13 at 14:42
  • 1
    @user3032632 Say I want you to put a number in a box where I can get it. I can do this two ways. I can pass you the box by reference, so you modifying the box modifies the box I'm going to look in. Or, I can pass you the *location* of the box by value. You don't need to modify the location of the box, but you can *use* that location to modify the contents. The one thing I *can't* do is pass you the box itself by value, because then there's no way for you to change the contents. – David Schwartz Nov 25 '13 at 14:43
  • @user3032632 In C++ you would call `void set_b_to_a(int& a, int& b);` just by writing `set_b_to_a(a, b);`, with no `&` symbols - because the parameters are _already_ references. Other than that, I am not going to give you a basic C++ course in a 500 character comment. – Daniel Daranas Nov 25 '13 at 14:46
  • @user3032632 Ok, then I am not going to give you a basic C course in a 500 character comment. – Daniel Daranas Nov 25 '13 at 14:49
1
void set_b_to_a(int a, int b)
{
    b=a;
}

The fact that this variable is called b has no significance. It's a formal parameter that can have any name at all, and this function behaves exactly the same as if you declared it this way:

void set_b_to_a(int not_a, int not_b)
{
    not_b=not_a;
}

The fact that both variables have the same name doesn't create some kind of relationship between them. They're two unrelated variables with different scopes and changing the value of one has no effect on the other.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    If you know that, then why was the program's behavior confusing to you? If you realized the two variables were unrelated, just coincidentally having the same name, why did you expect changing one to change the other? – David Schwartz Nov 25 '13 at 14:42
  • 1
    @user3032632 That _is_ the problem. You are free not to understand it, but the content posted in SO must be correct - and this answer is, *and* your comment is not. – Daniel Daranas Nov 25 '13 at 14:50