AiTechWorlds
AiTechWorlds
Think about a vending machine. You walk up, press B7 (that's your argument — what you're asking for), the machine does its internal work, and then it gives you a bag of chips (that's the return value — what you get back). The slot labeled B7 is the parameter — the slot defined to receive your input.
Every time you call a function, this same exchange happens: you give something in, the function processes it, and something comes back out.
These two words confuse almost every programmer at first. Here's the clean distinction:
# "celsius" is the PARAMETER — it's in the definition
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# 100 is the ARGUMENT — it's in the function call
result = celsius_to_fahrenheit(100)
The parameter celsius is a placeholder. When you call the function with 100, the value 100 fills the placeholder. Think of the parameter as an empty box with a label — the argument is what you put in the box.
Functions can accept multiple pieces of information:
def greet(first_name, last_name, title):
print(f"Good morning, {title} {last_name}!")
print(f"Welcome, {first_name}.")
greet("Alice", "Johnson", "Dr.")
Output:
Good morning, Dr. Johnson!
Welcome, Alice.
The order matters. Python matches arguments to parameters by position:
first_name gets "Alice"last_name gets "Johnson"title gets "Dr."return StatementSo far we've seen functions that print things. But printing isn't the same as returning. The difference is huge:
# This function PRINTS — the result disappears after display
def add_printing(a, b):
print(a + b)
# This function RETURNS — the result can be stored and used later
def add_returning(a, b):
return a + b
Why does this matter?
# Can I use the result of add_printing?
result1 = add_printing(5, 3) # Prints 8, but result1 is None
print(result1 * 2) # Error or prints "None2"
# Can I use the result of add_returning?
result2 = add_returning(5, 3) # result2 = 8
print(result2 * 2) # Prints 16
When a function returns a value, that value travels back to wherever the function was called. You can store it, use it in calculations, pass it to another function, or display it.
A function without a return statement implicitly returns None — Python's way of saying "nothing."
Sometimes you want a function to work even when the caller doesn't provide every argument. Default values handle this:
def send_email(to, subject, body, urgent=False, signature="Best regards"):
print(f"To: {to}")
print(f"Subject: {'URGENT: ' if urgent else ''}{subject}")
print(f"Body: {body}")
print(f"{signature}")
print()
# Using defaults
send_email("alice@example.com", "Meeting tomorrow", "See you at 10am")
# Overriding one default
send_email("boss@company.com", "Server down", "Production is offline", urgent=True)
Output:
To: alice@example.com
Subject: Meeting tomorrow
Body: See you at 10am
Best regards
To: boss@company.com
Subject: URGENT: Server down
Body: Production is offline
Best regards
Rule: Default parameters must come after non-default parameters in the function definition.
Instead of relying on position, you can name your arguments:
def order_coffee(size, milk, sugar=0, temperature="hot"):
print(f"Order: {size} {temperature} coffee, {milk} milk, {sugar} sugars")
# Positional — order matters
order_coffee("large", "oat")
# Keyword — order doesn't matter
order_coffee(sugar=2, size="medium", milk="whole", temperature="iced")
Output:
Order: large hot coffee, oat milk, 0 sugars
Order: medium iced coffee, whole milk, 2 sugars
Keyword arguments make code much more readable, especially when a function has many parameters.
Python has a unique ability: functions can return multiple values at once (as a tuple):
def get_min_max_avg(numbers):
minimum = min(numbers)
maximum = max(numbers)
average = sum(numbers) / len(numbers)
return minimum, maximum, average # Returns a tuple of 3 values
scores = [78, 92, 65, 88, 95, 71]
low, high, avg = get_min_max_avg(scores) # Unpack directly
print(f"Lowest: {low}")
print(f"Highest: {high}")
print(f"Average: {avg:.1f}")
Output:
Lowest: 65
Highest: 95
Average: 81.5
This is called tuple unpacking — Python automatically matches each variable on the left to each return value on the right.
Let's build something realistic. A company has workers earning an hourly rate. Standard hours are 40/week. Overtime is paid at 1.5× the regular rate.
def calculate_pay(name, hours_worked, hourly_rate, overtime_threshold=40):
"""
Calculate total pay including overtime.
Returns: (regular_pay, overtime_pay, total_pay, overtime_hours)
"""
if hours_worked <= overtime_threshold:
regular_pay = hours_worked * hourly_rate
overtime_pay = 0
overtime_hours = 0
else:
regular_pay = overtime_threshold * hourly_rate
overtime_hours = hours_worked - overtime_threshold
overtime_pay = overtime_hours * hourly_rate * 1.5
total_pay = regular_pay + overtime_pay
return regular_pay, overtime_pay, total_pay, overtime_hours
def print_payslip(name, hours_worked, hourly_rate):
regular, overtime, total, ot_hours = calculate_pay(name, hours_worked, hourly_rate)
print(f"\n{'='*40}")
print(f" PAYSLIP: {name}")
print(f"{'='*40}")
print(f" Hours worked: {hours_worked} hrs")
print(f" Hourly rate: ${hourly_rate:.2f}/hr")
print(f" Regular pay: ${regular:.2f}")
if ot_hours > 0:
print(f" Overtime ({ot_hours} hrs): ${overtime:.2f}")
print(f" {'─'*32}")
print(f" TOTAL PAY: ${total:.2f}")
print(f"{'='*40}")
# Process multiple employees
employees = [
("Alice Chen", 40, 25.00),
("Bob Martinez", 47, 22.50),
("Carol Davis", 35, 30.00),
]
for name, hours, rate in employees:
print_payslip(name, hours, rate)
Output:
========================================
PAYSLIP: Alice Chen
========================================
Hours worked: 40 hrs
Hourly rate: $25.00/hr
Regular pay: $1000.00
────────────────────────────────
TOTAL PAY: $1000.00
========================================
========================================
PAYSLIP: Bob Martinez
========================================
Hours worked: 47 hrs
Hourly rate: $22.50/hr
Regular pay: $900.00
Overtime (7 hrs): $236.25
────────────────────────────────
TOTAL PAY: $1136.25
========================================
========================================
PAYSLIP: Carol Davis
========================================
Hours worked: 35 hrs
Hourly rate: $30.00/hr
Regular pay: $1050.00
────────────────────────────────
TOTAL PAY: $1050.00
========================================
Notice how calculate_pay is a pure function — it takes data in, returns data out, never prints anything. print_payslip handles the display. This separation makes code easier to test and modify.
| Concept | What It Means |
|---|---|
| Parameter | Variable in the function definition |
| Argument | Actual value passed when calling |
| Default value | Used when argument not provided |
| Keyword argument | Named argument in function call |
| return | Sends value back to the caller |
| Multiple returns | Return tuple, unpack at call site |
The return value is what makes functions truly powerful. Instead of just printing and forgetting, functions can produce results that build on each other — the output of one becomes the input of another, creating complex behavior from simple pieces.
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises