0% found this document useful (0 votes)
2 views9 pages

Top-Down Design, Libarary Routine, Sub-Routine, Variables

The document covers key concepts in computer science, focusing on top-down design, decomposition, abstraction, and stepwise refinement for problem-solving. It explains the use of pseudocode, flowcharts, structured English, and structure diagrams as methods for designing algorithms, along with the roles of library routines and subroutines in programming. Additionally, it distinguishes between global and local variables, outlining their characteristics and scope within a program.

Uploaded by

nahiankadir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Top-Down Design, Libarary Routine, Sub-Routine, Variables

The document covers key concepts in computer science, focusing on top-down design, decomposition, abstraction, and stepwise refinement for problem-solving. It explains the use of pseudocode, flowcharts, structured English, and structure diagrams as methods for designing algorithms, along with the roles of library routines and subroutines in programming. Additionally, it distinguishes between global and local variables, outlining their characteristics and scope within a program.

Uploaded by

nahiankadir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Science

Teacher: Maruf Ahmed


Top-down design, Library routine, Sub-routine, Types of variables

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

Abstraction:
• Abstraction keeps the key elements required for the solution to the problem and discards any
unnecessary details and information that is not required
• Abstraction is used to filter out information / data that is not necessary for the task
• To keep only information / data that is necessary for the task
Page 1 of 9
Stepwise refinement:
• The breaking down of an algorithm / task / problem
• to a level of (sufficient) detail // into smaller parts / sub-tasks
• from which it can be programmed // which are easier to program

Explain the purpose / goal of stepwise refinement.


The purpose is:
• to express the algorithm in a level of sufficient detail // to split a large task into (smaller) sub-tasks
• so that it can be programmed // so that individual tasks are easier to solve // to make the problem
more manageable / understandable

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
• Structured English
• 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.

Structured English:
- Is a method of showing the logical steps in an algorithm, using an agreed subset of straightforward
English words for commands and mathematical operations to represent the solution
- Is a way of describing an algorithm using a small subset of the English language and a few simple
conventions

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.
Page 2 of 9
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.

Program libraries / Library routines:


- A library routine is a debugged block of code, often designed to handle commonly occurring
problems or tasks.
- Library routines are stored in a program library and given names. This allows them to be called into
immediate use when needed, even from other programs.
- They are designed to be used frequently.

Some features of program libraries / library routines:


• Set of pre-written / pre-compiled / pre-tested subroutines
• can be linked into a program without amendment
• To perform common / complex tasks
• The program library can be referenced/imported
• The functions/routines can be called in one’s own program

Benefits of using library routines in a program


• Code does not have to be written/re-written from scratch
• Code is already tested so it is more robust/likely to work/ should be relatively free from errors
• Precompiled
• Saves programming time
• The programmer can use e.g., mathematical functions that s/he may not know how to code
• If there is an improvement in the library routine the program updates automatically

Drawbacks of using library routines


• Compatibility issues: may not work with the other code/may require changing program for it to work
• Not guaranteed thorough testing
• may be unknown or unexpected bugs / virus
• Library routine may not meet exact needs
• If library routine is changed there may be unexpected results / errors

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 / is a collection of programming statements under a
single identifier
- 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 / to allow modular programs to be created

Page 3 of 9
Modular design, or modularity in design, is a design principle that subdivides a system into smaller parts
called modules
Benefits of sub-routines:
• a task which is repeated / reused / performed in several places which makes program shorter
• 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
• to make program creation faster because procedures can be re-used

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 so that they can be used in the subroutine
- allows the procedure / function to be re-used with different data

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.

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.

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

Page 4 of 9
What do you mean by Subroutine header? Subroutine header is the first line of the definition which
specifies the 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 by its name and
along with the parameter/s if needed 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 needed 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:

Set ReturnValue to SubtractNumber(30, 20)

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 name> (which must be of the type mentioned in the header)
ENDFUNCTION

Page 5 of 9
Problem: Write down a procedure which will take input of two integers and find the addition of the
numbers and display the result.

PROCEDURE Add1() //Procedure header


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

//Main program starts here


CALL Add1() //Procedure call made with CALL keyword

Problem: Write down a function which will take input of two integers and return the addition of the
numbers.

FUNCTION Add2() RETURNS INTEGER //Function header. There will always be a return data type
//mentioned in the header
DECLARE Num1, Num2, Sum: INTEGER
OUTPUT “Enter two numbers:”
INPUT Num1, Num2
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. The value may be returned as a variable or could be a direct
//value
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 data type
OUTPUT Result
// An example of function and procedure together with parameter passing

Problem: Write down a subroutine which will receive one whole number as a parameter and display a
message whether the number is odd or even.

PROCEDURE OddOrEven(Num: INTEGER)


IF Num MOD 2 = 0
THEN
OUTPUT Num, “is an even number”
ELSE
OUTPUT Num, “is an odd number”
ENDIF
ENDPROCEDURE

//Main program starts here


//Procedure call
CALL OddOrEven(15)

Page 6 of 9
Problem: Write down a subroutine which will receive two whole numbers as parameters and returns TRUE
if the first number a multiple of the second number or FALSE otherwise. Call the subroutine and display a
proper message based on the return value

FUNCTION Multiple(Num1, Num2: INTEGER) RETURNS BOOLEAN


IF Num1 MOD Num2 = 0
THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION

//Main program starts here


DECLARE Result: BOOLEAN
//Function call
Result ← Multiple(20,10)
IF Result = TRUE
THEN
OUTPUT “First number is a multiple of the second number”
ELSE
OUTPUT “First number is not a multiple of the second number”
ENDIF

// An example of function and procedure together with parameter passing


Problem: Write down the pseudocode for the following scenario:
- Take input of two numbers in the main program
- Pass the two numbers into a function as parameters
- The function will find the average of the two numbers and returns the value to the main program
- The return value will be passed to a procedure and will be used to display the average

FUNCTION Average (N1, N2: INTEGER) RETURNS REAL


DECLARE Avg: REAL
Avg ← (N1+ N2) / 2
RETURN Avg
ENDFUNCTION

PROCEDURE OutputAverage(Num: REAL)


OUTPUT “Average = ”, Num
ENDPROCEDURE

//Main program starts here


DECLARE AvgValue : REAL
DECLARE Num1, Num2 : INTEGER

INPUT Num1, Num2


AvgValue ← Average (Num1, Num2)
CALL OutputAverage(AvgValue)

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

Global variable:
- Any variable declared outside any subroutine is known as global variable

Page 7 of 9
- Global variables can be used by any other subroutines and any part in the program
- Value of a global variable can be changed anywhere in the program

Local variable:
- The variables which are declared and used inside a subroutine are known as local variables
- Local variables can be used only by the subroutines in which they were created
- Value of a local variable cannot be changed elsewhere in the program

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 (created once when the program starts
execution) 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.

Exceptions: What if a local variable has same name as the global one?
Answer is that the priority of the local variable is higher than the global. Means the subroutine that has the
same name variable as the global variable will never be able to access the global variable because every time
it refers to “a”, it would be referring to its own “a”. That own “a” has higher priority so it will not be able to
touch the global “a”.
Exceptions: When two of the subroutines have the same name variable?
This will be regarded as two different variables which will be created in two different memory locations.
They have nothing to do with each other.

Example use of global and local variables:

PROCEDURE FindTotal():
DECLARE NumArray1 : ARRAY[1:5] OF INTEGER //Local array
DECLARE i : INTEGER //Local variable
FOR i ← 1 TO 5
Total ← Total + NumArray1[i]
NEXT i
ENDPROCEDURE

PROCEDURE ArrayTotal():
DECLARE NumArray2 : ARRAY[1:5] OF INTEGER //Local array
DECLARE i : INTEGER //Local variable
FOR i ← 1 TO 5
Total ← Total + NumArray2[i]
NEXT i
ENDPROCEDURE

##Main program
DECLARE Total : INTEGER //Global variable
Total ← 0
CALL FindTotal()
OUTPUT “After calling FindTotal, Total=”,Total

CALL ArrayTotal()
OUTPUT “After calling ArrayTotal, Total=”, Total)

//Python code
def FindTotal():

Page 8 of 9
global Total
NumArray1 = [1, 2, 3, 4, 5]
for i in range(0, 5):
Total = Total + NumArray1[i]

def ArrayTotal():
global Total
NumArray2=[10,20,30,40,50]
for i in range(0,5):
Total=Total+NumArray2[i]

##Main program
global Total
Total=0
FindTotal()
print('After calling FindTotal, Total=',Total)

ArrayTotal()
print('After calling ArrayTotal, Total=',Total)

Parameter passing techniques:


- Pass by value / Call by value
- Pass by reference / Call by reference

Pass by value: This means that the values are passed (a copy of the variable/s) of the parameters to the
subroutine. If any kind of change is done to the parameters inside the subroutine, then those changes are not
reflected back to the actual parameters.
Pass by reference: This means that the references (addresses) are passed of the parameters to the
subroutine. If any kind of change is done to the parameters inside the subroutine, then those changes are
going to get reflected back to the actual parameters.

N.B., we can only use pass by value for functions. We can use both pass by value and pass by reference for
procedures.

In pseudocode to pass by value we have to write, BYVAL before the variable in the header part. However, if
nothing is mentioned in the subroutine header then it is assumed that the parameter has been passed by
value.
In pseudocode to pass by reference we have to write, BYREF before the variable in the header part. Anytime
pass by reference needs to be used it must be mentioned in the subroutine header.

For example, PROCEDURE FindIndex(BYREF X : INTEGER, BYVAL Y: REAL)


For example, PROCEDURE FindIndex(BYREF X : INTEGER, Y: REAL) //This means that both the
parameters are passed by reference
For example, PROCEDURE FindIndex(X : INTEGER, Y: REAL) //This means that both the parameters are
passed by value

Page 9 of 9

You might also like