Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← PYTHON FUNDAMENTALS LEVEL ONE PROFESSIONAL Β· Lesson 7 of 8

Module Six

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
7
Module 6 Β· Functions & Modularity Β· Python for 13-year-olds

🧩 Module 6 · Functions & Modularity

πŸ§‘β€πŸŽ“ for 13-year-olds reuse your code Β· 55 min
βš™οΈ def Β· return Β· args Β· import

Imagine writing the same 10 lines of code over and over. 😴 Boring, right? A function lets you save a block of code under a name, then run it whenever you want β€” just like pressing a button on a machine! πŸŽ›οΈ And modules let you use code other people already wrote (or split your own code into tidy files). This is how real programmers build big things.

1

Your first function βš™οΈ

A function is created with the keyword def, followed by a name, parentheses (), and a colon. The body is indented. To run it, you β€œcall” it by writing its name with parentheses.

πŸŽ›οΈ A function is like a machine

def greet(): β†’ greet() β†’ prints "Hello!"
def greet():
print("Hello, coder! πŸ‘‹")

# Now use it β€” as many times as you like!
greet()
greet()
🧠 Two steps: 1️⃣ Define the function with def. 2️⃣ Call it by typing its name. Defining it alone does nothing!
2

Parameters: giving your function input πŸ“₯

Functions get more powerful when you pass them parameters β€” values inside the parentheses. Think of them as the ingredients you feed the machine.

πŸ• A pizza function

make_pizza("cheese") β†’ "Your cheese pizza is ready! πŸ•"
make_pizza("pepperoni") β†’ "Your pepperoni pizza is ready! πŸ•"
def make_pizza(topping):
print(f"Your {topping} pizza is ready! πŸ•")

make_pizza("cheese")
make_pizza("pepperoni")
πŸ”’ Multiple parameters: separate them with commas β€” def add(a, b): and call it like add(3, 5).
3

Return values: getting results back πŸ“€

print() just shows something on the screen. But return sends a value back to whoever called the function β€” so you can store it in a variable, do maths with it, or use it again. This is how functions become building blocks.

def add(a, b):
return a + b

result = add(3, 5)
print(result) # 8
print(add(10, 20) * 2) # 60 β€” return value can be used directly!

πŸ”„ print vs return

print(x) β†’ shows it, then throws it away
return x β†’ hands it back to use later
⚠️ Heads up: return ends the function immediately. Any code after it inside the function won’t run.
4

Default & keyword arguments πŸŽ›οΈ

You can give a parameter a default value so it’s optional. You can also pass arguments by name (keyword) instead of just position β€” great for clarity and skipping optional ones.

def greet(name, greeting="Hello"):
print(f"{greeting}, {name}!")

greet("Alex") # Hello, Alex!
greet("Sam", greeting="Yo") # Yo, Sam!
greet(greeting="Hi", name="Max") # Hi, Max! (keyword order doesn't matter)
πŸ“ Rule: Parameters with defaults must come after ones without. You can’t write def f(a=1, b): β€” that’s an error.
5

Scope: where variables live 🏠

Variables created inside a function only exist there β€” this is called local scope. Variables outside are global. This keeps functions neat and prevents accidents.

def secret():
message = "I'm local!"
print(message)

secret() # I'm local!
# print(message) ← ❌ ERROR: message only exists inside the function

# Global variables are visible inside functions (but avoid changing them!)
score = 10
def show_score():
print(f"Score: {score}")

show_score() # Score: 10
πŸ’‘ Best practice: Pass data in as parameters and return results out. Avoid changing global variables from inside functions β€” it gets confusing fast!
6

Modules & imports: using tools from others πŸ“¦

A module is a file full of Python code β€” functions, variables, everything. Python comes with loads of built-in modules, and you can use them with import. This is modularity: building big programs from small pieces.

Import styleHow you use it
import mathmath.sqrt(16)
from math import sqrtsqrt(16)
import random as rndrnd.randint(1, 6)
import math
import random

print(math.sqrt(81)) # 9.0
print(random.randint(1, 6)) # roll a dice 🎲 (1–6)
print(random.choice(["rock", "paper", "scissors"]))
🧩 Make your own module: create mytools.py with functions, then in another file use import mytools β€” instant reuse across projects!
7

Put it all together πŸ—οΈ

Here’s a small program that uses functions, parameters, defaults, returns, and a module β€” a mini dice game!

import random

def roll_dice(sides=6):
return random.randint(1, sides)

def play_round(player, sides=6):
roll = roll_dice(sides)
print(f"{player} rolled a {roll} 🎲")
return roll

p1 = play_round("Alex")
p2 = play_round("Sam")

if p1 > p2:
print("Alex wins! πŸ†")
elif p2 > p1:
print("Sam wins! πŸ†")
else:
print("It's a tie! 🀝")
πŸ§ͺ Try it! Save as dice_game.py and run it a few times. Change the default to sides=20 and watch the rolls get bigger!

πŸ† Mini challenge: Your personal toolkit

Create a file called toolkit.py with three useful functions, then call them in the same file. Each should take parameters and return a value:

  • area_rectangle(length, width) β†’ returns the area.
  • celsius_to_fahrenheit(c) β†’ returns the temperature in Β°F (formula: c * 9/5 + 32).
  • is_even(number) β†’ returns True if even, False if odd.
# Starter code – complete the functions!
def area_rectangle(length, width):
# your code here
pass

def celsius_to_fahrenheit(c):
# your code here
pass

def is_even(number):
# your code here
pass

# Test them!
print(area_rectangle(5, 3)) # should be 15
print(celsius_to_fahrenheit(25)) # should be 77.0
print(is_even(8)) # should be True

🎁 Bonus: add a default parameter β€” e.g. area_rectangle(length, width=10) β€” so you can call it with just one value.

πŸ§‘β€πŸ« Remember: def creates a function, return sends a value back, and import grabs tools from modules. Small functions = happy coders!

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’