Functions and procedures
Functions are subprograms that return a value. Procedures are subprograms that do not return the value.
The following sample illustrates declaration of two procedures; one of them takes no arguments, and another procedure takes 2 arguments:
program procedureSample;
var n: integer; { this variable is visible in main
program block and in all functions
and procedures }
procedure noArgs;
begin
n := 5;
end;
procedure twoArgs(a: integer; b: string);
var len: integer; { this variable can be accessed only
from within the procedure and is
reinitialized every time that the
procedure is called }
begin
len := length(b);
n := a + len;
end;
begin
noArgs; { call the first procedure }
twoArgs(n, 'Some string'); { call the second procedure }
end.
Functions are similar to procedures, except that they can return values. The following sample illustrates the use of the functions in MIDletPascal:
program functionSample;
var result: integer;
{ this simple function always returns 5 }
function returnFive: integer;
begin
returnFive := 5;
end;
function multiply(a, b: integer): integer;
begin
multiply := a * b;
end;
begin
result := multiply(2, returnFive); { call the multiply function
where on argument is value
returned by calling the
other function }
end.
Recursion is also allowed:
program recursionSample;
var factorielOfFive: integer;
function factoriel(n: integer): integer;
begin
if n = 1 then
factoriel := 1;
else
factoriel := n * factoriel(n-1);
end;
begin
factorielOfFive := factoriel(5);
end.
Differences from standard Pascal language:
MIDletPascal supports forward references. Consider the following program:
procedure a (x: integer);
begin
...
b(x);
...
end;
procedure b(y: integer);
begin
...
a(y);
...
end;
begin
a(5);
end.
Procedure a calls procedure b, and procedure b calls procedure a. When procedure b call procedure a, everything is OK, because the procedure a appears before procedure b. But compiler will report an error in procedure a, because it calls procedure that has not been declared yet. To solve this problem, use the forward reference:
procedure b(y:integer); forward; { the forward reference tells
compiler that procedure 'b' will
be defined somewhere in the program code }
procedure a (x: integer);
begin
...
b(x);
...
end;
procedure b(y: integer);
begin
...
a(y);
...
end;
begin
a(5);
end.