[go: up one dir, main page]

0% found this document useful (0 votes)
81 views13 pages

Pascal Subprogram: Procedure Function Build in Function (E.g. Sin (X) ) Self-Defined Function (Out of Syllabus)

1) A Pascal subprogram can be a procedure or a function. Procedures perform actions without returning a value while functions return a value. 2) Procedures allow modular programming by separating code into logical units. They use parameter passing to share data between the main program and procedure. 3) Parameters can be value parameters, which pass a copy of an argument's value, or variable parameters, which pass the argument by reference so the procedure can modify the variable. Formal parameters specify the procedure signature while actual parameters are the arguments passed during a call.

Uploaded by

Amarilis Geiger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
81 views13 pages

Pascal Subprogram: Procedure Function Build in Function (E.g. Sin (X) ) Self-Defined Function (Out of Syllabus)

1) A Pascal subprogram can be a procedure or a function. Procedures perform actions without returning a value while functions return a value. 2) Procedures allow modular programming by separating code into logical units. They use parameter passing to share data between the main program and procedure. 3) Parameters can be value parameters, which pass a copy of an argument's value, or variable parameters, which pass the argument by reference so the procedure can modify the variable. Formal parameters specify the procedure signature while actual parameters are the arguments passed during a call.

Uploaded by

Amarilis Geiger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 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…

You might also like