Monday, October 26, 2009

8. Procedures in Pascal

We can look at the Pascal procedures as extensions to pascal functions except we do not assign value to the function name. Another difference is if we define multiple parameters in the procedure, each should be ended by a semicolon.
Here is a program which includes a procedure for converting minutes into hours and minutes. I introduced two new statements for crt, textbackground(white) and textcolor(red). The program is self explanatory.

Program program8;
(*Author: Rao S Lakkaraju*)
{ My pascal program 8 }

uses crt,graph;
var minutes : integer;

(* Procedure for converting minutes into hour and minutes *)

procedure time(minutes : integer);
var hr,min : integer;

begin
hr := minutes div 60;
min := minutes - hr * 60;
writeln;
textbackground(white);
textcolor(red);
write(minutes,' minutes equal to ', hr:2,' hours and ');
write(min:2, ' minutes');
writeln;
end;


{ Main Program Begins here }
begin
clrscr;
writeln(' My eigth pascal program ');
writeln('Please enter minutes ');
readln(minutes);

(*Call to the Procedure*)
time(minutes);

readln;
end.

Summary: If multiple statements are repeatedly used in a program, they could be put together to form a named procedure and be called by simply the procedure name wherever the statements are needed. Calling the procedure is simple, procedure name with parameters in parenthesis.

No comments:

Post a Comment