Title: Pascal Subprogram
1Pascal Subprogram
- Procedure
- Function
- Build in Function (e.g. Sin(x))
- Self-defined Function (out of syllabus)
2A Pascal Program
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15 Sum Num1
Num2 writeln( 'The sum is ', Sum ) end.
What is the sample output ?
3Using Procedure
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum writeln( 'The sum is ', Sum ) end.
What is FindSum ?
- procedure FindSum
- begin
- Sum Num1 Num2
- end
4Global Variables
How it works ?
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum writeln( 'The sum is ', Sum ) end.
- procedure FindSum
- begin
- Sum Num1 Num2
- end
5Local Variables
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum writeln( 'The sum is ', Sum ) end.
Which is the local variable?
- procedure FindSum
- var Sum integer
- begin
- Sum Num1 Num2
- end
6Parameter Passing
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum( Num1,Num2,Sum ) writeln( 'The sum is
', Sum ) end.
- procedure FindSum( A, Binteger
- var Cinteger)
- begin
- C A B
- end
7Formal Actual Parameters
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum( Num1,Num2,Sum ) writeln( 'The sum is
', Sum ) end.
How the data flows in and flows out?
- procedure FindSum( A, Binteger
- var Cinteger)
- begin
- C A B
- end
8Formal Actual Parameters
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum( Num1,Num2,Sum ) writeln( 'The sum is
', Sum ) end.
- procedure FindSum( A, Binteger
- var Cinteger)
- begin
- C A B
- end
Formal vs. actual parameter
9Formal Actual Parameters
If I forget to type var
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum( Num1,Num2,Sum ) writeln( 'The sum is
', Sum ) end.
- procedure FindSum( A, Binteger
- var Cinteger)
- begin
- C A B
- end
10Value Variable Parameters
program Scope var Num1, Num2, Sum integer
begin Num1 10 Num2 15
FindSum( Num1,Num2,Sum ) writeln( 'The sum is
', Sum ) end.
Value vs Variable parameter
- procedure FindSum( A, Binteger
- var Cinteger)
- begin
- C A B
- end
11General format of a procedure
procedure lt name of procedure gt (formal
parameter listdata type)
procedure heading
const lt constant definitions gt type lt type
definitions gt var lt variable declarations gt
local declarations part
statement part
begin lt statements gt end
12Beware of .
- check that a procedure must be declared and
placed in the proper position of a program - remember that a procedure must be ended with the
reserved word END and a semicolon. - match the type and order of the actual parameters
to the corresponding formal parameters - make sure that an actual parameter is a variable
when corresponding to a variable parameter.
13Further Reading and Exercises
- Further reading on web at home
- http//www.courseware.ust.hk/english/pascal_main/p
ascalfunction.html - Homework
- Text book p.145, exercise 1-4
- Think more, ask more, practice more,..
- You would become an expert