Pascal Subprogram - PowerPoint PPT Presentation

About This Presentation
Title:

Pascal Subprogram

Description:

Title: Pascal Subprogram Subject: CE Computer Studies Author: Kwok Wing Keung Last modified by: S005 Created Date: 7/27/1997 12:58:14 PM Document presentation format – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 14
Provided by: Kwok9
Category:

less

Transcript and Presenter's Notes

Title: Pascal Subprogram


1
Pascal Subprogram
  • Procedure
  • Function
  • Build in Function (e.g. Sin(x))
  • Self-defined Function (out of syllabus)

2
A 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 ?
3
Using 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

4
Global 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

5
Local 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

6
Parameter 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

7
Formal 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

8
Formal 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
9
Formal 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

10
Value 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

11
General 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
12
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.

13
Further 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
Write a Comment
User Comments (0)
About PowerShow.com