Académique Documents
Professionnel Documents
Culture Documents
h e l l 0
a[1] a[2] a[3] a[4] a[5]
6.3 Procedures
Procedures
extension of the declaration section of the program
placed after the variable declaration subsection
Advantages of using procedures
enhance top-down design
avoid repeating
increase readability
6.3 Procedures
PROGRAM <program name>;
CONST
VAR
PROCEDURE <procedure name>
CONST
VAR procedure
BEGIN
...
END;
BEGIN
... main program
END.
6.3 Procedures
Two forms of procedures
without parameters
PROCEDURE <name>;
BEGIN
...
END;
with parameters
PROCEDURE <name>(<parameter list>);
BEGIN
...
END;
© Longman Hong Kong Education Page21
Computer and Information
Technology for (HKCEE)
Module A2: Part B
6.3 Procedures
Value parameters
allow the value to be passed from the main program
to the procedure only
Variable parameters
any changes of values in the procedure produces a
corresponding change in the main program
variables are passed by reference
6.3 Procedures
Value parameters and variable parameters
6.4 Functions
Function
return a single value to the calling program
function name
y := sqrt(x);
obtain return argument
value of the
sqrt function
© Longman Hong Kong Education Page24
Computer and Information
Technology for (HKCEE)
Module A2: Part B
6.4 Functions
Standard functions
e.g. sqr, sqrt, round, trunc
argument is required
can be used in expressions
x := sqrt(y) + sqrt(z);
can be used in output statements
writeln(sqr(3) : 8);
6.4 Functions
User-defined functions
e.g. finding a volume of a cube
6.4 Functions
Recursive functions
a process of a subprogram calling itself
e.g. finding factorial of non-negative integer
FUNCTION factorial(n:integer):integer;
BEGIN
IF n=0 THEN
factorial := 1
ELSE
factorial := n * factorial(n-1)
END;
END