Loading content...
Loading content...
Master reusable modular programming: defining functions with def, function invocation, parameters vs arguments, return vs print(), default parameters, integrating conditions & loops, and building practical data analysis pipelines.
Imagine calculating tax, shipping fees, or conversion rates in 50 different places across your codebase. If the tax rate changes, you would have to find and update all 50 places manually.
# Price 1 tax1 = 500 * 0.18 # Price 2 tax2 = 1200 * 0.18 # Price 3 tax3 = 3500 * 0.18 # If rate changes to 0.12, fix every line!
def calc_tax(price):
return price * 0.18
tax1 = calc_tax(500)
tax2 = calc_tax(1200)
tax3 = calc_tax(3500)
# Rate change? Update in ONE place only!In Python, functions are defined using the def keyword, followed by the function name, parentheses (), and a colon ::
print("Hello!") and returns to callerdef greet(): print("Hello") and run the program without calling greet(), nothing will print.Define a function named greet() and then call it:
def greet():, indent 4 spaces, print "Hello from Pathubs", and call greet().Functions become truly powerful when they accept inputs. Beginners often confuse parameters with arguments:
The variable named in the function definition.
The real data value passed when calling the function.
Create a function that accepts two parameters: price and quantity:
def calculate_total(price, quantity): to print price * quantity. Test with calculate_total(500, 3) and calculate_total(1200, 2).This is one of the most important conceptual milestones in Python:
Displays characters on screen. The value cannot be stored or reused!
def add(a, b):
print(a + b)
res = add(10, 20)
print(res) # None!Sends the calculated value back to the caller for storage and reuse!
def add(a, b):
return a + b
res = add(10, 20)
print(res) # 30! Can be used in mathreturn statement automatically returns None.Write a function that returns the total so your program can apply further math:
calculate_total(price, quantity) that returns price * quantityprice=1000, quantity=2 and store in variable subtotal"Subtotal:", subtotaltax = subtotal * 0.1 and print "Tax:", tax| Feature | print() | return |
|---|---|---|
| Primary Purpose | Display information for the human user | Deliver data back to the program |
| Can Store in Variable? | No (stores None) | Yes (stores the actual result) |
| In Data Analytics Pipelines | Quick debugging and progress logs | Transforming rows, cleaning columns, calculating KPIs |
| Function Termination | Does NOT terminate the function | Immediately exits the function |
You can specify default values for parameters. If the caller does not supply an argument, Python falls back to the default:
Functions allow you to encapsulate decision-making logic and repeat it cleanly across entire datasets:
Apply your business rule function to a list of monthly sales figures:
Build a pricing utility function from scratch:
| Mistake | Incorrect Code | Correct Code | Why It Fails |
|---|---|---|---|
Forgetting def | calculate(): | def calculate(): | def is the required keyword that introduces a function. |
| Defining but never calling | def greet(): print("Hi") | greet() | Function definitions do not execute automatically. |
Using print() instead of return | def add(a,b): print(a+b) | def add(a,b): return a+b | Caller cannot store or reuse the computed value (receives None). |
| Wrong argument count | calculate_total(500) | calculate_total(500, 3) | Missing required positional arguments throws a TypeError. |
Build a modular sales performance pipeline with two reusable functions:
calc_revenue(unit_price, units_sold) → returns unit_price * units_soldcheck_performance(revenue, target=100000) → returns "Excellent" if revenue >= target, otherwise returns "Needs improvement"unit_price=2500, units_sold=50 (revenue = 125,000) and print results!Test your mastery of Python functions, return mechanics, parameter scoping, and default arguments:
Test your conceptual understanding of Python function definitions, parameter vs argument distinction, return mechanics, and default values.
1. What happens when a function without an explicit return statement finishes executing in Python?