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

TOPIC 6: FUNCTIONS

ALGORITHM DESIGN FOR PROGRAMS USING MODULES


COURSE OUTCOME
1. Analysis of problems requiring modules (functions)
A. Introduction to module
B. Modular concept
C. Step in modularization
2. Basic types of functions
3. Parameter passing: Passing-by-value and passing-by-reference
4. Algorithm development for modular programming (pseudocode
and flowchart)
1. ANALYSIS OF PROBLEMS REQUIRING
MODULES (FUNCTIONS)
1.A - INTRODUCTION TO MODULE
i. Programmer always breaking the programming problem into
reasonable units, and tackle one small task at a time.
ii. These reasonable units are called modules.
iii. Programmer also refers them as subroutines, procedure, functions,
or methods.
iv. The reason of doing modularization are:
a. Modularization provides abstractions.
b. Modularization allows multiple programmers to work on a problem
c. Modularization allows you to reuse your work
d. Modularization makes it easier to identify structures.
1.B - MODULAR CONCEPT
a. Modularization provides abstractions
• Easier to understand whereby it enable programmer to see the big
picture.
• Abstraction is the process of paying attention to important properties
while ignoring nonessential details.
• Some level of abstraction occurs in every computer program. Back
into 50s, a programmer had to understand the low‐level circuitry
instructions in the computer used. But now, newer high‐level
programming languages allow you to use English‐like vocabulary in
which one broad statement corresponds to dozens of machine
instructions.
1.B - MODULAR CONCEPT
a. Modularization provides abstractions
Modularization Without modularization
Do laundry Pick up laundry basket
Call Aunt Nan Put laundry basket in car
Start term paper Drive to Laundromat
Get out of car with basket
Walk into Laundromat
Set basket down
Find quarters for washing machine
…. And so on.
1.B - MODULAR CONCEPT
b. Modularization allows multiple programmers to
work on a problem
• When you break any large task into module, you gain ability to divide
the task among various people
• For example word‐processing. This program has so many options, and
responds to user selections in so many possible ways, that it would
take years for a single programmer to write all the instructions.
• Professional software developers can write a new program in weeks
or month, instead of years, by dividing large programs into modules
and assigning each module to an individual programmer or
programming team.
1.B - MODULAR CONCEPT
c. Modularization allows you to reuse your work
• A well‐written of function or subroutine or modules can be used
more than once within a program or in other programs.
• For example, a routine that checks the current months to make sure it
is valid (not lower than 1 and not higher than 12) is useful in many
program written for a business.
• Module, functions or even software that is reusable is more reliable.
It is also save time and money.
1.B - MODULAR CONCEPT
d. Modularization makes it easier to identify
structures
• When combining several programming tasks into modules, it may be
easier to identify structures.
• A statement that has common task and same structure can be
grouped into one single module.
1.C - STEP IN MODULARIZATION
Top‐down modular design is quite simple if the following steps are
performed every time you are presented with a programming design.
i. Define the problem by dividing it into its three components: input,
output and processing.
ii. Group the activities into subtasks or function to determine the modules
which will make up the program. Remember that a module is one
section of a program dedicated to the performance of a single function.
iii. Construct a hierarchy chart to illustrate the modules and their
relationship to each other.
iv. Establish business logic of the process (mainline) of the algorithm.
v. Develop the algorithm for each successive module in the hierarchy chart.
vi. Check the solution algorithm.
2. BASIC TYPES OF FUNCTIONS
2. BASIC TYPES OF FUNCTIONS
i. Function without value returned and parameter
• Does not return any value to the calling function and does not accept any
value from calling function.
ii. Function with value returned but without parameter
• Pass value to the calling function but does not receive a value from calling
function.
iii. Function without value returned but with parameter
• Does not return any value to the calling function but receive value from
calling function.
iv. Function with value returned and parameter
• Pass value to the calling function and receive a value from calling function.
3. PARAMETER PASSING: PASSING-BY-
VALUE AND PASSING-BY-REFERENCE
3. PARAMETER PASSING

• Another method of inter‐module communication is the passing of


parameters or arguments between modules.
• A parameter can be a variable, literal, or constant which can communicate
between various parts of a program.
• It may have one of three functions:
i. To pass information from a calling module to subordinate module. The
subordinate module would then use that information in its processing, but would
not need to communicate any information back to the calling module.
ii. To pass information from subordinate module to its calling module. The calling
module would then use that parameter in subsequent processing.
iii. To fulfill a two‐way communication role. Information may be passed by the calling
module to a subordinate module, where it is modified in some fashion and then
passed back to the calling module.
3.A PARAMETER PASSING:
PASSING-BY-VALUE
• When passing arguments by value, the only way to return a value
back to the caller is via the function's return value.
• By default, arguments in C++ are passed by value.
• When arguments are passed by value, a copy of the argument is
passed to the function.
• Because a copy of the argument is passed to the function, the original
argument can not be modified by the function.
3.A PARAMETER PASSING:
PASSING-BY-VALUE
• When arguments are passed by value, the called function creates a
new variable of the same type as the argument and copies the
argument's value into it. As we noted, the function cannot access the
original variable in the calling program, only the copy it created.
• Passing arguments by value is useful when the function does not
need to modify the original variable in the calling program.
• In fact, it offers insurance that the function cannot harm the original
variable.
3.B - PARAMETER PASSING:
PASSING-BY-REFERENCE
• Passing arguments by reference uses a different mechanism. Instead
of a value being passed to the function, a reference to the original
variable, in the calling program, is passed.
• An important advantage of passing by reference is that the function
can access the actual variables in the calling program.
• This provides a mechanism for passing more than one value from the
function back to the calling program.
3.B - PARAMETER PASSING:
PASSING-BY-REFERENCE
In pass by reference, we declare the function parameters as references rather
than normal variables:

#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int & c); void duplicate (int& a, int& b, int & c)
int main() {
{ a*=2;
int x=1, y=3, z=7; b*=2;
duplicate (x,y,z); c+=2;
cout << "x="<<x<<", y=“<<y<<", z="<<z; }
return 0;
}
3.B - PARAMETER PASSING:
PASSING-BY-REFERENCE
• Reference arguments are indicated by the ampersand (&) following the
data type:
int& a
• When the function is called, a will become a reference to the argument.
• Since a reference to a variable is treated exactly the same as the variable
itself, then any changes made to the reference are passed through to the
argument!
void duplicate (int& a, int& b, int& c)
void duplicate (int& x int& y int& z
void duplicate (int& x, int& y, int& z);
3.B - PARAMETER PASSING:
PASSING-BY-REFERENCE
• Sometimes we need a function to return multiple values.
• However, functions can only have one return value.
• One way to return multiple values is using reference parameters.
• Advantages of Pass By Reference:
• It allows us to have the function change the value of the argument, which is
sometimes useful.
• Because a copy of the argument is not made, it is fast, even when used with
large structs or classes.
• We can return multiple values from a function.
3.B - PARAMETER PASSING:
PASSING-BY-REFERENCE
3.B - PARAMETER PASSING:
PASSING-BY-REFERENCE
3.C PARAMETER PASSING:
PASSING-BY-VALUE vs PASSING-BY-REFERENCE
4. ALGORITHM DEVELOPMENT FOR
MODULAR PROGRAMMING
(PSEUDOCODE AND FLOWCHART)
Example 1:
Calculate the sum and average of 2 numbers
Example 1:
Calculate the sum and average of 2 numbers
CORRECT WRONG
Example 1:
Calculate the sum and average of 2 numbers
Example 1:
Calculate the sum and average of 2 numbers
Example 2:
Calculate the area of one circle
Example 2:
Calculate the area of one circle
Example 2:
Calculate the area of one circle
THE END

You might also like