Procedure and function

advertisement
S.K.H. Kei Hau Secondary School AL CS 2003-2004
Modular Programming
S.K.H. Kei Hau Secondary School
Department of Computer
AL Computer Studies
Modular Programming
Content
1. Introduction to parameter lists
2. Value parameter, variable parameter
3. Functions
4. Recursive functions
1. Introduction to parameter lists
Each procedure call statement has two parts: a procedure name and an actual
parameter list:
Actual parameter list
Exponent(X, Y, var Z)
Procedure
name
When you design the procedure, however, you may have to decide the formal
parameter lists:
Formal
parameter list
Procedure Exponent(Xinput, Yinput, Var Zoutput)
The location of the procedure should be somewhere before the begin…end of the
main program.
Program main;
Var X, Y, Z: integer;
Procedure Exponent(Xinput, Yinput, var Zoutput)
Begin
Zoutput := Exp(Xinput * Ln(Yinput))
End;
Begin
Writeln(‘Enter 2 values’);
2016/2/5
1/4
S.K.H. Kei Hau Secondary School AL CS 2003-2004
Modular Programming
Readln(X, Y);
Exponent(X, Y, Z);
Writeln(‘The result is ’, Z)
End.
Note that Xinput, Yinput and Zoutput are local variables (declare in local procedure),
while X, Y and Z are global variables (declare in main program).
The number and the type of the actual parameters and formal parameters should be
matched. (integer, real, Boolean, etc)
2. Value parameter, variable parameter
A value parameter receives information passed into a procedure, and a variable
parameter returns a procedure result by changing its corresponding actual parameter
value.
Procedure Summation (X, Y : real; var Z: real);
Note that when the main program calls the procedure, the actual parameters are
needed. The value of the corresponding actual parameter to the variable parameter
will be updated if the local variable is updated.
Program main;
Var X, Y, Z: integer;
Procedure summation(Xinput, Yinput, var Zoutput)
Begin
Zoutput := summation(Xinput + Yinput)
End;
Begin
Writeln(‘Enter 2 values’);
Readln(X, Y);
Exponent(X, Y, Z);
Writeln(‘The result is ’, Z)
Local, but variable
parameter
Z, global variable, is updated, since
the corresponding parameter is a
variable parameter
End.
3. Functions
Functions are independent modules like procedures, but it only returns single result.
Consider, n := Abs(-1), n will be 1 since abs is a predefined function of Pascal.
2016/2/5
2/4
S.K.H. Kei Hau Secondary School AL CS 2003-2004
Modular Programming
User defined function
Function Exponent (X, Y: Real) : Real;
Type of the
result.
Eample program
Program main;
Var X, Y, Z: integer;
Function Exponent(Xinput, Yinput) : real;
Begin
If X = 0.0 then
Exponent := 0.0
Else if Xinput >0.0 then
Exponent := Exp(Xinput * Ln(Yinput))
Else
Writeln(‘Error of first parameter’)
End;
Begin
Writeln(‘Enter 2 values’);
Readln(X, Y);
Z := Exponent(X, Y);
Writeln(‘The result is ’, Z)
End.
Exercise
Write a Pascal program function FindAverage to allow a user to input the number of
items to be added and find the average of the entered items. You should check the
entered value is a non-zero value.
4. Recursive function
A function that calls itself is a recursive function.
Example, factorial of N
N! = 1
for N = 0 or 1
N! = N x (N-1)!
for N >1
If we design a function Factorial, the statement of the repeating operation is:
2016/2/5
3/4
S.K.H. Kei Hau Secondary School AL CS 2003-2004
Modular Programming
Factorial := Factorial(N-1);
Consider:
Function Factorial (N : integer) : integer;
Being
If N <=1 then
Factorial := 1
Else
Factorial := N * Factorial(N-1)
End.
The function call is a recursive call. Consider N = 3,
Factorial(3) = 3 * Factorial(2) = 3 * (2 Factorial(1))
Since Factorial (1) returns 1, so the chain stops. We call Factorial(1) is the stopping
case of the function.
Note that if the recursive function operates continuously, it may cause stack overflow.
Reference
Elliot B. Koffman, PASCAL 5th Edition (1994), Addison-Wesley
2016/2/5
4/4
Download