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

前端 未结 9 1288
野的像风
野的像风 2020-12-06 18:05

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

相关标签:
9条回答
  • 2020-12-06 18:22

    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.)

    0 讨论(0)
  • 2020-12-06 18:23

    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.)

    0 讨论(0)
  • 2020-12-06 18:25

    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.

    0 讨论(0)
  • 2020-12-06 18:29

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

    void foo(int& i)
    {
        i++;
    }
    
    0 讨论(0)
  • 2020-12-06 18:30

    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.

    0 讨论(0)
  • 2020-12-06 18:31

    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.

    0 讨论(0)
提交回复
热议问题