PASCAL PROGRAMMING
PROCEDURE IN PASCAL
While coding, the need to reuse a part of the code might arise. It is not a good practice to copy this part again within the program. Instead, PASCAL has components, that is subprograms called procedures and functions for this purpose. These two components are make the program easier to write, debug and enhances readability.
A procedure
is a block of code which only runs when it is called. You can pass data, known
as parameters, into a procedure. Procedures are used to perform certain actions.
Why use procedures? To reuse code: define the code once, and use it many times.
Procedures are subprograms that can return a single value or a group of
results. Procedures have a set of parameters, value or variable. Value
parameters are those which are only passed to the procedure, because otherwise
the procedure would not be able to do the task it is said to. Value parameters
do not change after the execution of procedures. They are given in the
parentheses which comes after the procedure declaration. Variable parameters
are those which may affect the execution of the procedure and need to be
updated according to this procedure. They are also given in the parentheses
after the procedure declaration, however they have a 'var' keyword before them
to indicate that they are variable parameters.
Syntax to declare a procedure in pascal:
procedure procedureName(arg(s) : datatype1, arg(s) :
datatype2 …); begin //statements end; |
TIP: Start with the keyword procedure
Program exercises implementing PROCEDURE in Pascal programming
Lab1
//Pascal program to display your details using
procedure program displayDetailsLabEx; var name1, name2 : string; procedure displayDetails( var yourName : string;
fullname : string); begin yourName := fullname; //just like we assign a value to result
variable end; begin //pass values directly displayDetails(name1, 'dmactutor'); displayDetails(name2,
'www.whitepaceblog.blogspot.com'); writeln('Your name is:: ', name1); writeln('Your blog site is:: ', name2); end. |
Lab2
//Pascal program to calculate area of rectangle
using procedure program areaRectangleUsingProcedure; var answer, len, wid :real; procedure calculateAreaRec( var area : real; length
: real; width : real); begin area := length * width; end; begin //ask users for input writeln('Insert Length in cm''s'); readln(len); writeln('Insert Width in cm''s'); readln(wid); calculateAreaRec(answer, len, wid); writeln('The area of rectangle is:: ', answer:0:2); end. |
TIP: Here,
the procedure has three parameters, a variable and two values. area is the
variable parameter, length and width are the value parameters. In the procedure
call, the value of answer is passed to area, although it does not have any
significant initial value, and the value of len is passed to length, while wid
is passed to width, that is the input from the user. In the procedure, area
gets the value length * width. Then, after the procedure has finished execution,
the new value of area is passed to answer. Now, answer stores the calculated
value and can be output with precision 2.
TIP2: If you separate the parameters with commas and decide to use one datatype, it generate error, saying 'variable identifier expected'
Lab3
//Pascal program to calculate area of rectangle
using procedure program areaRectangleUsingProcedure; var a1, a2, a3 : real; procedure calculateAreaRec( var area : real; length
: real; width : real); begin area := length * width; end; begin //pass values directly calculateAreaRec(a1, 5, 3); calculateAreaRec(a2, 2.5, 8.13); calculateAreaRec(a3, 12.9, 10.5); writeln('The first area of rectangle is:: ',
a1:0:2); writeln('The second area of rectangle is:: ',
a2:0:2); writeln('The third area of rectangle is:: ',
a3:0:2); end. |
Lab4
program lab4; var n1, n2,
n3, min: integer; procedure findMin(p, q, r: integer; var ans:
integer); //Finds the minimum of the 3 values begin if p < q
then ans:= p else ans:= q; if r <
ans then ans:= r; end; begin writeln(' Insert
three numbers: '); readln(n1,
n2, n3); //call the
procedure findMin(n1,
n2, n3, min); writeln('
Minimum: ', min); end. |
OUPTUT Insert three numbers: 45 20 199 Minimum: 20 |
FUNCTION IN PASCAL
Functions are
similar to procedures, except that they return one value with a specified type
and all its parameters should be input parameters, that is value parameters.
That is only one value should be able to be changed.
Syntax to declare a function in Pascal:
function functionName(arg(s) : datatype1, arg(s) :
datatype2 …) : datatype; begin //statements end; |
TIP: Start with keyword function
Program exercises implementing FUNCTION in Pascal programming
Lab5
//Pascal program to display your details using
function program displayDetailsLabEx; var fname : string; function displayDetails(fullname : string) : string; begin displayDetails := fullname; end;
//main begin
fname := displayDetails('dmactutor'); writeln('Your Youtube handle is:: ', fname); end. |
Lab6
//Pascal program to calculate area of rectangle
using function program areaofRectLabEx; var answer, len, wid : real; function areaRect(length, width:real) : real; begin areaRect := length * width; end; //main begin writeln('Insert length in cm''s '); readln(len); writeln('Insert width in cm''s '); readln(wid); answer := areaRect(len,wid); writeln('The area of Rectangle is:: ', answer:0:2); end. |
Comments
Post a Comment