Call by value / result (aka copy / restore)

I read the book "Programming Languages", learning something about the passage of parameters, and there was doubt about the call by value / restoration.

I understand how this works in general cases, but consider this case:

procedure P(x, y, i){
    x[i]=y[5-i]
    if( i<4 ) P(x, y, i+1)    
}

procedure main(){
    a=(1, 2, 3, 4, 5)
    P(a, a, 0)
}

After calling P (a, a, 0), a local copy of both "a" is created (allows you to call them a_0 and a_1). But when he returns, a_0 and a_1 will have different meanings.

a_0 = (5, 4, 3, 2, 5)
a_1 = (1, 2, 3, 4, 5)

Therefore, when he tries to restore "a" with a new value ... we find two different possibilities. What will be here? Will it be restored first by a_0 and overwritten by a_1? What would be the meaning of "a" after P (a, a, 0)?

, - , .

.

+3
2

:

  • . . , .

  • , . . , , , , .

  • undefined, . C ​​ . , .

, , / . , ( , , ), - , .

: -.

+3

, . , - ; , .;)

, , call-by-copy, - Fortran. Fortran, Fortran-2008 , - ​​ Fortran, , .

POINTER nor TARGET, , undefined . ,

CALL SUB (A (1:5), A (3:9))

A(3:5) , undefined , , undefined , , . A(1:2) A(6:9) . ISO/IEC 1539-1: 2010 (E), . 301, 12.34

, Fortran call-by-reference call-by-copy, .

Objective-C ( ), ; , . , , , , , a y, of x. :

cat >pass-by-copy.m <<EOF
#import <Foundation/Foundation.h>

void sub(NSString **x, NSString **y) {
    *y = @"bar";
    *x = @"baz";
}

int main() {
    NSString *a = @"foo";
    sub(&a,&a);  // looks like call-by-reference, is actually call-by-copy
    NSLog(@"%@\n", a);  // prints "bar", not "baz"
}
EOF
clang -fobjc-arc -framework Foundation pass-by-copy.m -o pass-by-copy
./pass-by-copy

2012-09-02 22:37:26.677 pass-by-copy[72719:707] bar
0

All Articles