User-defined Functions in Turbo Pascal

You know that Turbo has a number of built-in functions:

     Name of Function              Value returned by function
     X := RANDOM(99)               random integer 0, 1, ..., 99
     X := ROUND(6.7)                    7
     X := TRUNC(6.7)                    6
     X := SQRT(36)                      6.0

A programmer-defined function is called in exactly the same way
as any standard, built-in function.  To call a function in
Pascal, the program provides the function with one or more
arguments.  The function then returns a single value.  A function
call is a particular kind of expression and, like all other
expressions, it has a value.  That value is said to be the value
returned by the function.

A Pascal function is used differently than a procedure.  A
procedure call is a statement and, like any statement [for
example, X := 5;] it causes the computer to perform an action.  A
function call is a particular kind of expression and so returns a
value.  Although it is called somewhat differently than a
procedure, you declare a function in much the same way that a
procedure is declared.  Function declarations appear in the same
general area that procedures are found in a program.  Here's a
simple, complete program which shows a user-defined function:

Program Find_cube;

Var
  Base, Power: Integer;

Function Cube(X: Integer): Integer;

{ Returns X cubed }

Begin
  Cube := X * X * X
End;

Begin
  Write('Enter number to be cubed: ');
  Readln(Base);
  Power := Cube(Base);
  Writeln(Base, ' cubed is ', Power)
End.

Notice the features of a function declaration:

(1)  The function declaration is started with the key word
     Function (instead of procedure);

(2)  The formal parameter list appears after the function name;
     all variables in the formal parameter list are typed, just
     like in procedures; however, variable parameters are rarely
     used in a function (always pass by value).

(3)  The type of value returned by the function is declared at
     the end of the definition statement

(4)  At least once in the body of the function definition, the
     name of the function is used on the LEFT HAND SIDE of an
     assignment statement.

A more general function to compute powers might be written:

Function Power(X: Real; N: Integer): Real;

{ Returns the value of X to the power N;
  Precondition:  X <> 0 }

Var
  M, Count, Product: Integer;

Begin
  Absolute_N := Abs(N);
  Product := 1;
  Case N of
    1..Maxint:  Begin
                  For Count := 1 to N Do
                    Product := Product * X;
                  Power := Product;
                End;
    O:          Power := 1;
    Else        Begin
                  For Count := 1 to Absolute_N Do
                    Product := Product * X;
                  Power := 1 / Product
                End
    End { Case }
End; { Power }

Sometimes programmers get into trouble thinking that the name of
a function can be used like a variable.  However, it is NOT a
variable (it is the name for a process) and cannot be used like
one.  For example, it might be tempting to write one of the FOR
loops in the above example like this:

                  For Count := 1 to N Do
                    Power := Power * X;

The statement Power := Power * X; will produce an error message. 
The compiler requires that every time the name of a function is
invoked on the right-hand side of an assignment statement, it
appears with identifiers which will serve as arguments to the
function.  The compiler will likely issue an error message like
"Insufficient arguments for function" or "Number of parameters
does not agree with declaration."