Fatskills
Practice. Master. Repeat.
Study Guide: Computer Science Grade 5 Functions Reusing Code
Source: https://www.fatskills.com/5th-grade-science/chapter/computer-science-grade-5-functions-reusing-code

Computer Science Grade 5 Functions Reusing Code

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~6 min read

Grade 5 Computer Science Study Guide: Functions – Reusing Code


1. The Driving Question

"If you had to write the same set of instructions—like ‘draw a square’—over and over in your code, why would that be annoying, and how could you teach the computer to ‘remember’ those steps so you only have to write them once?" What if you could name those steps, like a recipe, and just say the name whenever you need them? That’s what functions do—but how do they actually work, and why does it matter?


2. The Core Idea – Built, Not Listed

Imagine you’re programming a robot to build a LEGO castle. Every time you want a tower, you have to tell the robot: 1. Place a 2x4 brick.
2. Stack a 2x2 brick on top.
3. Add a 1x2 brick as a flag.
4. Repeat for the next tower.

Writing those four steps every single time would take forever—and if you mess up one copy, the castle looks broken. Instead, you could teach the robot a new word: build_tower(). Now, whenever you say build_tower(), the robot runs those four steps automatically. That’s a function—a named set of instructions you can reuse.

Functions are like shortcuts in a video game. Instead of pressing "jump" (spacebar) + "forward" (W) + "attack" (mouse click) every time you want to do a combo, you could program a single button (like "F") to do all three at once. The game remembers the combo, and you just press "F" to trigger it.

Key Vocabulary:
- Function: A named block of code that performs a specific task.
Example: make_smoothie() could include steps like "add banana," "pour milk," and "blend for 30 seconds"—instead of writing those steps every time you want a smoothie.
- Parameter: A piece of information you give to a function to customize it.
Example: draw_shape(sides=6) could tell the function to draw a hexagon instead of a square. The "6" is the parameter.
- Return Value: The result a function gives back after it runs.
Example: add_numbers(3, 5) might return 8—like a calculator showing the answer.
- Call: When you use a function’s name to run it.
Example: Writing build_tower() in your code is "calling" the function.


3. Assessment Translation (Grade 5 Classroom Focus)

How this appears in class:
- Exit Tickets: Short coding tasks where you write or fix a function (e.g., "Write a function called draw_triangle() that makes a triangle with 3 lines").
- Show-Your-Work Problems: You’re given a program with repeated code and asked to rewrite it using a function (e.g., "Replace the 3 copies of ‘move forward, turn left’ with a function called take_step()").
- Debugging Challenges: A program has a function that doesn’t work right (e.g., draw_square() only draws 3 sides). You fix it.

Proficient vs. Developing Responses:
| Proficient | Developing | |----------------|----------------| | Writes a function with a clear name and correct steps (e.g., draw_square() includes 4 turns and 4 moves). | Writes a function but misses steps (e.g., draw_square() only has 3 moves). | | Uses parameters to customize the function (e.g., draw_shape(4) for a square, draw_shape(3) for a triangle). | Hardcodes values (e.g., always draws a square, even if the task asks for a triangle). | | Explains why functions are useful (e.g., "I used a function so I didn’t have to repeat code"). | Just says "it’s shorter" without explaining reuse. |

Model Proficient Response:
Prompt: "Write a function called draw_stairs() that makes a robot draw 3 steps. Each step is 20 pixels wide and 10 pixels tall."


def draw_stairs():
for step in range(3):
move_forward(20) # width
turn_right()
move_forward(10) # height
turn_left()

Why it’s proficient: - The function has a clear name (draw_stairs).
- It uses a loop to avoid repeating code.
- The steps match the prompt (20 pixels wide, 10 tall).


4. Mistake Taxonomy

Mistake 1: Forgetting to Call the Function
Prompt: "Use your draw_square() function to draw 2 squares." Common Wrong Response:


def draw_square():
for side in range(4):
move_forward(50)
turn_right()

Why it loses credit: The function is defined but never called (e.g., missing draw_square() twice). The robot doesn’t run the code.
Correct Approach:


def draw_square():
for side in range(4):
move_forward(50)
turn_right() draw_square() # Call it once draw_square() # Call it again

Mistake 2: Hardcoding Instead of Using Parameters
Prompt: "Write a function called draw_rectangle(width, height) that draws a rectangle with any size." Common Wrong Response:


def draw_rectangle():
move_forward(30) # Hardcoded width
turn_right()
move_forward(10) # Hardcoded height

Why it loses credit: The function ignores the parameters (width and height) and always draws the same rectangle.
Correct Approach:


def draw_rectangle(width, height):
move_forward(width)
turn_right()
move_forward(height)
turn_right()
# ... (complete the rectangle)

Mistake 3: Mixing Up Function Definition and Call
Prompt: "Fix this code so it draws 5 circles using a function." Common Wrong Response:


def draw_circle():
for i in range(5): # Wrong: loop inside the function
draw_circle()

Why it loses credit: The loop is inside the function, so calling draw_circle() once draws 5 circles. The prompt wants the call to repeat, not the function.
Correct Approach:


def draw_circle():
# Code to draw one circle for i in range(5): # Loop outside the function
draw_circle()


5. Connection Layer

  1. Within Computer Science: Functions → Loops
    Why it matters: Loops repeat code in place, but functions let you reuse code anywhere in your program. Together, they make complex programs manageable (e.g., a game with 100 enemies, each using the same move() and attack() functions).

  2. Across Subjects: Functions → Math Equations
    Why it matters: A math function like f(x) = 2x + 1 is like a coding function—you give it an input (x=3), it runs the steps (2*3 + 1), and returns an output (7). Both are "black boxes" that transform inputs into outputs.

  3. Outside School: Functions → Fast-Food Drive-Thrus
    Why it matters: When you order a "Number 3" at a drive-thru, the cashier doesn’t recite the entire recipe for a burger, fries, and a drink. They just say "Number 3," and the kitchen calls the pre-set instructions (like a function). This is how restaurants save time—and how functions save coding time.


6. The Stretch Question

"If you could only use functions (no loops or if-statements), could you still write a program to draw a spiral? How?"

Pointer toward the answer: You’d need a function that draws one line of the spiral, then turns slightly, then calls itself to draw the next line. This is called recursion—a function that calls itself. It’s like a snake eating its own tail: the function keeps running until it hits a "stop" condition (e.g., after 100 lines). Recursion is powerful but can be tricky—what happens if the function never stops calling itself? (Hint: It’s like an infinite loop, and your program crashes!)



ADVERTISEMENT