0% found this document useful (0 votes)
15 views

Sub Programming - Intro and Procedures

Sub programming breaks down programs into modular components called subprograms or modules. This allows complex tasks to be decomposed into simpler steps, improves code reuse and

Uploaded by

Amon Barno
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Sub Programming - Intro and Procedures

Sub programming breaks down programs into modular components called subprograms or modules. This allows complex tasks to be decomposed into simpler steps, improves code reuse and

Uploaded by

Amon Barno
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sub Programming

Some of the techniques used in programming to improve on program readability include:

- 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.

Advantages of using Sub programs

The advantages of breaking down a program into subroutines include:

 Allows the decomposing of a complex task into simpler steps


 Reduces duplicate code within a program
 Enables reuse of code across multiple programs
 Dividing a large programming task among various programmers, or various stages of a project,
this saves on development time
 Improves on program understanding, coding, traceability, debugging and testing
 An error in a particular sub program does not necessarily affect the functioning of the entire
program, and can be corrected independently

Disadvantages

Invoking a subroutine (versus using in-line code) imposes some computational overhead in the call mechanism.

Pascal Procedures and Functions

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).

Procedure declaration (or definition)


The general form of defining a procedure header is as follows:

Procedure procedure name (optional list of formal parameters);

e.g procedure average(a,b,c:integer);


procedure average;

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.

From the above program, it’s important to note that:

When a procedure is called/ referenced by a procedure calling/ referencing statement, CONTROL is


automatically transferred to the beginning of the procedure (procedure header).
The actual statements inside the procedure are then executed taking into account any special
declarations that are unique/ local to the procedure.
When all actual statements within the procedure have been executed, CONTROL is transferred to the
next statement immediately after the procedure calling statement.

Consider the following Pascal program:


program performance(input,output);
{this program determines students grades}
var d,m,p,f,c,marks:integer;
ans:char;
procedure initialize; {this procedure initializes counters}
begin
d:=0;
m:=0;
p:=0;
f:=0;
end; {end of procedure initialize}

procedure inputmarks; {this procedure is used to input marks}


begin
writeln(‘please enter the marks of the student’);
readln(marks);
end; {end of procedure inputmarks}

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}

procedure outputmarks; {the procedure outputs marks of students}


begin
writeln(‘distinctions are’, d);
writeln(‘merits are’, m);
writeln(‘passes are’, p);
writeln(‘fails are’, f);
writeln(‘total number of students’, c);
end;
begin {main program}
c:=0;
initialize;
ans:=’y’;
while (ans=’y’) or (ans=’Y’) do
begin
c:=c+1;
inputmarks;
grade;
writeln(’do you want to continue y/n?’);
readln(ans);
end;
outputmarks;
end.

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.

4. Explain the use of the EXIT command as used with procedures

You might also like