Details on Procedures and Functions

 

Local Variables

To perform the assignment of a procedure, you can declare and use local variables that are exclusively part of the procedure. Such variables are declared using the var keyword and a declaration section on top of, or before, the begin keyword. Here is an example:

Procedure StopHere;
var GoodBye : string;
begin

end;

After declaring the variable(s), you can use it (them) as you see fit, in the body of the procedure or function. Here are two examples:

program Project1;

{$APPTYPE CONSOLE}

Procedure StopHere;
var GoodBye : string;
begin
	GoodBye := 'Press any key to continue...';
	write(GoodBye);
	readln;
end;

begin
	StopHere
end.

Like a procedure, a function can use local variables, declared only for its own use. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

Procedure StopHere;
var GoodBye : string;
begin
	GoodBye := 'Press any key to continue...';
	write(GoodBye);
	readln;
end;
Function AddTwoNumbers : Integer;
var
	Number1 : Integer;
	Number2 : Integer;
begin
	Number1 := 1250;
	Number2 := 48;
	Result := Number1 + Number2;
end;

begin
	Writeln(AddTwoNumbers);
	StopHere
end.


Forward Implementation

In most programming languages, including Object Pascal, before calling a routine, it must already exist one way or another. This means that it must either be built in the language or you must first create it. To provide this functionality, you must define a routine before calling it. In the following example, notice that the GetNumber function is implemented before the AddTwoNumbers function before the later needs it:

program Project1;

{$APPTYPE CONSOLE}

function GetNumber : Double;
var Nbr : Double;
begin
	Write('Enter Number: ');
	Readln(Nbr);

	Result := Nbr;
end;

Function AddTwoNumbers : Double;
var
	Number1 : Double;
	Number2 : Double;
begin
	Number1 := GetNumber;
	Number2 := GetNumber;
	Result := Number1 + Number2;
end;

var
	Number: Double;
begin
	Number := AddTwoNumbers;

	Writeln;
	Writeln('Number: ', Number:6:2);

	Writeln;
	Write('Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Enter Number: 125.55
Enter Number: 48.62

Number: 174.17

Press any key to continue...

If you call a routine before it has been implemented, you would receive an error. For example, the following will not work:

program Project1;

{$APPTYPE CONSOLE}

Function AddTwoNumbers : Double;
var
	Number1 : Double;
	Number2 : Double;
begin
	Number1 := GetNumber;
	Number2 := GetNumber;
	Result := Number1 + Number2;
end;

function GetNumber : Double;
var Nbr : Double;
begin
	Write('Enter Number: ');
	Readln(Nbr);

	Result := Nbr;
end;

var
Number: Double;
begin
	Number := AddTwoNumbers;

	Writeln;
	Writeln('Number: ', Number:6:2);

	Writeln;
	Write('Press any key to continue...');
	Readln;
end.

In Delphi, you would receive the following error message:



Object Pascal allows to declare a routine once and implement it later. With this, you can place the structure of the routine before another routine that would call it, making the calling routine aware of the called routine. You must still remember to implement the called routine somewhere in the project. To declare a routine without yet implement it, type the forward keyword at the end of its declaration. This technique allows you to list routines in any order of your choice. You do not have to declare each routine as long as you define it before it is called. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

Procedure StopHere; forward;
function GetNumber : Double; forward;
procedure Welcome; forward;

Function AddTwoNumbers : Double;
var
	Number1 : Double;
	Number2 : Double;
begin
	Number1 := GetNumber;
	Number2 := GetNumber;
	Result := Number1 + Number2;
end;

function GetNumber : Double;
var Nbr : Double;
	begin
	Write('Enter Number: ');
	Readln(Nbr);

	Result := Nbr;
end;

procedure Welcome;
begin
	Writeln('Welcome to our Object Pascal lessons');
end;

Procedure StopHere;
var GoodBye : string;
begin
	Writeln;
	GoodBye := 'Press any key to continue...';
	write(GoodBye);
	readln;
end;

var
	Number: Double;
begin
	Welcome;
	Number := AddTwoNumbers;

	Writeln;
	Writeln('Number: ', Number:6:2);

	StopHere;
end.

This would produce:

Welcome to our Object Pascal lessons
Enter Number: 248.64
Enter Number: 3502.48

Number: 3751.12

Press any key to continue...

Previous Copyright © 2004 FunctionX Next