📖 Scenario: You are creating a simple calculator program that adds two numbers. You will learn how to use function parameters to pass values into a function.
🎯 Goal: Build a Rust program with a function called add that takes two numbers as parameters and returns their sum. Then call this function and print the result.
📋 What You'll Learn
Create a function named add with two parameters named a and b of type i32.
The function add should return the sum of a and b as i32.
Call the function add with the arguments 5 and 7.
Print the result of the function call.
💡 Why This Matters
🌍 Real World
Functions with parameters are used everywhere in programming to reuse code and perform tasks with different inputs, like calculators, games, and web apps.
💼 Career
Understanding how to define and use function parameters is essential for writing clean, reusable code in any software development job.
Progress0 / 4 steps
1
Create the add function with parameters
Write a function named add that takes two parameters: a and b, both of type i32. The function should return an i32. Do not write the function body yet.
Rust
Hint
Define the function header with parameters and return type. Use a + b as the return expression.
2
Call the add function with arguments
Create a variable named result and assign it the value returned by calling the function add with arguments 5 and 7.
Rust
Hint
Use let result = add(5, 7); to call the function and store the result.
3
Print the result
Write a println! macro to print the value of the variable result.
Rust
Hint
Use println!("{}", result); to display the sum.
4
Complete program with main function
Wrap all the code inside a fn main() function so the program runs correctly.
Rust
Hint
Put the function call and print statement inside fn main() { ... }.
Practice
(1/5)
1. What is the purpose of function parameters in Rust?
easy
A. To send data into functions so they can use it
B. To define the return type of a function
C. To declare variables inside a function
D. To create loops inside functions
Solution
Step 1: Understand what parameters do
Parameters allow functions to receive input data when called.
Step 2: Identify the correct purpose
Sending data into functions makes them flexible and reusable.
Final Answer:
To send data into functions so they can use it -> Option A
Quick Check:
Function parameters = input data [OK]
Hint: Parameters pass data into functions for flexibility [OK]
Which call correctly uses the function parameters to calculate the area of a 5.0 by 3.0 rectangle?
hard
A. calculate_area(5, 3)
B. calculate_area(5.0, 3.0)
C. calculate_area("5", "3")
D. calculate_area(width=5.0, height=3.0)
Solution
Step 1: Check parameter types
The function expects two f64 (floating-point) parameters.
Step 2: Evaluate each call
calculate_area(5, 3) uses integer literals, which Rust automatically coerces to f64 (implicit conversion). calculate_area(5.0, 3.0) uses floating-point literals matching the f64 parameter type exactly. calculate_area("5", "3") uses strings, type mismatch. calculate_area(width=5.0, height=3.0) uses named arguments, which Rust does not support.
Final Answer:
calculate_area(5.0, 3.0) -> Option B
Quick Check:
Use correct types and order for parameters [OK]
Hint: Match argument types exactly to parameter types [OK]
Common Mistakes:
Using integers instead of floats without suffix
Passing strings instead of numbers
Trying named arguments (not supported in Rust)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.