| «‹ July 2026 ›» | | S | M | T | W | T | F | S | | | | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 12 | 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | 26 | 27 | 28 | 29 | 30 | 31 | |
|
2009 年 9 月 11 日 星期五  |
| Subprograms(Subroutines) |
分類: Programming Language |
High-level Programming Language (Pascal)
Subprograms (also called Subroutines)
- is a group of statements designed to carry out a specific task
- there are two types of subprograms : procedures and functions
- to perform the same set of instructions on different sets of data or at different locations in the program
- since function is not covered in the syllabus, it will not be discussed
- the main difference between the two is that
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 subprograms should be placed between the variable declaration and the program body
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 ; |
- differences between procedure and main program :
- procedure starts with a procedure header instead of a program header, and they end with a semicolon instead of a period
- procedure can have its own constants, and variables, and even its own subprograms, all these items can only be used with the procedure in which they are declared
- variables declared in the declaration part of the main program are called Global Variables and can be accessed anywhere in the program
- variables declared in the declaration part of the procedure are called Local Variables and can only be accessed within the procedure
- Calling a Procedure
- procedure can be called by a procedure call statement in a main program or a subprogram
- whenever a procedure is called, the control is transferred from the calling program to the procedure reference and after the execution of procedure reference, the control is transferred back to the calling program or a subprogram and the next program statement will be executed
- the calling of procedures
the structure of procedure call statement
Procedure_Name (Actual_parameter_list); or Procedure_Name ;

- Parameters and Procedure Calls
- values are passed into and out of procedures via the procedure’s parameters (and via non-local variables)
- the parameters which are declared in the procedure heading are called the formal parameters, the values and variables which match the formal parameters in the procedure call are the actual parameters
- the number of actual parameters must be the same as the number of formal parameters
- the order and type of actual parameters must match exactly with the formal parameters
- the value of each actual parameter is assigned to the corresponding formal parameter when the procedure is called, a correspondence between actual parameters and formal parameters is shown below :

- Formal Parameters
- can be classified as value parameter and variable parameter, for example
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
|
|