Pascal - Functions and Procedures

From SwinBrain

Programming languages uses functions and procedures to group together instructions. These routines make it easier to manage the code related to your program, without them writing code would be much more difficult. Functions and Procedures are declared within Blocks in the Delphi Pascal language. For more information on blocks see the Pascal - Programs topic.

Contents

Procedure Declaration

A procedure is a named group of statements that can be called.


ref123x.png

ref124x.png

ref125x.png

A procedure declaration contains a procedure header followed by a semicolon (;), then a subroutine block, and finally another semicolon (;).

Procedure Header

The procedure header starts with the procedure keyword and is followed by an identifier (i.e. the name of the procedure). Following the identifier is the formal parameter list.


ref129x.png

ref130x.png

Most parameters in routines will be value parameters. The following syntax shows value parameters as well as variable, out, and const parameters.


ref131x.png

ref132x.png

ref133x.png

ref134x.png

Each of these parameter declarations contains an identifier list. The identifier list is described in the following syntax diagram. In each case the identifier list is used to declare the names of the parameters used within the procedure.


ref32x.png

The following examples illustrate the procedure header declaration.

procedure Main(); //procedure called Main with no parameters
 
procedure SayHello(name: String); 
//procedure called SayHello which accepts a name value parameter of type string.
 
procedure ShowMessage(message: String; times: Integer); 
//Accepts two value type parameters message which is a string, and times which is an integer. 

The procedure keyword indicates the start of the procedure declaration. This is followed by an identifier which is used to name the procedure. In the examples above Main, SayHello, and ShowMessage are the names of the procedures. Following the procedure's identifier is the formal parameter list this list starts with an open bracket (i.e. ( ) and contains a number of parameter declarations separated by semicolons (;). The Main example above has an empty parameter list, SayHello contains a single parameter declaration, while ShowMessage has two parameter declarations.

Each of the parameter declarations is either a value, variable, out, or const parameter declaration. The examples above are all value parameter declarations (the most frequently used type of parameter). For the value parameter declaration you should see an identifier list (i.e. a list of comma (,) separated identifiers) followed by a colon (:) and then the parameter type. So message is the first identifier in ShowMessage's parameter declarations. The type for this parameter is String.

The end of the formal parameter list is identified by the closing bracket (i.e. )). This is then the end of the formal parameter list, so we return to the procedure header. This is also the end of the procedure header so we return to the procedure declaration. This indicates that after the procedure header there is a semicolon (;) followed by a subroutine block. The next section looks at the subroutine block syntax.

Subroutine block

The subroutine block is a block, as covered in Pascal - Programs. The declaration part of this block usually only contains variable and constant declarations, though it can include the other declarations as well. The main declarations for a subroutine block are therefore:


ref154x.png

ref155x.png

ref157x.png

ref160x.png

ref163x.png

The declaration section is then followed by a compound statement (begin ... end) which contains the instructions for this procedure. The following are some examples of procedures in Delphi-Pascal.

procedure Main();
begin
    WriteLn('Hello World');
end;
 
procedure SayHello(name: String);
begin
    WriteLn('Hello ', name);
end;
 
procedure SillyName();
var
    name: String;
begin
    Write('Enter your name: ');
    ReadLn(name);
 
    if name == 'Fred' then
    begin
        SayHello(name);
    end
    else
    begin
        WriteLn('I dont know you...');
    end;
end;
 
procedure PrintMultiple(message: String; times: Integer);
var
    i: Integer;
begin
    for i := 1 to times do
    begin
        WriteLn(message);
    end;
end;

The PrintMultiple procedure from the example above includes a single variable declaration part. This declaration is used to declare the i Integer variable. This variable will be accessible within this procedure.

Function Declaration

A function is similar to a procedure, except that it returns a value. As a result, function calls are used within expressions, while procedure calls are a kind of statement.


ref126x.png

ref127x.png

ref128x.png

Looking at the function declaration you should see that it is very similar to the procedure declaration. The difference here is that it starts with the function keyword and that there is a result type at the end of the header before the semicolon (;). The code for the function is implemented in a block in the same way as the procedure. The following are all valid function declarations.

function ReadString(message: String): String;
var temp: String;
begin
    Write(message, ': ');
    ReadLn(temp);
    result := temp; //result is the value returned
end;
 
function CircleArea(radius: Double): Double;
var area: Double;
begin
    area := PI * radius * radius;
    result := area;
end;
 
function Highest(v1, v2, v3: Integer): Integer;
begin
    if (v1 > v2) and (v1 > v3) then
    begin
        result := v1;
    end 
    else if v2 > v3 then
    begin
        result := v2;
    end
    else
    begin
        result := v3;
    end;
end;

Calling a Procedure

You can execute a procedure using the procedure statement (as compared to the procedure declaration where you define what the procedure is).


ref104x.png

ref96x.png

To call the procedure you use the procedure's identifier (i.e. its name) followed by the actual paramter list. The actual parameter list is a list of comma separated expressions that will be passed to the parameters of the procedure being called. The following procedure statements call the procedures that were defined above.

begin
    Main();
    SayHello('Fred');
    SillyName();
    PrintMultiple('Hello World', 23 * 2 + 1 - 3);
end.

Calling a Function

Unlike procedures there is no function call statement. Rather, function calls are expressions and must be included within some other statement.


ref95x.png

ref96x.png

The following are examples of calling the functions defined above.

begin
    name := ReadString('Enter your name: ');
    //assignment statmenet, expression calls ReadString function.
 
    area := CircleArea(10.0);
    //assignment statement
 
    maxBy2 := Highest(10, 3, 18) * 2;
    //function call within expression
 
    SayHello(ReadString('Enter your name'));
    //procedure statement, function call as one of the parameter values
 
    PrintMultiple(ReadString('Enter a string to print'), Highest(15, 12, 5));
    //procedure statement, function calls to determine parameter values
 
    max := Highest(
        Highest(10, 5, 11),
        Highest(6, 12, 5),
        34);
    //two function calls to calculate the parameter values
    // the first will return 11 (Highest(10, 5, 11))
    // the second will return 12 (Highest(6, 12, 5))
    //Then there is the function call within the expression part of the 
    //assignment statement.
    //  the result will be Highest(11, 12, 34) = 34
end.

[edit]Pascal: Programs | Functions and Procedures | Statements | Data Types | Identifiers | ...
Links: Topic Brains
Return to Swin Brain