Sei sulla pagina 1di 13

Pascal Subprogram

 Procedure
 Function
- Build in Function (e.g. Sin(x))
- Self-defined Function (out of syllabus)
A Pascal Program
program Scope;
var Num1, Num2, Sum : integer;

begin
Num1 := 10;
Num2 := 15;
Sum := Num1 + Num2; What is the
writeln( 'The sum is ', Sum ) sample output ?
end.
Using Procedure
program Scope;
var Num1, Num2, Sum : integer;
What is “FindSum” ?
procedure FindSum;
begin
Sum := Num1 + Num2
end;

begin
Num1 := 10;
Num2 := 15;
FindSum;
writeln( 'The sum is ', Sum )
end.
Global Variables
program Scope; How it
var Num1, Num2, Sum : integer; works ?
procedure FindSum;
begin
Sum := Num1 + Num2
end;
begin
Num1 := 10;
Num2 := 15;
FindSum;
writeln( 'The sum is ', Sum )
end.
Local Variables
program Scope;
var Num1, Num2, Sum : integer;
procedure FindSum;
Which is the
var Sum : integer; local variable?
begin
Sum := Num1 + Num2
end;
begin
Num1 := 10;
Num2 := 15;
FindSum;
writeln( 'The sum is ', Sum )
end.
Parameter Passing
program Scope;
var Num1, Num2, Sum : integer;
procedure FindSum( A, B:integer;
var C:integer);
begin
C := A + B
end;
begin
Num1 := 10;
Num2 := 15;
FindSum( Num1,Num2,Sum );
writeln( 'The sum is ', Sum )
end.
Formal & Actual Parameters
program Scope;
var Num1, Num2, Sum : integer;
procedure FindSum( A, B:integer; How the
var C:integer); data flows
begin in and
C := A + B flows out?
end;
begin
Num1 := 10;
Num2 := 15;
FindSum( Num1,Num2,Sum );
writeln( 'The sum is ', Sum )
end.
Formal & Actual Parameters
program Scope;
var Num1, Num2, Sum : integer;
procedure FindSum( A, B:integer;
var C:integer); Formal
begin vs. actual
C := A + B paramete
end; r
begin
Num1 := 10;
Num2 := 15;
FindSum( Num1,Num2,Sum );
writeln( 'The sum is ', Sum )
end.
Formal & Actual Parameters If I
program Scope; forget
var Num1, Num2, Sum : integer; to type
var…
procedure FindSum( A, B:integer;
var C:integer);
begin
C := A + B
end;
begin
Num1 := 10;
Num2 := 15;
FindSum( Num1,Num2,Sum );
writeln( 'The sum is ', Sum )
end.
Value & Variable Parameters
program Scope;
var Num1, Num2, Sum : integer;
Value vs
procedure FindSum( A, B:integer; Variable
var C:integer); paramete
begin r
C := A + B
end;
begin
Num1 := 10;
Num2 := 15;
FindSum( Num1,Num2,Sum );
writeln( 'The sum is ', Sum )
end.
General format of a procedure

procedure heading procedure < name of procedure >


(formal parameter list:data type) ;

local declarations const < constant definitions > ;


part type < type definitions > ;
var < variable declarations > ;

statement part begin


< statements >;
end;
Beware 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.
Further Reading and Exercises
 Further reading on web at home
 http://www.courseware.ust.hk/english/pascal_main/pascalfunction.html

 Homework
 Text book p.145, exercise 1-4

 Think more, ask more, practice more,..


 You would become an expert…

Potrebbero piacerti anche