You are on page 1of 28

DAT 13303

COMPUTER ALGORITHM
CHAPTER 6: FUNCTIONS
Lesson Outcome
• State the purpose of modular approach
• Explain when to use modular approach
• Describe when to use parameter
• Differentiate between actual and formal parameters
• Explain module call statement
• Explain return statement
• Differentiate between local and global variables
• Construct algorithm to solve problems modularly
Intro
• Current way of writing program from 1st week of this sem until now: all programs
statements in one module - main() module.
• Problem with current way of writing: unsuitable for large programs - hard to read &
understand.
• Solution for large program:
• Proper code arrangement - clarify logical structure & flow of program execution.
• Construct it from smaller pieces or modules (sub-program), each of which is more
manageable than original problem - Modular programming:
• Concept: Divide and Conquer, Hiding
• organizing a program into small, independent modules that are separately
named and individually invoke to perform a specific job
Top-down design – intro
• break down a big or complex problem into a few
smaller problems.
• Try to get the solution for each small problem
independently.
• Should any of these small problems be broken down
further? If could easily write the algorithm for the small
problem, no. Else, break it down again.
• write algorithm for each of the small problems.
• combine all algorithms for each smaller problem to get
a complete algorithm for an entire problem.
• If not yet complete algorithm for a certain sub problem,
create a stub to replace the incomplete algorithm to
enable execution of the entire solution.
• E.g. problem broken into 3 parts, X, Y and Z. Algorithm
completed only for X and Y. cannot integrate all
solutions because of incomplete algorithm for Z. To test
algorithm X and Y, replace algorithm Z with a stub.
Top-down design – intro
• complete solution for a complex problem requires
• tremendous effort, planning
• team effort
• team divided into several smaller groups.
• Each group consists of a few staffs.
• each part handled by a group.
• each staff in group concentrate to find solution for that
particular part only.
• At the end, solution for each part gathered to get entire solution
for problem.
Top-down design - Advantages
• Breaking problem into parts
• clarify what needs to be done.
• allows more than one person to work on solution
• avoids redundancy (repeated instructions)
• Easier, faster to write, debug.
• Increase readability
• logical structure more apparent
• program less complex - At each step of refinement, new sub problems become less
complicated and therefore, it is easier to figure out and can be easily documented and
coded.
• Program easier to maintain
• Parts of the solution may turn out to be reusable
Top-down design - example
activities
ATM machine

• above problem handled by 4 teams


• Each activity per team
Skeleton for pseudo code
Complex problem

Calling module statement


Simple problem

Called module

execution resume at main

Module header

Module body
1st skeleton 2nd skeleton
Flowchart for 2nd skeleton
Execution of algorithm using module (without
parameter)
Test yourself:
iii)18 1 4 -9
iv) 67 5 15 33
Naming module
• no specific way
• similar as how to name variable. As long as name can give idea what’s done
inside
• E.g.
• module to find max value - findMax() or calculateMaximum()
• module to calculate average of N numbers - calculateAvg() / calcAvg() / calcAverage()
• symbol () used after module’s name
Examples of Problem Solving Using Module
• steps:
• Write basic algo
• Convert basic algo -> main module
• Convert each process (complex or relatively complex) into module
• Do not convert a simple process into module
Example Main module Module

Start Read 3 numbers 3:


Read 3 numbers Display biggest, smallest, numbers in ascending order  
Find biggest Find biggest
Find smallest Find smallest
Sort in ascending order Sort numbers in ascending order
Display biggest, smallest, numbers in
ascending order
End
Start Read basic salary Calculate increment
Read basic salary Display basic salary, increment, new salary Calculate new salary
Calculate increment
Calculate new salary
Display basic salary, increment, new salary
End
Actually perform same process
Difference: diagram size (rows, columns), symbol
Convert to general module

Formal parameters
-supplied from main

Depends on which diagram


to draw
In formal/ value formal parameter
-Supplied by main module and go in to module

• No of actual = no of formal
• Parameter aka argument:
special variable used to pass data between main
module and module(s)
• Type of formal para
• In: calling module  called module
• Out: called module  calling module
• In-out:
-values supplied to formal parameters 1. Calling module  called module
-inside () at main module 2. Change data
3. Called module  calling module
• Situations when module no need para:
• No data needed to pass from calling  called
• Data needed in called already known or inside
Test yourself: called
draw flowchart
Formal parameter

“out” shows data flows modules  main


Actual parameter

valueF , valueG
passed into main

Parameter F, G will accept


No link between nom, number, and main module 
local variable: recognized inside module only
In formal para
Out formal para

Actual para

Actual para
Array As A Parameter
• can pass
• entire array
• use an address/reference formal parameter. The
array itself is a reference.
• an element of array
• use a
• value formal parameter or as
• an address/reference formal parameter.
Entire Array As Parameter
Algorithm asks user to enter salaries for 12 employees. Calculate new salary for all
employees. Print old and new salaries. The increment is 15% of current salary. Write the
algorithm using modules as follows:
• ask user to enter name and store into array name in module inputName(). 
• ask user to enter salary and store into array salary in module inputSalary(). 
• calculate new salary where new salary = salary + increment in module
calculateNewSalary().
• Display salary and new salary in module print() as follows:
Main module Module inputSalary(in size, out salary[])
Start for(i=0, i<size, i++)
size = 12 Local variable read salary[i]
next
//array declaration
Return
name[size]
salary[size] Module calculateNewSalary(in salary[], out newSalary[], in size)
newSalary[size] for(j=0, j<size, j++)
//module call newSalary[j] = salary[j] x 15% + salary[j]
inputName(size, name) next
inputSalary(size, salary) Return
calculateNewSalary(salary, newSalary, size)
Module print(in name[], in salary[], in newSalary[], in size)
print(name, salary, newSalary , size)
Display “Employee’s Name Salary New Salary”
End Display newline
for(k=0, k< size, k++)
Display name[k], “ ”, salary[k], “ “ , newSalary[k]
Module inputName(in size, out name[]) Display newline
for(h=0, h<size, h++) next
read name[h] Return

next
Return
Test yourself
Ask user to enter 20 marks. Calculate and display average of marks.
Then display all marks which is greater than average. Use following
modules:
• inputData() – receive array size. Return filled array.
• calcAvg() – receive marks, array size. Return average.
• displayInfo() – receive marks, average, array size. Display average,
marks greater than average.
An Element of Array As Parameter
Main module Module swap (in-out data1, in-out data2)
Start temp = data1
num[4] = {20, 1, 6 90} data1 = data2
data2 = temp
Return
Display “Original data: ”
For(j = 0, j < 4, j++)
Display num[j], “ ”
next
Display newline
Display newline
 
Call swap (num[0], num[1])
Call swap (num[2], num[3])
 
Display “After process: ”
For(j = 0, j < 4, j++)
Display num[j], “ ”
Next
End
Test yourself
Create an array.
Initialize with following data: 5 2 3 4 6 10 9 7 5 20 79 and 100.
Increase value of each element by 10 using module changeData().
solution 1: passing array as parameter
solution 2: passing an element of array as parameter (use loop to call)
Display new content of array using module displayNewData().

You might also like