Login

Please fill in your details to login.





lesson 3.8.4 passing information with parameters

Using parameters and arguments to build flexible, smart programs in Python.



image

Welcome back, coders! Last time, you learned how to bundle code into proceduresI have no idea what this means to tidy up your programs. But there was a catch; your
draw_square()
function could only draw one specific square. If you wanted a bigger one, or a red one, you were stuck! Today, we are going to upgrade your skills to the level of a professional Software DeveloperI have no idea what this means. You will learn how to send information into your subroutines using parametersI have no idea what this means. By the end of this lesson, you won't just be writing code; you'll be building flexible, reusable tools that can handle any job you throw at them. Let's get flexible!

Learning Outcomes
The Building Blocks (Factual Knowledge)
Define a parameterI have no idea what this means as a variable declared in the function definition that acts as a placeholder.
Define an argumentI have no idea what this means as the actual value passed to the function when it is called.
State that parameters allow a single block of code to process different data values.

The Connections and Theories (Conceptual Knowledge)
Explain how using parameters allows for generalisationI have no idea what this means, turning specific solutions into general ones.
Analyse the relationship between the order of arguments in a function call and the parameters in the definition.
Explain that using parameters reduces code duplication by allowing the same logic to be reused for different scenarios.

The Skills and Methods (Procedural Outcomes)
Create a procedure definition that includes one or more parameters.
Call a procedure passing valid arguments that match the defined parameters.
Refactor a rigid, repetitive script into a dynamic, parameterised program.

Digital Skill Focus: Use a REPLShort for 'Read, Evaluate, Print, Loop' - an immediate console used to interact with a programming language interpreter. (Read-Evaluate-Print Loop) to test simple expressions and function calls with different arguments.

The Problem with "Hard-Coded" Values


Imagine you have built a robot that makes sandwiches. You have programmed a procedure called
make_sandwich()
. It works perfectly, but it only makes cheese sandwiches. Every time you run it, you get cheese. Just cheese.

If you want a ham sandwich, you have to build a whole new robot called
make_ham_sandwich()
. If you want jam, you need a third robot. This is inefficient and, when changes need to be made (for instance, if the location of the fridge changed) unproductive.

In programming, we call this "hard-coding" - writing specific values (like "cheese" or "100 pixels") directly into your code. To fix this, we need to make our procedure flexible. We need to teach the robot to accept an order.

Enter Parameters and Arguments


To make our subroutines flexible, we use two special tools:

1
Parameters: These are the variables you put inside the brackets when you define the procedure. They act as placeholders for data that will arrive later.
2
Arguments: These are the actual values you put inside the brackets when you call the procedure. This is the data you are sending.

# DEFINITION
# 'filling' is the PARAMETER (The Placeholder)
def make_sandwich(filling):
    print("Getting bread...")
    print("Putting in " + filling)
    print("Sandwich is ready!")

# CALLS
# "Cheese" and "Ham" are the ARGUMENTS (The Actual Data)
make_sandwich("Cheese")
make_sandwich("Ham")


By changing just the argument, the same code does two different jobs!


time limit
Task 1 The Shape Shifter
Your teacher has just shown you how to turn a "dumb" procedure into a "smart" one using parameters. Now check your understanding.

Look at the following code snippet:

import turtle
screen = turtle.Screen()
t = turtle.Turtle()

def draw_line(length):
    t.pendown()
    t.forward(length)
    t.penup()

t.penup()
draw_line(50)
t.goto(0,10)
draw_line(100)

t.hideturtle()
turtle.done()


You might want to run this in Thonny for a little practice? Get ready to answer the following questions...

1
Which word is the Parameter?
2
Which numbers are the Arguments?
3
What would happen if you wrote
draw_line()
with nothing in the brackets?

Outcome: I can identify the difference between the data a function expects and the data it is given.

Checkpoint

Multiple Parameters


You aren't limited to just one piece of data. You can pass as many as you need, separated by commas. However, the order matters. If you define a procedure like this:

def login(username, password):
    # code here


And you call it like this:

login("secret123", "admin")


The computer doesn't know you mixed them up. It will try to use "secret123" as the username and "admin" as the password. This is a common logic errorSee Semantic/Logic error. Always match your arguments to your parameters (this is why documentation is important).


time limit
Task 2 The Polygon Painter
It's time to build the ultimate drawing machine. Instead of writing separate code for a triangle, a square, and a pentagon, you are going to write one procedure that can draw any of them!

Generalisation is a superpower for a Software DeveloperI have no idea what this means. It means solving a whole class of problems with one solution. Follow the steps carefully.

1
Get Organised!

Open Thonny and start a new script.
Save the script as polygon-painter.py.
Import turtle, create the screen and the turtle.

import turtle
screen = turtle.Screen()
t = turtle.Turtle()


2
Define the Procedure

1
Create a procedure called
draw_poly
that accepts three parameters:
sides
(How many sides?)
size
(How long is each side?)
colour
(What colour is it?)
2
Inside the procedure:
Set the turtle's colour using the
colour
parameter.
Drop the turtles pen.
Write a loop that repeats
sides
times.
Move forward by
size
.
Turn right by
360 / sides
.
Remember the lift the pen at the end.

def draw_poly(sides,size,colour):
    t.color(colour)
    t.pendown()
    for i in range(sides):
        t.forward(size)
        t.right(360 / sides)
    t.penup()


3
Test It!

Call your procedure three times with different arguments to draw a small red triangle, a medium blue square, and a large green hexagon. You also need to move the turtle in between drawing each shape or they will overlap.

draw_poly(3, 50, "red")    # Triangle
t.forward(75)
draw_poly(4, 100, "blue")  # Square
t.forward(150)
draw_poly(6, 30, "green")  # Hexagon


4
Gracefully destroy the turtle.

Finally, to cleanly close your script, gracefull destroy the turtle.

t.hideturtle()
turtle.done()


5
Run it!

Run your script - are you amazed?

Challenge
Can you change the procedure so that it draws a filled shape?
Can you add a separate 'stroke' and 'fill' parameter so that you can control the line and the fill colour separately?

Outcome: I have created a general-purpose procedure that adapts its behaviour based on the arguments I pass to it.

Checkpoint

image
Today you have learnt how to use parametersI have no idea what this means and argumentsI have no idea what this means to create flexible, generalised subroutines that can process different data values without rewriting code.

Out of Lesson Learning

Last modified: December 7th, 2025
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning