Processofsolvingproblems, Subroutines, Maintainableprogram
Processofsolvingproblems, Subroutines, Maintainableprogram
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.
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
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.
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
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.
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.
For example, if a procedure AddNumber() is called without any parameter in the program then it will be
represented in the following way:
If a function SubtractNumber() is called with two integer parameters in the program then it will be
represented in the following way:
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.
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
//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
Page 5 of 6
ENDFUNCTION
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!
Page 6 of 6