Computer Science Grade 5 20 min

Functions for Math: Performing Calculations

Students will use functions to perform mathematical calculations and simplify code.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define a custom function that accepts one or more numbers as parameters. Use math operators (+, -, *, /) inside a function to perform a calculation. Use a 'return' statement to send a calculated value back from a function. Call a math function with specific arguments and store its returned value in a variable. Combine multiple math functions to solve a more complex problem. Explain the difference between a parameter (the placeholder) and an argument (the real value). Have you ever wondered how a video game instantly knows your new score after you collect a coin? 🎮 Let's learn how to build the code that does that math! In this lesson, we will learn how to create special blocks of code called functions that act like mini-calculators. These...
2

Key Concepts & Vocabulary

TermDefinitionExample FunctionA named block of reusable code that performs a specific task. For math, it's like creating your own custom calculator button.A function named `addNumbers` that takes two numbers and adds them together. ParameterA placeholder variable inside the function's definition. It's like an empty box waiting for a value to be put inside.In `function add(num1, num2)`, `num1` and `num2` are parameters. ArgumentThe actual value that you send to a function when you call it. It's the number you put into the empty box (the parameter).When you call `add(5, 3)`, the numbers `5` and `3` are the arguments. Return ValueThe value or answer that a function sends back after it has finished its calculation.The function `add(5, 3)` would have a return value of `8`....
3

Core Syntax & Patterns

Defining a Math Function function functionName(parameter1, parameter2) { // code to do math let result = parameter1 * parameter2; return result; } Use the `function` keyword, give it a name, and list its parameters in parentheses. Inside the curly braces `{}`, do your calculation and use `return` to send back the answer. Calling a Function and Storing the Result let myAnswer = functionName(argument1, argument2); To use a function, type its name and provide the actual values (arguments) in the parentheses. Use a variable with `=` to catch and save the return value.

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
A function `calculateTotal(price, taxRate)` is needed to find the final cost of an item. The `taxRate` is a decimal (e.g., 0.20 for 20%). The calculation should be `price + (price * taxRate)`. What would `calculateTotal(50, 0.20)` return?
A.50.20
B.10
C.60
D.70
Challenging
The number 9 in binary is `1001`. A function `getBit(number, position)` gets the binary digit at a certain position, counting from the right starting at 0. For example, `getBit(9, 0)` would be 1. What would `getBit(9, 2)` return?
A.1
B.0
C.2
D.9
Challenging
Analyze this recursive function: `def mystery(n): if n <= 1: return 1; else: return n * mystery(n - 1);`. This is a factorial function. What is the result of `mystery(4)`?
A.4
B.10
C.24
D.1

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.