Thursday, January 26, 2017

Lecture 10 - Pascal Programming (Part IV)



Procedures and Functions

Procedures - Are a type of subroutine which can be used in a Pascal program. These subroutines can accept parameters but cannot directly return values to the calling program. Therefore procedure call statement should always be given as a separate instruction.

Functions - Are the other type of subroutine that can be used in Pascal program. These can directly return a values. Therefore, the function call can be part of another instruction.

Parameter Passing
When a subroutine is called the data items passed from the calling module are known as parameters. These parameters can serve number of different purposed as given below.

  • Input Parameter - Carry a value from the calling module to the subroutine.
  • Output Parameter - Does not provide an input to the subroutine but take a result back
    to the caller.
  • Input / Output Parameter - Carry a value into the subroutine which is altered there and returned.

Pascal provides two different type of Parameter passing techniques.

Call by Value ( Value Parameters )
Here the formal parameters received by the subroutine are copies of actual parameters. Therefore when these are changed in the subroutine actual parameters are not changed. This technique is used if the parameter is having an input role.

FUNCTION Average( X, Y : Integer ) : Real;






Call by Reference ( Variable Parameters )
Here the formal parameters received by the subroutine are references to actual parameters. Therefore when these are changed in the subroutine actual parameters are also changed. This technique is used if the parameter is having an output role.

PROCEDURE Swap(VAR X, Y : Integer );


Calculate the Average of two number using a function
PROGRAM Example6;
VAR
X , Y: integer;
Avg: Real;

FUNCTION Average( A, B : integer ) : Real;
{ Uses passing by value as the parameters are input)

VAR
Total : integer;
BEGIN
Total :=A+B;
Average := Total I 2; { Return value is assigned to function name}
END;

BEGIN
Write('Enter two integers : ');
Readln( X,Y ),
Avg := Average( X, Y )' { Calling the function }
Writeln('Average =',Avg:0:2);
END.


Using a Procedure Swapping ( interchanging ) two integer values entered through keyboard.
PROGRAM Example5;
VAR
X , Y: Integer;

PROCEDURE Swap(VAR A, B : Integer );
{ Uses passing by reference as the parameters are input/output I
VAR
Hold : integer;
BEGIN
Hold := A;
A:= B;
B := hold;
END;

BEGIN
Write('Enter two integers : ');
Readln( X, Y ),
Swap ( X, Y ); { Calling the procedure}
Writeln('After Swapping');
Writeln('X = ',X);
Writeln('Y = ',Y);
END.


Recursion

Recursion is technique that allows you to call a subroutine by the same subroutine itself. When a subroutine is called by it self it will iterate number of times. Therefore recursion is an alternative technique for repetitions such as while, for and repeat / until loops.

Recursion is handled by using a stack therefore the section above the recursive call is run as the routine is called and the section below the recursive call is run as the subroutine calls are returned. Due this the latter segment is executed in last in first out basis.

Following Program uses recursive function for calculating the factorial.

PROGRAM Example6;
VAR
Num : Integer;

FUNCTION Factorial( Num : integer ) : integer;
BEGIN

IF Num=0 THEN
Factorial := 1
ELSE
Factorial .= Num * Factorial( Num - 1 );
END;

BEGIN
Write('Enter positive integer : ');
Readln( Num );
Writeln('Factorial of ',Num,' is',Factorial(Num));
END.


Consider the following program which uses a recursive procedure

PROGRAM Example7;
VAR
Num, Low, High . integer;
Test: Boolean;

PROCEDURE Testrange( N, R1, R2 : integer; VAR inrg : Boolean );
BEGIN

IF Rl=N THEN
inrg := True
ELSE
IF R1 > R2 THEN
ELSE inrg := False
BEGIN
P1 := Succ(R1);
Testrange( N, P1, R2, inrg );
END;
END;

BEGIN
Readln( Num, Low, High );
Testrange( Num, Low, High, Test );
IF Test = True THEN
Writeln(Num, 'Lies inside the range')
ELSE
Writeln(Num, 'Lies outside the range');
END.


No comments:

Post a Comment

Important Notice!

Dear students and friends. When you commenting please do not mention your email address. Because your email address will be publicly available and visible to all. Soon, it will start sending tons of spams because email crawlers can extract your email from feed text.

To contact me directly regarding any inquiry you may send an email to info@bcslectures.website and I will reply accordingly.