Functional and Modular Programming: Computer Programming BFC 20802 Prepared by DR Goh Wan Inn
Functional and Modular Programming: Computer Programming BFC 20802 Prepared by DR Goh Wan Inn
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
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.
Function Header
Function
Body
Return Value
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()
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