13

When pass a variable to a function, why the function only gets a copy/duplicate of the variable?

int n=1;

void foo(int i)
{
    i++;
}

As we all know, the function foo() can't change the value of n by using foo(n).

Of course we can pass the address of the variable to make some change to the parameter variable.
But don't you think that is a little bit inconvenient?

Why c/c++ is designed to only give a duplicate to the function instead of directly give the "real" variable itself to the function?
What't the pro/benefit of this paradigm?


Update:
I have read @paxdiablo's answer. I think his "encapsulation, modularity and localisation of effect" explanation is good.
But in my way, it can also preserve the parameter argument's value as well. It can also realize encapsulation. By this way:(assume the function can directly get the "real" variable instead of a duplicate by default )

void foo(int n)
{
    int temp=n;
    //Do something to temp...

}

And in my way, the complicated mechanism,such as "pass by reference" or pointer can be eliminated when you do want to change the value of parameters passed in. That's the benifit.

After a time of thought. I realise the reason why c/c++ isn't designed as I proposed is just because of the INCONVINIENCE of my way!
In my way, if a function has a long list of variables, it would to terrible. What I thougth is the more convenient way is infact inconvenient:
You must write like this:

void foo(int a,int b,double c,float d,char s...)
{
    int temp1=a;
    int temp2=b;
    double temp3=c;
    float temp4=d;
    char temp5=s;
    ...
    //Do something to temp{1,2,3,4,5....}

}

So the designers of c/c++ introduce complex mechanism to trade off with convenience.
Am I right?

Anonymous
  • 125
  • 7
duleshi
  • 1,966
  • 2
  • 21
  • 32
  • possible duplicate of [Pass by Reference / Value in C++](http://stackoverflow.com/questions/410593/pass-by-reference-value-in-c) – iammilind Jul 31 '12 at 09:01
  • possible duplicate of [What's the difference between passing by reference vs. passing by value?](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – iammilind Jul 31 '12 at 09:02
  • @iammilind: NO. I understand the concepts of passing by reference/value well.I'm not asking that. – duleshi Jul 31 '12 at 09:12

9 Answers9

12

The are basically two schools of thought on this matter.

The first is pass-by-value where a copy of the value is created for the called function.

The second is pass-by-reference where the parameter that appears in the called function is an "alias" of the original. That means changes you make to it are reflected in the original.

C is generally a pass-by-value language. You can emulate pass-by-reference by passing the address of a variable and then using that to modify the original:

void setTo42 (int *x) { *x = 42; }
:
int y;
setTo42 (&y);
// y is now 42

but that's more passing the pointer to a variable by value, than passing the variable itself by reference.

C++ has true reference types, possibly because so many people have trouble with C pointers :-) They're done as follows:

void setTo42 (int &x) { x = 42; }

:
int y;
setTo42 (y);
// y is now 42

Pass-by-value is usually preferable since it limits the effects that a function can have on the "outside world" - encapsulation, modularity and localisation of effect is usually a good thing.

Being able to arbitrarily modify any parameters passed in would be nearly as bad as global variables in terms on modularity and code management.

However, sometimes you need pass-by-reference since it might make sense to change one of the variables passed in.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
5

Most modern languages are defined to use pass by value. The reason is simple: it significantly simplifies reasoning about the code if you know that a function cannot change your local state. You can always pass by non-const reference if you want a function to be able to modify local state, but such cases should be extremely rare.

EDITED to respond to updated question:

No, you're not right. Pass by value is the simplest mechanism for passing parameters. Pass by reference or copy-in/copy-out are more complex (and of course, Algol's expression replacement is the most complicated).

Think about it for awhile. Consider f(10). With call by value, the compiler just pushes 10 on the stack, and the function just accesses the value in situ. With call by reference, the compiler must create a temporary, initialize it with 10, and then pass a pointer to it to the function. Inside the function, the compiler must generate an indirection each time it accesses the value.

Also, protecting from modification inside the function doesn't really help readability. If the function takes no reference parameters, you know without looking inside the function that it cannot modify any variables you pass as arguments. Regardless of how anyone modifies the function in the future. (One could even argue that functions should not be allowed to modify global state. Which would make the implementation of rand() rather difficult. But would certainly help the optimizer.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
3

Because C pass arguments by value.

From Kernighan & Richtie 2nd edition : (1.8 Call by value) "In C all function arguments are passed by "value""

ouah
  • 142,963
  • 15
  • 272
  • 331
1

If you really want a function to change its actual parameter, you can pass it by reference in C++

void foo(int& i)
{
    i++;
}
Andriy
  • 8,486
  • 3
  • 27
  • 51
0

It is to make sure that the function doesn't change the original value.

Jeeva
  • 4,585
  • 2
  • 32
  • 56
0

I guess one reason is efficiency, it's more efficient to access values directly then through a pointer. Another advantage is choice, the C/C++ way you can choose to pass arguments by value or by pointer, your way you only have the choice to pass by pointer. But most importantly passing by value means that your function is isolated from the rest of your code, and changes to variables inside the function don't affect the rest of the code. Believe me you would get far more bugs and coding would be more difficult if this were not the case.

jahhaj
  • 3,021
  • 1
  • 15
  • 8
  • How can it be more efficient? What if the argument is a class object that contains lots and lots of data. Creating a copy of a huge object when passing by value is hardly efficient. – mathematician1975 Jul 31 '12 at 08:53
  • This is true, I was thinking of the built in types. – jahhaj Jul 31 '12 at 08:54
  • @mathematician1975: What if the argument is an int (like the OP's example)? Copying it is efficient, and accessing the copy directly is more efficient than accessing the original through a dereferenced pointer. That's how it can be more efficient. – Benjamin Lindley Jul 31 '12 at 08:55
  • @BenjaminLindley Yes I totally agree. I was just saying that pass by value is not necessarily more efficient in ALL cases. – mathematician1975 Jul 31 '12 at 08:57
  • @mathematician1975: I couldn't have known that you meant that by what you said, *"How can it be more efficient?"*, which implies that you couldn't think of any way in which it *could* be more efficient. – Benjamin Lindley Jul 31 '12 at 09:01
  • 1
    Whatever. Efficiency isn't the main motivation. Correct code is. C (and other languages) learned from Fortran's mistakes. (In early versions of Fortran, you could write something like `f(10)`, and the function `f` could modify the `10` so that it would have some other value the next time you executed this line.) – James Kanze Jul 31 '12 at 09:06
0

In order to change the value of a parameter you need to pass by reference using the '&' symbol.

The reason this is done is so that you can choose whether or not you want changes to stick with your variable. If you pass by value you can be sure that it will not change and cause an error in your program.

Troy Cosentino
  • 4,658
  • 9
  • 37
  • 59
0

One reason C is pass-by-value is that in FORTRAN, which is pass-by-reference, it was possible that you could call a subroutine with a call like “CALL MYROUTINE(3)”, and the subroutine would change the value of its argument. Then, after that point in the program, “3” would have the value given to it by the subroutine.

Naturally, when this occurred, it caused great confusion. It made bugs hard to find, because source code did something very different from what it appeared to do.

So, as people have learned about programming languages, one of the principles used in designing languages is to avoid things that made code hard to understand or that made bugs more likely. (This principle is not always successfully applied, as we are also tempted to give programming languages more expressive power and to enable compilers to optimize code.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
-1

Depending on what you're trying to do, you can do this:

int n = 1;

n = foo(n);

Just make sure foo(int i) returns i after modifying it.

Community
  • 1
  • 1
fvgs
  • 21,412
  • 9
  • 33
  • 48