Sub Programming - Intro and Procedures
Sub Programming - Intro and Procedures
- Sub programming
- Use of comments
- Use of blank lines
- Use of indentations
Sub programming is as a result of the modular approach to programming. Modularity is a software design
technique that allows the development of separate, interchangeable program components called modules by
breaking down program tasks into sub programs, each of which accomplishes one task and contains everything
necessary to accomplish that task.
In programming, modules are also referred to as a subroutine, a procedure, function, routine, method, or
subprogram. In Pascal however, subprograms can be either be a procedure or a function.
Disadvantages
Invoking a subroutine (versus using in-line code) imposes some computational overhead in the call mechanism.
Procedures
A procedure is a self contained program structure that is included within a program to accomplish a
task(s) and has everything necessary to accomplish that/ those task(s). A procedure has both the header
and the block (or body). It is declared in the declaration part of the main program as the last element
after labels, constants, type definitions and variables. A procedure must be declared (defined) before
being invoked (called or referenced).
Consider following Pascal program that is used to accept and then determine the largest of three
integers.
program numbers(input,output);
var a, b, c:integer;
procedure maximum;
var max:integer;
begin
if a>b then
max:=a
else
max:=b;
if c>max then
max:=c;
writeln(’the maximum number is’, max);
end;
begin
writeln(’enter the three integers a,b and c’);
readln(a,b,c);
while a< >0 do
begin
maximum;
readln(a);
end;
end.
procedure grade; {this procedure finds and computes the grades of students}
begin
case marks of
85..100: d:=d+1;
65..84: m:=m+1;
40..64: p:=p+1;
0..39: f:=f+1;
else
writeln(’marks out of range’);
end; {end of the case construct}
end; {end of procedure grade}
Assignment:
1. Write a Pascal program that will use two procedures to give the following output:
1*
2**
3***
4****
5*****
5*****
4****
3***
2**
1*
2. Write a Pascal program that accepts two integers and an arithmetic operator. The program then
calculates and displays the results of the two integers depending on the operator. Use the CASE
selection construct. {Hint: Use three procedures namely: input, compute and display}
3. Write a Pascal program that computes the area and perimeter of a circle. The program should use two
procedures named Area and Perimeter.