Session 5 · Functions#
📖 Based on the blog posts Python Functions and Tasks: Python Function.
The big idea#
A function is a recipe: write the steps once, give them a name, and use them again whenever you need them, with different ingredients each time.
Everyday example: you don’t reinvent dosa every morning. You follow the same recipe; only the batter amount changes. Functions give you:
Reuse: write once, use many times.
Organisation: like sections in a recipe book.
No repetition: one “chop vegetables” recipe, used by many dishes.
Smaller problems: a big dinner becomes chutney + tiffin + sweet.
Before and after#
Without a function, the same formula appears three times:
celsius1 = 25
print(f"{celsius1}°C is {(celsius1 * 9/5) + 32}°F")
celsius2 = 30
print(f"{celsius2}°C is {(celsius2 * 9/5) + 32}°F")
25°C is 77.0°F
30°C is 86.0°F
With a function, the formula lives in one place:
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
for c in [25, 30, 15]:
print(f"{c}°C is {celsius_to_fahrenheit(c)}°F")
25°C is 77.0°F
30°C is 86.0°F
15°C is 59.0°F
If the formula ever has a bug, you fix it once.
Anatomy of a function#
Part |
Meaning |
|---|---|
|
“I’m defining a function” |
|
its name (same rules as variables) |
|
parameters: the inputs it expects |
indented body |
the steps; runs only when called |
|
sends a value back and ends the function |
|
a call; |
print vs return#
def greet(name):
print(f"Hello, {name}!") # shows text, returns None
def add(a, b):
return a + b # gives a value back
greet("Alice")
result = add(5, 3)
print(f"The sum is: {result}")
print(greet("Bob"))
Hello, Alice!
The sum is: 8
Hello, Bob!
None
print is for people to read. return is for the program to keep using the value. A
function without return gives back None.
More examples#
import math
def is_even(number):
return number % 2 == 0
def max_of_three(a, b, c):
largest = a if a > b else b
return largest if largest > c else c
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1) # a function can call itself (recursion)
def area_of_circle(radius):
return math.pi * radius ** 2
print(is_even(4), is_even(7))
print(max_of_three(3, 9, 5))
print(factorial(5))
print(round(area_of_circle(5), 2))
True False
9
120
78.54
Default values and keyword arguments#
A parameter can have a default, used when the caller leaves it out:
def power(number, exponent=2):
return number ** exponent
print(power(5)) # exponent defaults to 2
print(power(2, 10))
print(power(exponent=3, number=2)) # keyword arguments: any order
25
1024
8
Common mistakes#
Forgetting the brackets:
greet(no()) refers to the function, it doesn’t call it.Printing instead of returning:
total = add_and_print(2, 3)isNoneif the function only prints.Code after
returnnever runs.Calling before defining: Python runs top to bottom, so
defmust come before the first call.Naming a variable
max: it hides the built-inmax()function (the blog’s version did this; this page useslargest).
Hands-on exercises#
The seven tasks from Tasks: Python Function.
Task 1. Write greet(name) that prints a greeting.
Solution
def greet(name):
print(f"Vanakkam, {name}!")
greet("Priya")
Task 2. Write sum_two(a, b) that returns the sum.
Solution
def sum_two(a, b):
return a + b
print(sum_two(10, 20)) # 30
Task 3. Write is_even(n) that returns True for even numbers and False for odd ones.
Solution
def is_even(n):
return n % 2 == 0
print(is_even(10), is_even(7)) # True False
n % 2 == 0 is already True or False, so there’s no need for if … return True else return False.
Task 4. Write find_max(a, b) that returns the larger number.
Solution
def find_max(a, b):
if a > b:
return a
return b
print(find_max(4, 9)) # 9
Task 5. Write multiplication_table(n) that prints the table of n from 1 to 10.
Solution
def multiplication_table(n):
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
multiplication_table(7)
Task 6. Write celsius_to_fahrenheit(c) that returns the temperature in Fahrenheit.
Solution
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
print(celsius_to_fahrenheit(37)) # 98.6
Task 7. Write power(number, exponent) where exponent defaults to 2.
Solution
def power(number, exponent=2):
return number ** exponent
print(power(9)) # 81
print(power(2, 5)) # 32
Bonus · Bill splitter. Write split_bill(total, people, tip_percent=10) that returns how much
each person pays, rounded to 2 decimals.
Solution
def split_bill(total, people, tip_percent=10):
with_tip = total * (1 + tip_percent / 100)
return round(with_tip / people, 2)
print(split_bill(1200, 4)) # 330.0
print(split_bill(1200, 4, 0)) # 300.0