Specific styles for passing parameters: Call-By-Value, Call-By-Name, etc.

Studying my final exam and stumbled upon this question with a previous exam:

Consider the following program written in a C-like notation:

int i = 1;
A[] = {4, 0, 1, 2};

void mystery05(int from, int to)
{
    int temp;
    temp = A[from];
    A[from] = A[to];
    A[to] = temp;
    i = i + 2;
    to = -1;
}

int main(void)
{
    mystery05(A[i+2], A[i]);
}

In the table below, fill in the fields with the corresponding variable values ​​after calling mystery05 in the main one. Each line corresponds to a specific style of parameter passing (i.e., use the specified style, not the default C-language semantics). Suppose arrays are indexed from 0.

style               |___i___|__A[0]__|__A[1]__|__A[2]__|__A[3]__| 
call-by-value       |_______|________|________|________|________|
call-by-name        |_______|________|________|________|________|
call-by-reference   |_______|________|________|________|________|
call-by-value-result|_______|________|________|________|________|

I'm not sure how to do this, but if it was regular C semantics, I assumed that the answers would be

i = 3; A [0] = 4; A [1] = 2; A [2] = 1; A [3] = 0

+3
source share
2

@S.Lott: , " . ?

: . , . , , , . !

: , ; proc i.

A .

proc, .

i 1, A [3] A [1] .

A [3] 0, A [1] 2. A [0] A [2] .

3

, q , "to" proc.

+1

call-by-value - - " C"

call-by-name - , C . . " C"

call-by-reference , "&" "*". " C", , C.

call-by-value-result - , C . .

. C. , C. -, .

0

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


All Articles