Computer Science
Grade 5
20 min
Calling Functions: Using Your Reusable Blocks
Students will learn how to call or execute functions in their programs.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a 'function call' is and how it executes a block of code.
Identify the function definition and the function call as separate parts of a program.
Explain why calling functions makes code shorter, more organized, and easier to read.
Predict the output of a program that calls the same function multiple times.
Write a program that calls pre-defined functions to solve a problem in the correct sequence.
Debug simple errors in function calls, such as spelling mistakes or missing parentheses.
Ever wish you could teach a robot a chore, give it a name like 'clean room', and then just say that one command to make it happen? 🤖 That's exactly what calling a function does in coding!
In this lesson, you'll learn the superpower...
2
Key Concepts & Vocabulary
TermDefinitionExample
FunctionA named block of code that performs a specific task. Think of it as a recipe with a name.A function named `drawStar` that contains all the `move` and `turn` commands needed to draw a star shape.
Function DefinitionThe part of the code where you create the function. You give it a name and write the step-by-step instructions inside it.The block of code that starts with `function drawStar() { ... }` and contains the drawing commands inside the curly braces.
Function CallA single line of code that tells the program to run the instructions inside a function.Typing `drawStar();` in your main program, which tells the computer to find the `drawStar` recipe and follow its instructions.
ReuseThe power to use a function many times in your program without having to copy...
3
Core Syntax & Patterns
The Calling Syntax
functionName()
To run, or 'call', a function, you must type its exact name followed by parentheses. The name is case-sensitive, so `myFunction` is different from `myfunction`.
Define Before You Call
A function must be defined before your program tries to call it.
The computer needs to know the instructions for a function *before* you ask it to perform them. It's best to put all your function definitions at the top of your program.
The 'Jump and Return' Flow
Main Program -> Function Call -> Jumps to Function Code -> Runs Function -> Returns to Main Program
Think of a function call as a side quest in a game. Your program pauses its main path, completes the side quest (the function), and then returns to the exact s...
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
Your program is supposed to draw a fence `|-|` but instead it draws `_-_`. Your code is below. Which function's DEFINITION is most likely broken?
`function drawFencePost() { ... }`
`function drawHorizontalLine() { ... }`
`drawFencePost();`
`drawHorizontalLine();`
`drawFencePost();`
A.The `drawHorizontalLine()` function is fine, but the `drawFencePost()` function is broken.
B.Both function definitions are broken.
C.The `drawFencePost()` function is fine, but the `drawHorizontalLine()` function is broken.
D.The function calls are in the wrong order.
Challenging
You have `drawStar()` which prints '*' and `drawDash()` which prints '-'. You want to create the pattern '*-*-*'. Which code block correctly produces this pattern?
A.`for (let i = 0; i < 2; i++) { drawStar(); drawDash(); } drawStar();`
B.`for (let i = 0; i < 3; i++) { drawStar(); drawDash(); }`
C.`drawStar(); for (let i = 0; i < 2; i++) { drawDash(); drawStar(); }`
D.`for (let i = 0; i < 5; i++) { drawStar(); }`
Challenging
A student's code `PrintMessage()` isn't working. The function is defined as `function printMessage()`. The student says, 'It must be broken because the computer doesn't know what a message is.' What is the flaw in the student's reasoning?
A.The student is correct; the computer doesn't understand English words.
B.The student forgot to put quotes around the function name.
C.The student should have named the function `function_print_message()`.
D.The student is ignoring the actual error, which is a case-sensitivity mismatch, not the meaning of the words.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free