Computer Science Grade 5 20 min

Functions for Drawing: Creating Patterns

Students will use functions to create reusable drawing patterns or shapes.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define a function that accepts one or more parameters to draw a shape. Explain the difference between a parameter (the placeholder) and an argument (the real value). Call a custom drawing function multiple times with different arguments. Use a loop with a variable to call a function repeatedly, creating a changing pattern. Combine multiple function calls to create a more complex drawing, like a scene. Debug simple errors in code that uses functions and loops to draw patterns. Have you ever noticed the repeating patterns on a snowflake, a honeycomb, or your favorite wallpaper? ❄️ We can teach a computer to be an artist and create amazing patterns just like those! In this lesson, you will learn how to create your own 'drawing recipes' called func...
2

Key Concepts & Vocabulary

TermDefinitionExample FunctionA named block of code that performs a specific task. Think of it as a recipe that you can use again and again.A function named `drawSquare` contains all the commands to draw one square. You can then just 'call' `drawSquare` instead of re-typing all the commands. ParameterA special variable that acts as a placeholder inside a function. It's like an empty box waiting for a value.In a function `drawSquare(size)`, the `size` is a parameter. It lets the function know it will receive a size value when it's called. ArgumentThe actual value you give to a function's parameter when you call it. It's the information you put in the box.When you call `drawSquare(50)`, the number `50` is the argument. It tells the function to make the square&#...
3

Core Syntax & Patterns

Defining a Function with a Parameter function functionName(parameter1, parameter2) { // code to run goes here } Use this structure to create a new 'recipe'. The `function` keyword starts it, you give it a name, and list the 'ingredient' placeholders (parameters) in parentheses. Calling a Function in a Loop for count from 1 to 10 { functionName(count * 5); } To create a changing pattern, place your function call inside a loop. You can use the loop's counter variable (like `count`) in a calculation to change the argument on each repetition.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You have code that draws a 5x5 grid of circles using a nested loop: `repeat 5 times (row) { repeat 5 times (col) { drawCircle() } }`. How could you change this to draw a right-angled triangle pattern instead of a full square grid?
A.Change the outer loop to `repeat 10 times`
B.In the inner loop, only draw a circle if `row` is an even number
C.Change the inner loop to `repeat row times` instead of `repeat 5 times`
D.Add a third nested loop inside the second one
Challenging
A function `drawBinarySquare(digit)` draws a black square for a 1 and a white square for a 0. If you want to draw the pattern for the number 6 (which is 110 in binary), what would the function calls be in the correct order?
A.drawBinarySquare(1); drawBinarySquare(1); drawBinarySquare(0);
B.drawBinarySquare(6);
C.drawBinarySquare(1); drawBinarySquare(0); drawBinarySquare(1);
D.drawBinarySquare(110);
Challenging
To draw an expanding spiral, you use a loop. Inside the loop, you move forward and then turn slightly. How must the `moveForward(distance)` command change on each step of the loop?
A.The distance moved should get a little bit longer with each loop repetition
B.The distance moved should stay exactly the same
C.The amount you turn should become zero after the first loop
D.The distance moved should get a little bit shorter with each loop repetition

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.