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

Functional and Modular

Programming
COMPUTER PROGRAMMING
BFC 20802
Prepared by Dr Goh Wan Inn
Modular Programming
• Modular programming: breaking a program up
into smaller, manageable functions or
modules

• Function: a collection of statements to


perform a task
• Motivation for modular programming:
– Improves maintainability of programs
– Simplifies the process of writing programs
Modular Programming

Figure 4-1: Structure Chart


Program Syntax between
Conventional vs Modular Programming

4
Functional Programming
• Paradigm of writing a computer program using
functions.
• Function is a group of statement that
executed together to perform a specific task.
• Every phyton program has at least one
function, which is the main function.
Process of using function
1. Declare the function:
• Known as function declaration. Write a function prototype
that specifies:
– the name of the function.
– its list of arguments and their types.
– use keyword of def to define function.

2. Define the function:


• Known as function definition or function implementation.
• Write the block of statements (body) of the function to
define processes should be done by the function.

3. Call the function:


• Known as function call or function invocation.
• Call the name of the function in order to execute it.
Defining and Calling Functions
• Function call: statement causes a function to
execute
• Function definition: statements that make up a
function
Function Name Formal Parameter

Function Header

Function
Body

Return Value

Writing function for calculating area of


rectangular
9
Function Element Description

Function Header The first line containing keyword def, function name, formal
parameter, parenthesis, and double colon.
Function Name Name of the function that declares using keyword def. To use
the function, this name will be called into the main function.
Formal Parameter This is like a placeholder and it is optional. When a function is
invoked, the value from the actual parameter will pass to a
formal parameter.
Function Body The compound statement that contains the programming
statement that defines what does the function does.
Return Value Require to returning the result to the main function. The
function terminates when a return statement is executed.

10
Calling a Function

Slide 6- 11
Explanation on Function Process
def new_function(): # Declare function name
print ("Hello from a new_function file") # Define

print ("")
new_function() # Calling function
Calling a Function
• To call a function, use the function name
followed by ()
printHeading()

• When called, program executes the body of


the called function

• After the function terminates, execution


resumes in the calling function at point of call.
Calling Functions
• main can call any number of functions
• Functions can call other functions
• Compiler must know the following about a
function before it is called:
– name
– return type
– number of parameters
– data type of each parameter
Calling a function with or without return value

15
Code Modularization
• Technique of subdividing a computer
programming into a separate program
• Advantages of modular programming
i. Easy to debug
ii. Code is reusable
iii. Readability
iv. Reliability

16
Example of code module for calculating the
hypotenuse of the triangle

17
• Keywords from is used to access the library
storing the function
• Keywords import is to import the specific
function from the main library.

18

You might also like