Computer Science
Grade 5
20 min
Functions with Parameters: Passing Information
Students will learn how to pass parameters (inputs) to functions to make them more versatile.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a function that accepts at least one parameter.
Explain the difference between a parameter and an argument.
Call a function and pass a specific value (argument) to it.
Write a function that uses a parameter to change its behavior, like drawing a square of a specific size.
Create a function that accepts multiple parameters in the correct order.
Debug simple errors related to missing or incorrect arguments in a function call.
Have you ever ordered a pizza? 🍕 You don't just say 'I want a pizza,' you give specific information like 'a large pizza with pepperoni!' How do we give specific instructions to our computer programs?
In this lesson, you'll learn how to create super-smart functions that can accept specific instruct...
2
Key Concepts & Vocabulary
TermDefinitionExample
FunctionA named block of code that performs a specific task. You can 'call' it by name whenever you need it.A function named `sayHello` that prints 'Hello!' to the screen.
ParameterA special variable that acts as a placeholder inside a function definition. It's like a blank space in a recipe waiting for an ingredient.In `FUNCTION greet(name)`, the parameter is `name`. It's waiting to be told a specific name.
ArgumentThe actual value or information you 'pass' to a function when you call it. It fills in the blank left by the parameter.When you call `greet('Alex')`, the argument is the string 'Alex'. It fills the `name` parameter.
Function CallThe line of code that tells the program to run a function.`drawCircl...
3
Core Syntax & Patterns
Defining a Function with a Parameter
FUNCTION functionName(parameterName) {
// code that uses parameterName
}
When you create a function, you declare its parameters in the parentheses. This creates a placeholder variable that can only be used inside that function.
Calling a Function with an Argument
functionName(argumentValue);
To run the function, you call it by its name and put the actual value (the argument) you want to use inside the parentheses. This value gets assigned to the parameter inside the function.
Multiple Parameters Pattern
FUNCTION functionName(param1, param2) { ... }
CALL functionName(arg1, arg2);
Functions can have multiple parameters, separated by commas. When you call the function, you must provide arguments for each parameter, and the order...
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
This function draws a custom robot. To create a robot named "Bolt" that is 100 units tall and is colored "gold", what is the correct function call? `function createRobot(name, height, color) { ... }`
A.createRobot("Bolt", 100, "gold")
B.createRobot(100, "gold", "Bolt")
C.createRobot(name, height, color)
D.createRobot("Bolt", "gold", 100)
Challenging
You see this repetitive code in a program to draw a line of trees. Which function is the BEST and most flexible way to replace these lines? `drawTree(50, 100); drawTree(75, 100); drawTree(100, 100);`
A.`function drawForest(x1, x2, x3, y) { ... }`
B.`function drawThreeTreesAtY100() { ... }`
C.`function drawTreeRow(startX, y, count, spacing) { ... }`
D.`function drawTree(x, y) { ... }` (This function already exists)
Challenging
Carefully trace this code. What is the final number printed by the last line? `let totalApples = 10; function eatApples(numToEat) { numToEat = numToEat - 1; print("Ate one!"); } eatApples(totalApples); print(totalApples);`
A.9
B.1
C.10
D.An error will occur.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free