Functions
Functions allow programmers to organise code into reusable components. A function groups a set of related instructions under a single name. Once the function has been defined, it can be called whenever that task needs to be performed again.
Functions are important because they make programs shorter, clearer and easier to maintain. Most Python libraries are built around functions and methods that perform specialised tasks.
Creating a Simple Function
A function is defined using the def keyword. The
indented lines below the function definition form the function body.
# Define a simple function
def welcome():
print("Welcome to Python.")
print("Functions help organise code.")
print("This function prints three lines.")
# Call the function
welcome()
Functions with Parameters
Parameters allow information to be passed into a function. This makes the same function work with different inputs.
# A function with one parameter
def calculate_square(number):
result = number ** 2
print("Input:", number)
print("Square:", result)
calculate_square(5)
calculate_square(12)
Returning Values
Many functions return a value rather than printing directly. Returning values allows the output of one function to be used in later calculations.
# A function that returns an average
def average(a, b, c):
result = (a + b + c) / 3
return result
student_average = average(72, 81, 77)
print("Average mark:", student_average)
Recursive Functions
A recursive function is a function that calls itself. Recursion is useful when a problem can be broken down into smaller versions of the same problem. Every recursive function needs a base case, which stops the recursion, and a recursive case, which calls the function again.
# Recursive factorial function
def factorial(n):
# Base case: stop when n is zero
if n == 0:
return 1
# Recursive case: call the function again
return n * factorial(n - 1)
print(factorial(5))
Fibonacci Sequence
The Fibonacci sequence is a classic example of recursion. Each number is the sum of the two previous numbers. The first two values are usually defined as 0 and 1.
# Recursive Fibonacci function
def fibonacci(n):
# Base cases
if n <= 1:
return n
# Recursive case
return fibonacci(n - 1) + fibonacci(n - 2)
for i in range(10):
print(fibonacci(i))
Recursive Sum
A recursive sum adds a number to the sum of all smaller positive integers. This example shows how recursion can represent a mathematical definition directly.
# Add all numbers from 1 to n recursively
def recursive_sum(n):
# Base case
if n == 1:
return 1
# Recursive case
return n + recursive_sum(n - 1)
print(recursive_sum(10))
Recursion versus Loops
Many problems can be solved using either recursion or loops. Loops are often faster and use less memory, while recursion can be clearer when the problem is naturally recursive.
# Iterative version of factorial using a loop
def factorial_loop(n):
result = 1
for value in range(1, n + 1):
result = result * value
return result
print(factorial_loop(5))
In the next activity, we conclude the session and review the main concepts covered.