Understanding Subroutines & Nested Iteration
Understanding Subroutines & Nested Iteration
What do you already know about subroutines and loops?
Learning Objective
At the end of the lesson you will understand subroutines and nested iteration in Python, and be able to write and use them confidently in your own programmes.
What Are Subroutines?
Subroutines are blocks of code designed to perform a specific task. In Python, these are often called functions.
Why Use Subroutines?
They help organise code, make it easier to read and maintain, and allow code re-use.
Defining a Subroutine in Python
To create a subroutine (function), use the 'def' keyword.
For example:
def greet():
print('Hello!')
Activity: Gap Fill Code
Let's go to a Python website and complete a function gap fill together. Replace the missing keywords.
Parameters and Arguments
Subroutines can take inputs called parameters. Arguments are the values you give these parameters when you call the subroutine.
Example: A Subroutine with Parameters
Example:
def add(a, b):
return a + b
Calling a Subroutine
Invoke a subroutine by writing its name followed by parentheses. Example: greet() or add(2,3).
When calling a subroutine, the subroutine that is being called should be named outside of its own subroutine, however if you are calling a subroutine different to the one you are currently coding inside of, it must be a different subroutine.
Quick Quiz 1
What is the keyword to define a subroutine in Python?
A) func B) def C) sub D) call
Quick Quiz 1
What is the keyword to define a subroutine in Python?
A) func B) def C) sub D) call
Nested Iteration: The Basics
Nested iteration means having one loop inside another. It is useful for tasks like processing grids or matrices.
Syntax of Nested Loops
for i in range(3): for j in range(2): print(i, j)
Example: Printing a Pattern
Nested loops can be used to print patterns, such as stars or multiplication tables.
Teacher Demo: Writing Nested Loops
Let's visit a Python site and create a nested loop to print a series of names.
Combining Subroutines and Nested Loops
Often, subroutines include nested loops for complex tasks, such as working with matrices or processing data.
Activity: Gap Fill with Nested Loops
Try a gap-fill code example on a Python website, filling in the missing parts of a function with a nested loop.
Quick Quiz 2
What is nested iteration?
A) A loop inside another loop B) Two programmes run together C) Code outside functions D) An unused loop
Quick Quiz 2
What is nested iteration?
A) A loop inside another loop B) Two programmes run together C) Code outside functions D) An unused loop
Common Mistakes with Subroutines
Missing indentation, not returning values, or forgetting to call the subroutine can cause errors.
Imagine this was your program:
def main():
print("Hello!")
print("How was your day?")
Since the subroutine has not been called, this would not return anything,
There would also be no error message to tell you that you forgot to call it.
Common Mistakes with Subroutines
Now imagine this was your program:
def main():
print("Hello!")
print("How was your day?")
main()
Since the subroutine has been called, this would return something.
Common Mistakes with Subroutines
Now imagine this was your program:
def main():
print("Hello!")
print("How was your day?")
main()
Since the subroutine has been called, this would return something.
Turn and talk:
What would this program return?
Debugging Tips
Test subroutines one at a time, print variables, and check indentation when using nested loops.
Quick Quiz 3
Which of these is a valid subroutine name? A) 1loop B) loop_1 C) *add D) for
Question
Teacher's Note:
The following 3 slides are follow-up thinkers. They are optional.
Write down 3 things you learned in this lesson.
Write down 2 things you want to know more about.
Ask the person next to you one question about something you haven't quite understood yet.