18 min read
Modular Programming
Split a program into modules (functions) with one job each — easier to write, test, reuse and share.
Modules and Functions
A program made of two modulesPython
def calculate_discount(total): if total > 5000: return total * 0.9 return total def print_receipt(amount): print("Perera Corner Mart") print("Amount to pay: LKR", round(amount, 2)) bill = 7200to_pay = calculate_discount(bill)print_receipt(to_pay)Expected output
Perera Corner Mart Amount to pay: LKR 6480.0
Each function has one job. The main part of the program simply calls them in order. You will learn exactly how to write functions in the Python course.
Benefits of Modular Programming
| Why programmers use modules | |
|---|---|
Practice
The discount rule changes from 10% to 12%. In a modular program, where do you change it?
More lessons in Introduction to Programming · Next: Course Exam · Previous: Structured Programming
