next up previous contents
Next: What is a Function? Up: Subroutines Previous: Nested Invocation   Contents


Functions

Procedures are handy and flexible, but they are also clumsy to use in some cases. This is because a procedure has only one method of returning information to the caller: passing parameters by reference. While this method is flexible (because a procedure can use multiple parameters passed by reference to return multiple pieces of information), it is clumsy when there is only one result to return.

Consider the procedure that computes the difference two numbers:

procedure difference(num1, num2 : integer; var diff : integer);
  begin
    diff := num1 - num2
  end;

This works, but it also requires the caller to pass a variable to formal parameter diff. For example, just to print the difference of two numbers, we need the following code:

difference(myvar1, myvar2, temp);
writeln('the difference of ',myvar1,' and ',myvar2',' is ',temp);

Does it work? Yes. But this is clumsy.



Subsections

Tak Auyeung 2003-12-03