High-level Programming Language (Pascal)
Subprograms (also called Subroutines)
a function returns
a value and can be used in expressions, like this :
ˇ@ˇ@x := sqrt (A);
while a procedure is called to perform one or more tasks :
ˇ@ˇ@writeln ('This is a
test.');
Declaration of Pascal Procedure
consist of three parts : procedure heading, local declaration part and procedure body (action part) which appear in a fixed order
|
Major
parts
|
Example
|
| Procedure heading | procedure ProcedureName (Formal_parameter_list : Data_type) ; |
| Local Declaration part | const type {not covered in HKCEE syllabus} var procedure function {not covered in HKCEE syllabus} |
| Procedure body | begin ˇ@statement_1 ; ˇ@statement_2 ; ˇ@ˇ@ˇ@... ˇ@ˇ@ˇ@... ˇ@statement_n end ; |
the structure of procedure call statement
Procedure_Name (Actual_parameter_list); or Procedure_Name ;
ˇ@procedure AreaOf Rectangle (Width, Length : real ; var Area : real);
- formal parameters Width and Length are value parameters
- which receive information passed into the procedure, the actual parameter corresponding to the value parameter can be a variable, constant or Pascal expression
- at the time of procedure call, the values of the actual parameters are passed into the value parameters, there is no further connection between the formal and the actual parameters; therefore, any changes to the formal parameters will not affect the corresponding actual parameters
- the calling is known as calling by value
- formal parameter Area is a variable parameter
- which will return the result of the procedure to the calling program, the actual parameter corresponding to the variable parameter must be a variable
- the reserved word var must be placed before the variable parameter
- the variable parameter refers to the same memory location as the actual parameter, thus any changes in the variable parameter in the called procedure represents a change in the actual parameter being passed
- the calling is known as calling by reference