Call by reference, value, and name

前端 未结 5 875
陌清茗
陌清茗 2020-12-20 16:46

I\'m trying to understand the conceptual difference between call by reference, value, and name.

So I have the following pseudocode:

foo(a, b, c)
{
           


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 17:26

    This won't change the value of X, Y or Z if it is pass-by-value. When you use a function such as "foo()", it basically copies the variables (x, y and z) into other variables (a, b, and c) and does certain actions with them, without changing the originals (x, y and z). For you to change a value you would have to return a value, something like this:

    foo(a, b, c)
    {
    a = a++;
    b = b++;
    c = a + b * 10;
    return c;
    }
    
    x = 1;
    y = 2;
    z = 3;
    z = foo(x, y+2)
    

    Then x and y would be the same, but z would be (x+1)+(y+1)*10 which in this case would be 32.

提交回复
热议问题