Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))

Similar presentations


Presentation on theme: "Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))"— Presentation transcript:

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
Sum := Num1 + Num2; writeln( 'The sum is ', Sum ) end. What is the sample output ?

3 Using Procedure procedure FindSum; 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 procedure FindSum; begin Sum := Num1 + Num2 end;
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 procedure FindSum; 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 procedure FindSum( A, B:integer; 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, B:integer; var C:integer); 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, B:integer; var C:integer); 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, B:integer; var C:integer); 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, B:integer; var C:integer); 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, B:integer; var C:integer); begin C := A + B end;

11 General format of a procedure
procedure heading procedure < name of procedure > (formal parameter list:data type) ; local declarations part const < constant definitions > ; type < type definitions > ; var < variable declarations > ; statement part begin < statements >; 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 Homework Text book p.145, exercise 1-4 Think more, ask more, practice more,.. You would become an expert…


Download ppt "Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))"

Similar presentations


Ads by Google