When a variable is passed to a function, why does the function only get a duplicate of the variable?

When a variable is passed to a function, why does the function only get a copy / duplicate of the variable?

int n=1; void foo(int i) { i++; } 

As we all know, the function foo () cannot change the value of n with foo (n).

Of course, we can pass the address of the variable to make some changes to the parameter variable.
But don't you think this is a little uncomfortable?

Why is c / C ++ intended only for duplicating a function, and not for directly providing a "real" function variable?
What is the use of this paradigm?


Update:
I read @paxdiablo's answer. I think his explanation of "encapsulation, modularization and localization of the effect" is good. But in my way, it can also save the value of the parameter. It can also implement encapsulation. Thus: (suppose the function can directly get the "real" variable instead of the default duplicate)

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

And in my opinion, a complex mechanism, such as "pass by reference" or a pointer, can be deleted when you want to change the value of the passed parameters. It's profitable.

Over time. I understand the reason why c / C ++ is not designed, as I suggested, simply because of the CONTINUITY of my path!
In my opinion, if a function has a long list of variables, it will be terrible. What I'm doing in a more convenient way is inconvenient:
You should 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....} } 

Thus, c / C ++ developers introduce a sophisticated mechanism to compromise convenience.
I'm right?

+6
source share
9 answers

Basically these are two schools of thought on this subject.

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

Secondly, it is pass-by-reference, where the parameter that appears in the called function is the "alias" of the original. This means that your changes are reflected in the original.

C is usually a transition language. You can emulate a pass-by-reference by passing the address of a variable, and then using this to change the original:

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

but this passes the pointer to the variable by value more than passing the variable itself by reference.

C ++ has true reference types, perhaps because many people have problems with C pointers :-) They are made as follows:

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

Passing by value is usually preferable because it limits the effects that the function for the “outside world” may have - encapsulation, modularization and localization of the effect are usually good.

The ability to arbitrarily change any parameters passed will be almost as bad as global variables in terms of modularity and code management.

However, sometimes you need to pass by reference, because it makes sense to change one of the transferred variables.

+7
source

Most modern languages ​​use pass by value. The reason is that it is simple: it greatly simplifies the discussion about the code if you know that the function cannot change your local state. You can always pass a non-constant link if you want the function to be able to change the local state, but such cases should be extremely rare.

EDITED will answer an updated question:

No you are not right. Pass by value is the easiest parameter passing mechanism. Passing by reference or copying / copying is more complicated (and, of course, substituting an Algol expression is the most difficult).

Think about it for a while. Consider f(10) . When called by value, the compiler simply pushes 10 onto the stack, and the function simply accesses the in situ value. When called by reference, the compiler must create a temporary one, initialize it with 10 , and then pass a pointer to it with a function. Inside the function, the compiler must generate an indirect call every time it accesses the value.

In addition, protection against modification within a function does not really help readability. If a function does not accept reference parameters, you know, without looking inside the function, that it cannot change any variables that you pass as arguments. No matter how someone modifies this feature in the future. (You could even argue that functions should not allow global state changes, which complicates the implementation of rand() , but will certainly help the optimizer.)

+4
source

Because C passes arguments by value.

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

+3
source

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

 void foo(int& i) { i++; } 
+2
source

You must make sure that the function does not change the original value.

0
source

I believe that one of the reasons is efficiency, accessing values ​​directly and then through a pointer is more efficient. Another advantage is the choice, the C / C ++ method, which you can choose to pass arguments by value or pointer, since you have the choice to pass arguments. But most importantly, passing by value means that your function is isolated from the rest of your code, and changes to the variables inside the function do not affect the rest of the code. Believe me, you will get much more errors and coding will be more difficult if it is not.

0
source

To change the parameter value, you need to follow the link using the '&' symbol.

The reason for this is because you can choose whether you want the changes to be inserted into your variable. If you pass by value, you can be sure that it will not change and will not cause an error in your program.

0
source

Depending on what you are trying to do, you can do this:

int n = 1;

n = foo (n);

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

0
source

One of the reasons C is pass-by-value is because in FORTRAN, which is a pass by reference, it is possible that you could call a subroutine with a call like "CALL MYROUTINE (3)", and change the subroutine the value of his argument. Then, after this point in the program, "3" will have the value given to it by the subroutine.

Naturally, when this happened, it caused a lot of confusion. This made it difficult to find errors because the source code was doing something very different from what it was apparently doing.

So, since people learned about programming languages, one of the principles used in developing languages ​​is to avoid making code difficult to understand or making mistakes more likely. (This principle is not always successfully applied, since we also tend to give programming languages ​​more expressive power and allow compilers to optimize the code.)

0
source

Source: https://habr.com/ru/post/921747/