11-15-2025, 10:38 PM
Chapter 14 — Functions: Reusable Code Blocks
Functions allow you to bundle code into reusable, organised pieces — the foundation of real software.
A function is a block of code you can run whenever you need it.
Example:
Call it:
---
14.1 Why Functions Matter
Functions help you:
• avoid repeating code
• keep programs clean
• break big problems into smaller parts
• reuse logic anywhere
• make code easier to read and maintain
---
14.2 Creating Your First Function
Calling the function:
---
14.3 Functions With Parameters
Parameters are input values the function needs.
Call it:
Output:
---
14.4 Multiple Parameters
Call:
---
14.5 Functions That Return Values
Return sends a value BACK to the program.
Output:
---
14.6 return vs print
print()
• shows text to the user
• does NOT give data back to your program
return
• sends data back
• allows reuse
• allows storing values
Example:
---
14.7 Default Parameter Values
---
14.8 Keyword Arguments
You can specify parameters by name:
---
14.9 Scope — Local vs Global Variables
Local variables exist ONLY inside a function.
Global variables exist everywhere.
Avoid globals as much as possible — functions should be self-contained.
---
14.10 Function Design — Best Practices
✔ clear name
✔ does one job
✔ short
✔ returns a value if needed
✔ parameters instead of globals
---
14.11 Mini Project — Area Calculator
Write functions:
• area_rectangle(width, height)
• area_circle(radius)
Ask user which shape they want and calculate the answer.
---
14.12 Challenge — Bank Account Simulator
Write functions:
• deposit(balance, amount)
• withdraw(balance, amount)
• show_balance(balance)
Rules:
• withdrawing too much gives an error
• each function returns the new balance
• track balance using variables
Program flow example:
---
14.13 Chapter Summary
• functions organise code
• parameters = inputs
• return = output
• functions improve readability
• avoid global variables
• functions are essential for all real programs
Next:
Chapter 15 — Modules & Importing Code
This is where Python becomes *infinite* — you unlock tools written by millions of developers.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
Functions allow you to bundle code into reusable, organised pieces — the foundation of real software.
A function is a block of code you can run whenever you need it.
Example:
Code:
def greet():
print("Hello from Python!")Call it:
Code:
greet()---
14.1 Why Functions Matter
Functions help you:
• avoid repeating code
• keep programs clean
• break big problems into smaller parts
• reuse logic anywhere
• make code easier to read and maintain
---
14.2 Creating Your First Function
Code:
def welcome():
print("Welcome to the Lumin Archive!")Calling the function:
Code:
welcome()---
14.3 Functions With Parameters
Parameters are input values the function needs.
Code:
def greet(name):
print(f"Hello, {name}!")Call it:
Code:
greet("Mia")
greet("Johno")Output:
Code:
Hello, Mia!
Hello, Johno!---
14.4 Multiple Parameters
Code:
def add(a, b):
print(a + b)Call:
Code:
add(3, 7)---
14.5 Functions That Return Values
Return sends a value BACK to the program.
Code:
def multiply(a, b):
return a * b
result = multiply(4, 5)
print(result)Output:
Code:
20---
14.6 return vs print
print()
• shows text to the user
• does NOT give data back to your program
return
• sends data back
• allows reuse
• allows storing values
Example:
Code:
def square(n):
return n * n
value = square(6)
print(value)---
14.7 Default Parameter Values
Code:
def greet(name="friend"):
print(f"Hello, {name}!")Code:
greet() # Hello, friend!
greet("Lee") # Hello, Lee!---
14.8 Keyword Arguments
You can specify parameters by name:
Code:
def power(base, exp):
return base ** exp
print(power(exp=3, base=2))---
14.9 Scope — Local vs Global Variables
Local variables exist ONLY inside a function.
Code:
def test():
x = 10
print(x)Global variables exist everywhere.
Code:
score = 0 # global
def add_point():
global score
score += 1Avoid globals as much as possible — functions should be self-contained.
---
14.10 Function Design — Best Practices
✔ clear name
✔ does one job
✔ short
✔ returns a value if needed
✔ parameters instead of globals
---
14.11 Mini Project — Area Calculator
Write functions:
• area_rectangle(width, height)
• area_circle(radius)
Ask user which shape they want and calculate the answer.
---
14.12 Challenge — Bank Account Simulator
Write functions:
• deposit(balance, amount)
• withdraw(balance, amount)
• show_balance(balance)
Rules:
• withdrawing too much gives an error
• each function returns the new balance
• track balance using variables
Program flow example:
Code:
Balance: £100
Deposit £20 → £120
Withdraw £50 → £70 ---
14.13 Chapter Summary
• functions organise code
• parameters = inputs
• return = output
• functions improve readability
• avoid global variables
• functions are essential for all real programs
Next:
Chapter 15 — Modules & Importing Code
This is where Python becomes *infinite* — you unlock tools written by millions of developers.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
