Procedures and Functions

In a procedure a sequence of statements is given a name so that it can be called from other parts of the program and used as many times as necessary. A function is also a named sequence of statements but it returns a value. This means that it can usually be used by name as if it were a variable. However, unlike a variable, a function cannot be used to the left of the assignment operator (:=) because values cannot be assigned to it.

Programs are generally easier to understand and modify when they use procedures and functions. The main section of the program (between begin and end.) becomes short and calls procedures by meaningful names. The first example shows how to write a simple procedure then call it.

program Welcome;
{$APPTYPE CONSOLE}
uses
  SysUtils;

procedure Hello;
var
  Forename : string;
begin
  write('What is your first name? ');
  readln(Forename);
  writeln('Hello, ', Forename);
end;  

begin
  Hello;
  readln;
end.

Continue with the following topics to learn more about this vital area of programming:

Programming - a skill for life!

Pascal Programming Tutorials