Computer Science Grade 5 20 min

What is a Function?: Introducing Reusable Blocks

Students will learn the concept of functions as reusable blocks of code.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a function is in programming. Explain why functions are useful for making code shorter and easier to read. Identify the two main parts of using a function: defining it and calling it. Create a simple function that performs a specific task, like drawing a shape. Use a function multiple times in a program to avoid repeating code. Modify an existing program to use functions instead of repeated code blocks. Ever wish you could teach a robot a new trick and have it remember that trick forever? 🤖 That's exactly what functions do for our code! In this lesson, we'll discover 'functions,' which are like special instruction manuals for our computer. We'll learn how to write a set of instructions once and then use it over and o...
2

Key Concepts & Vocabulary

TermDefinitionExample FunctionA named block of code that performs a specific task. You can run this block of code whenever you need it just by calling its name.A function named `drawStar` that contains all the commands to draw a five-pointed star. Define a FunctionThe process of creating a function, giving it a name, and writing the instructions it will follow inside.Writing the code: `function drawStar() { ...code to draw a star... }` Call a FunctionThe act of telling the program to run the code that is inside a function.Typing `drawStar();` in your program to make a star appear on the screen. Reusable CodeCode that can be used multiple times in a program without having to be rewritten.Instead of writing the star-drawing code 5 times, you define the `drawStar` function once and then call...
3

Core Syntax & Patterns

Defining a Function Pattern function functionName() { // Code to run goes here } Use the `function` keyword, give your function a descriptive name (like a verb, e.g., `moveForward`), and put the instructions you want to reuse inside the curly braces `{}`. Calling a Function Pattern functionName(); To run the code inside your function, just type its name followed by parentheses `()`. You can call it as many times as you want, anywhere in your program after it has been defined.

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 see a long program that draws a robot. The code for the robot's left arm is identical to the code for the right arm. The code for the left leg is identical to the right leg. What is the best way to 'refactor' (improve the design of) this code?
A.Create a `drawArm()` function and a `drawLeg()` function, then call each twice
B.Put all the arm and leg code into one giant `drawLimbs()` function
C.Leave the code as it is, because it already works
D.Use variables instead of functions to represent the arms and legs
Challenging
You have two functions: `getTax(price)` which returns the tax amount, and `displayTotal(totalPrice)` which prints to the screen. How would you calculate and display the final price for a $50 item, using a variable `itemPrice`?
A.displayTotal(itemPrice)
B.displayTotal(getTax(itemPrice))
C.displayTotal(itemPrice + getTax(itemPrice))
D.getTax(displayTotal(itemPrice))
Challenging
A programmer writes a function `findSmaller(a, b)` that is supposed to return the smaller of two numbers. The code is: `if (a > b) { return a; } else { return b; }`. What is the bug in this function?
A.The function should use a loop, not an `if` statement
B.The logic is reversed; it returns the larger number
C.The function needs a third parameter, `c`
D.The `return` keyword is spelled incorrectly

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.