Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Computer Science

Teacher: Maruf Ahmed


Process of solving problems, subroutines, maintainable program

Algorithm: An algorithm is a plan, a logical step-by-step process for solving a problem. Algorithms are
normally written in pseudocode or drawn as a flowchart.

The computer system and its sub-systems


Top-down design:
- Top-down design is the decomposition of a computer system into a set of subsystems, then
breaking each sub-system down into a set of smaller sub-systems, until each sub-system just
performs a single action.

Advantages of top-down design


 Making the design well-structured and easier to understand, modify and debug
 Speeding up the development time and reduces testing time
 Different problem parts can be assigned to different teams

An example of a top-down design of a Temperature Conversion System:

Decomposition:
- Is breaking down a problem / task into sub problems / steps / smaller parts in order to explain /
understand
- It is easier to solve the problem leading to the concept of program modules
- Different problem parts can be assigned to different teams

Decomposing a problem into its component parts:


Any problem that uses a computer system for its solution needs to be decomposed into its component
parts. The component parts of any computer system are:
 inputs – the data used by the system that needs to be entered while the system is active
 processes – the tasks that need to be performed using the input data and any other previously
stored data
 outputs – information that needs to be displayed or printed for the users of the system
 storage – data that needs to be stored in files on an appropriate medium for use in the future
Page 1 of 6
Stepwise refinement:
- The process of breaking down into smaller sub systems is called stepwise refinement.
- Stepwise Refinement is the process of breaking down a programming problem into a series of
steps.

Methods used to design and construct a solution to a problem:


Solutions to problems need to be designed and developed rigorously. The use of formal methods enables
the process to be clearly shown for others to understand the proposed solution. There are three ways this
can be achieved. They are:
 pseudocode
 flowcharts
 structure diagrams

Pseudocode:
- Pseudocode is a simple method of showing an algorithm.
- It describes what the algorithm does by using English key words that are very similar to those
used in a high-level programming language.
- However, pseudocode is not bound by the strict syntax rules of a programming language. It does
what its name says; it pretends to be programming code!

Flowcharts:
- A flowchart shows diagrammatically the steps required to complete a task with some pre-defined
shapes (boxes) and the order that they are to be performed.
- A flowchart consists of specific shapes which are linked together with flow lines.
- It is basically a diagrammatic representation of an algorithm.

Structure diagrams:
- In order to show top-down design in a diagrammatic form, structure diagrams can be used.
- This shows the design of a computer system in a hierarchical way, with each level giving a more
detailed breakdown of the system into sub-systems.
- If necessary, each sub-system can be further divided.
- This gives the overview of the program or subroutine
- It also shows the relationship between different components of a system

You may have to draw complete structure diagram / fill up some incomplete boxes based on a given
scenario.

Example practice question of structure diagram:


A satellite navigation system works using destination details entered by the user, either a new destination
or chosen from previously saved destinations. The satellite navigation system will then output directions
to the destination in the form of either a visual map or a list of directions.

A satellite navigation system is an example of a computer system that is made up of sub-systems.


Draw the required structure diagram to show the main system and all the sub-systems of the satellite
navigation system.

Page 2 of 6
Sub-routines
- A subroutine is a sequence of program instructions that perform a specific task, packaged as a unit
- This unit can then be used in programs wherever that particular task should be performed
- It usually performs a task that is frequently required
- In different programming languages, a subroutine may be called a procedure, a function, a
routine, a method, or a subprogram

Characteristics / Features of sub-routines are:


- A subroutine is not a complete program
- A subroutine is a self-contained piece of code
- A subroutine cannot execute on its own
- A subroutine is called from within a program to get executed
- A subroutine may or may not return a value to the code from which it was called

Reason for using subroutines in the construction of an algorithm:


 To make a more manageable / understandable solution
 To support modular design

Benefits of sub-routines:
 a task which is repeated / reused / performed in several places
 Reduces complexity of program / program is simplified
 Testing / debugging / maintenance is easier
 Allows teams to work on different parts of the solution
 If the task changes the change needs to be made only once
 Reduces unnecessary duplication / program lines

According to O’ level IGCSE syllabus there are two types of sub-routines. They are procedure and
function.

Procedure:
- A subroutine that does not return a value to the caller
- Procedure calls are single standalone statements

Function:
- A subroutine that always returns a value to the caller
- Function calls are made as part of an expression, on the right-hand side and not as a standalone
statement. When a function is called it will always be on the right hand side of the assignment
operator and there will be a variable on the left hand side to catch the value that will be returned
by the function

Parameters:
- are special kind of variables used in a sub-routine to refer to one of the pieces of data provided as
input to the sub-routine
- Sometimes we need to pass values (may be direct value or may be by passing variables) to a
subroutine as arguments. Those have to be received by the subroutine. In the subroutine header
part, the number of variables and their data types must be mentioned to receive those values.
Those variables which are given in the subroutine header part are known as parameters. The
subroutine will be able to work with those variables in its definition part. When a subroutine is
called, number of matching parameters along with their matching data types must be given.

Purpose / use of parameters:


They are used to pass values / arguments to the subroutine
Page 3 of 6
Arguments: When values (may be direct value or may be by passing variables) are passed to execute a
subroutine, those values are known as arguments and those arguments will be received by parameters.

