sippycup - 1 year ago 70

Pascal Question

I'm currently trying to understand passed by reference, value and name in a example problem.

`procedure practice (x,y,z); interger x,y,z;`

begin

y=1;

z=y-x;

end

The code given looks like and i'm supposed to show the final values of n,A[1],A[2] depending on how they are passed

`A[1] := 1;`

A[2] := 2;

n = 2;

practice (A[1], n, A[n]);

I believe passed by value would equate to the original values because they remain unchanged but the other two I have trouble understanding what happens.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
procedure practice_by_value(x,y,z : integer);
```

In pass by value - x,y,z are variables separate from A[1],n,A[2] but do get a copy of the values that were in the corresponding A[1],n,A[2]. So since they are separate variables, as you state - the global variables A[1],n,A[2] are unchanged if changes are made to x,y or z.

```
procedure practice_by_reference(var x,y,z : integer);
```

With pass by reference - x, y, z become pseudonyms for A[1], n, A[2]. Any operations on x, y, z are the same as if that operation had been performed on A[1], n, or A[2].

I am not familiar with "pass by name" in Pascal.

```
var
A : array[1.. 10] of integer;
n :integer;
procedure practice_by_value (x,y,z : integer);
begin
y := 1;
z := y-x;
end; { practice_by_value }
procedure practice_by_reference(var x,y,z : integer);
begin
y := 1;
z := y-x;
end;
begin
A[1] := 1;
A[2] := 2;
n := 2;
practice_by_value (A[1], n, A[n]);
writeln('after pass by value: ',A[1],' ',n,' ',A[2]);
practice_by_reference(A[1],n,A[n]);
writeln('after pass by reference : ',A[1],' ',n,' ',A[2]);
end.
after pass by value: 1 2 2
after pass by reference: 1 1 0
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**