Computer Science
Grade 5
20 min
Creating Simple Functions: Defining a Block of Code
Students will practice creating simple functions with basic commands.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a simple function using a name and a block of code.
Identify the 'definition' and the 'call' of a function.
Explain why functions are useful for avoiding repeated code.
Create a function that combines multiple commands to perform a single task.
Call a function they have created multiple times within a program.
Debug a program where a function is defined but not called.
Ever get tired of writing the same instructions over and over again? 😴 What if you could package a set of instructions, give it a cool name, and use it with one simple command?
In this lesson, we'll learn how to create our own custom code blocks called 'functions'. Functions let us group commands together, making our code shorter, cleaner, and muc...
2
Key Concepts & Vocabulary
TermDefinitionExample
FunctionA named block of code that performs a specific task. You can run this block of code whenever you want, just by using its name.A function named `draw_square` could contain all the 'move' and 'turn' commands needed to draw a square.
Define a FunctionThe process of creating a function. This is where you give the function a name and list the instructions (the block of code) that it will follow.Using a block like `define draw_star { ... }` to create a new function called `draw_star`.
Call a FunctionThe act of telling the computer to run the code inside a function you have already defined. If you don't 'call' a function, its code will never run.Placing a single block that says `draw_star` in your main program to make the computer...
3
Core Syntax & Patterns
The 'Define' Pattern
define function_name {
// command 1
// command 2
// ...
}
Use this pattern to create a new function. Choose a descriptive name for `function_name` and place all the commands you want to group together inside the curly braces `{}`.
The 'Call' Pattern
function_name()
Use this pattern to run a function you have already defined. Whenever the computer sees the function's name followed by parentheses, it will execute the block of code inside that function's definition.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
In a game, collecting a power-up does three things: 1) score increases by 50, 2) a 'power-up!' sound plays, 3) the character glows for 3 seconds. This can happen on any of the game's 10 levels. What is the most efficient way to code this behavior using a function?
A.Copy and paste the three lines of code everywhere a power-up can be collected.
B.Create one function `handle_power_up()` that contains the three lines of code, and call this function whenever a power-up is collected.
C.Create three separate functions: `add_score()`, `play_sound()`, and `start_glow()`, and call all three every time.
D.Write the code once in a special file and hope the computer knows when to use it.
Challenging
You have a function `draw_star_row()` that prints a single row of five stars (`*****`). How could you use this function inside a loop to draw a 5x5 square of stars?
A.Call `draw_star_row()` and then multiply the result by 5.
B.You can't; you must rewrite the code to draw a square without the function.
C.Put the definition of `draw_star_row()` inside a loop that runs 5 times.
D.Create a loop that runs 5 times, and inside the loop, call the `draw_star_row()` function.
Challenging
A robot is supposed to do a victory dance when `score` is 100, but instead it does a sad dance. The code for the victory dance is correctly written inside a function called `do_victory_dance()`. Where is the most likely location of the bug?
A.Inside the `do_victory_dance()` function; the dance moves are wrong.
B.The computer's speaker is broken, so the music is sad.
C.In the `if` statement that checks the score; it's probably calling the wrong function when the condition is met.
D.The variable named `score` is spelled wrong.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free