Return to Examples

Pascal Programs:
Pass-By-Value:

program Pass(input, output);
  var c, d : integer;

procedure swap(a, b: integer);
   var temp : integer;
   begin{swap}
      temp := a;

  writeln('Beginning of subprogram:');
  writeln('a = ' , a );
  writeln('b = ' , b );
      a := b;
      b := temp;
  writeln('Ending of subprogram:');
  writeln('a = ' , a );
  writeln('b = ' , b );
   end; {swap}

begin{program}
  c := 1;
  d := 0;

  writeln('Before call:');
  writeln('c = ' , c );
  writeln('d = ' , d );
  swap(c, d);
  writeln('After call:');
  writeln('c = ' , c );
  writeln('d = ' , d );
end. {program}
The output will be
Before call:
c =          1
d =          0
Beginning of subprogram:
a =          1
b =          0
Ending of subprogram:
a =          0
b =          1
After call:
c =          1
d =          0
Pass-By-Reference:
program Pass(input, output);
  var c, d : integer;

procedure swap(var a, b: integer);
   var temp : integer;
   begin{swap}
      temp := a;

  writeln('Beginning of subprogram:');
  writeln('a = ' , a );
  writeln('b = ' , b );
      a := b;
      b := temp;
  writeln('Ending of subprogram:');
  writeln('a = ' , a );
  writeln('b = ' , b );
   end; {swap}

begin{program}
  c := 1;
  d := 0;

  writeln('Before call:');
  writeln('c = ' , c );
  writeln('d = ' , d );
  swap(c, d);
  writeln('After call:');
  writeln('c = ' , c );
  writeln('d = ' , d );
end. {program}
The output will be
Before call:
c =          1
d =          0
Beginning of subprogram:
a =          1
b =          0
Ending of subprogram:
a =          0
b =          1
After call:
c =          0
d =          1
Pass-By-Reference with List:
program Pass(input, output);
  type SmallArray = array[1..5] of integer;
  var list : SmallArray;
      i, j : integer;

procedure swap(var a, b: integer);
   var temp : integer;
   begin{swap}
      temp := a;

  writeln('Beginning of subprogram:');
  writeln('a = ' , a );
  writeln('b = ' , b );
      a := b;
      b := temp;
  writeln('Ending of subprogram:');
  writeln('a = ' , a );
  writeln('b = ' , b );
   end; {swap}

begin{program}
  i := 3;
  for j := 1 to 4 do
      list[j] := j + 1;
  list[5] := 0;

  writeln('Before call:');
  writeln('i = ' , i );
  writeln('list[i] = ' , list[i]);
  writeln(list[1], list[2], list[3], list[4], list[5]);
  swap(i, list[i]);
  writeln('After call:');
  writeln('i = ' , i );
  writeln('list[i] = ' , list[i]);
  writeln(list[1], list[2], list[3], list[4], list[5]);
end. {program}
Before call:
i =          3
list[i] =          4
         2         3         4         5         0
Beginning of subprogram:
a =          3
b =          4
Ending of subprogram:
a =          4
b =          3
After call:
i =          4
list[i] =          5
         2         3         3         5         0

The address of list[i] is computed at the time of the call and does not change after that.
The parameter of b in subprogram was referenced to the box of list[3]
at the time when the main program called the subprogram.
After that, the reference to which box is not changed.
Therefore, the element in list[3] is changed to the new value of b.