Parameters

In the previous example we had a procedure which drew a line of 20 dashes :

procedure DrawLine;
var i : integer;
begin  
  for i := 1 to 20 do output('-');
  output(new line);
end;  

..which is OK but what if sometimes we wanted lines with 10 dashes and sometimes lines with 40 dashes ...and so on...

The way to do this is to pass a parameter to the procedure...

procedure DrawLine( NumDashes : integer);
var i : integer;
begin  
  for i := 1 to NumDashes do write('-');
  writeln;
end;  

and when we call the procedure we put in brackets the value of the parameter we would like to pass....

  DrawLine(40);

for a line of 40 dashes or

  DrawLine(10);

for a line of 10 dashes.

Suddenly our procedure DrawLine is much more useful!

Another important point about subroutines....

Subroutines should be designed to be as 'self-contained' as possible. This means that they should not rely on variables or results from other subroutines - and they do not use variables declared globally if it can be avoided. The interface of a subroutine should only be through its parameters.

A well-designed subroutine should be capable of being used in programs other than the one for which it was originally designed.