1. Exam Points
Call given procedures:
- procName (arg1, arg2, …)
Pass arguments to parameters: procName(arg1, arg2, …)Return a value from a procedure: result ← procName(arguments)Note:The code segment after a return statement is unreachable.Predict output/resultof executing procedures.
2. Knowledge Points
(1) Understand Procedures
- A
procedureisa named group of programming instructionsthat may have parameters and return values. - Example: develop a procedure and call the procedure
Input and outputin CSP:- Input: procedure
INPUT()- x ← INPUT()
- Output: procedure
DISPLAY(): display a value,followed by a space- DISPLAY(”a”)
- DISPLAY(10)
- Input: procedure
(2) Develop and Call Procedures
Proceduresare referred to by different names, such asmethodorfunction, depending on the programming language.- Syntax:
- Example 1:
a procedure with parameters
-
Parameters(形式参数 - count) : input variables of a procedure.Arguments(实际参数 - 5): actual values of the parameters when a procedure is called.
-
- Example 2: a procedure with a returned value
-
Note:- The RETURN statement may appear at any point inside the procedure and causes an
immediate returnfrom the procedure back to the calling statement. - If return(a) is executed, then the statements after return(a) in the procedure will not be executed.
- The code segment after a return statement is unreachable.
- The RETURN statement may appear at any point inside the procedure and causes an
Purpose of the return statement:- Return a value
- Return the flow of control back
-
(3) Note
- A procedure call
interrupts the sequential executionof statements, causing the program to execute the statements within the procedure before continuing. - Once the
last statement in the procedure (or a return statement) has executed,flow of control is returned tothe point immediatelywhere the procedure was called. - Example: b is displayed only after the last statement in proc2() is executed.