This is to be remembered that if a subroutine does not have parameters defined in the header part then,
when the subroutine is called, no argument is needed to be passed. This type of subroutine is known as
void subroutine.

What do you mean by Subroutine definition? Subroutine definition is setting up the subroutine to do
the required task

What do you mean by Subroutine call? Subroutine call is to use the subroutine to perform the set task

What do you mean by Subroutine header? Subroutine header is the first line of the definition which
specifies type of subroutine (procedure / function), the name of the subroutine, and any parameters to be
received or not into the subroutine.

How subroutine gets executed? Sub-routine cannot execute on its own. It has to be called to get
executed. When the call is made of a subroutine then the control of execution is passed to the subroutine.
It finishes the task and then the control returns back to the place where it was called from.
If the sub-routine is a function then after performing the tasks that have been defined in the function
definition, it returns a value to the caller and the job of the function is done. That means the control is
back to the place where the sub-routine was called from.
If the sub-routine is a procedure then after performing the tasks that have been defined in the procedure
definition, it just returns the control to the caller and the job of the procedure is done. That means the
control is back to the place where the procedure was called from.

How would you identify a subroutine in an algorithm? Any time an identifier with parenthesis () is
present in a code or in a flowchart, then the identifier is a representation of a subroutine.

Representation of subroutine in flowchart:


Every time a subroutine (procedure or function) needs to be called, this will be represented by the
following shape / box in flowchart.

For example, if a procedure AddNumber() is called without any parameter in the program then it will be
represented in the following way:

CALL AddNumber() //CALL keyword is optional for procedure calling

If a function SubtractNumber() is called with two integer parameters in the program then it will be
represented in the following way:

ReturnValue ← SubtractNumber(30, 20)

Page 4 of 6
Pseudocode example for procedure and function:

N.B. This is to be remembered that any local variables used within a subroutine must be declared before
use. Any variable mentioned in the parameter does not need to be declared in the subroutine.

PROCEDURE declaration:
PROCEDURE identifier name (parameter/s if any)
statement(s)
ENDPROCEDURE

FUNCTION declaration:
FUNCTION identifier name (parameter/s if any) RETURNS data type
statement(s)
RETURN variable (which must be of the type mentioned in the header)
ENDFUNCTION

Write down a procedure which will find the addition of two numbers and display the result.

PROCEDURE Add1() //Procedure header


DECLARE Num1, Num2, Sum: INTEGER
INPUT Num1, Num2
Sum ← Num1 + Num2
OUTPUT “The total of two numbers = ”, Sum
ENDPROCEDURE

//Main program starts here


CALL Add1() //Procedure call. CALL word may or may not be used

Write down a function which will return the addition of two numbers.
FUNCTION Add2() RETURNS INTEGER //Function header. There will always be a return data type
//mentioned in the header
DECLARE Num1, Num2, Sum: INTEGER
Num1 ← 50
Num2 ← 60
Sum ← Num1 + Num2
RETURN Sum //There will always be at least one statement which will return a value of the data type
//mentioned in the header
ENDFUNCTION

//Main program starts here


DECLARE Result: INTEGER

//Function call
Result ← Add2() //The value that will be returned from Add2() function will be received by Result. The
//data type of Result must match with the return type
OUTPUT Result

// An example of function and procedure with parameter passing


FUNCTION Average (a, b: INTEGER) RETURNS REAL
DECLARE x: REAL
x ← (a+b) / 2
RETURN x

Page 5 of 6
ENDFUNCTION

PROCEDURE OutputAvg(p: REAL)


OUTPUT “Average = ”, p
ENDPROCEDURE

//Main program starts here


DECLARE Avg : REAL
DECLARE num1, num2 : INTEGER
INPUT num1, num2
Avg ← Average (num1, num2)
CALL OutputAvg(Avg)

Types of variable
- Variables are of two types. They are global and local.

Global variable: Any variable declared and used outside any subroutine is known as global variable.
They are called global variables because they can be used by any other subroutines and anywhere in the
program

Local variable: The variables which are declared and used inside a subroutine are known as local
variables. They are called local variables because these variables can be used only by the subroutines in
which they were created.

Scope of variable: Scope means the area of the program where the variable can be used. The global
variable can be used anywhere in the program and by any subroutine in the program whereas the local
variable can be used only within the subroutine where the variable has been created.

Life of variable: Life of a global variable is once. It lives once and dies once at the end of the program.
Life of local variables is several times. They are created every time they are called and they die out when
the execution of the subroutine is finished.

Maintainable program
Once a program is written, it may need to be maintained or updated by another programmer at a later
date. The programmer may have no documentation other than a copy of the source program. Even a
programmer looking at their own program several years later may have forgotten exactly how all the
tasks in it were completed!

Features that should be included to create a maintainable program:


- Meaningful identifiers:
o to enable the programmer (or future programmers) to easily recognize the purpose of a
variable / array / constant / subroutine etc.
o to enable easy tracking of a variable / constant / array through the program
o For example variable Total should be used to do totaling
- Use of comments:
o to annotate each section of a program so that a programmer can find specific sections and
the programmer knows the purpose of that section of code
o For example: //The following loop is going to perform totaling of 100 numbers
- Use of sub-routines:
o to make programs modular and easier to update / add functionality
o For example: PROCEDURE Add(Num1, Num2) will be used to add two numbers

Page 6 of 6

You might also like