📖 Scenario: Imagine you are building a simple calculator program that adds two numbers. To keep the program organized, you will use functions that call each other.
🎯 Goal: You will create functions that call each other to add two numbers and display the result.
📋 What You'll Learn
Create a function named add that takes two i32 numbers and returns their sum.
Create a function named calculate that calls the add function with two numbers.
Create a main function that calls calculate and prints the result.
💡 Why This Matters
🌍 Real World
Functions help organize code into small reusable parts. This is useful in calculators, games, and many apps.
💼 Career
Understanding function calls is essential for writing clean, maintainable code in any programming job.
Progress0 / 4 steps
1
Create the add function
Write a function called add that takes two parameters named a and b of type i32 and returns their sum as i32.
Rust
Hint
Use fn to define a function. Return the sum using a + b.
2
Create the calculate function
Write a function called calculate that calls the add function with the numbers 5 and 7 and returns the result.
Rust
Hint
Call the add function inside calculate with 5 and 7.
3
Create the main function
Write the main function that calls the calculate function and stores the result in a variable named result.
Rust
Hint
Define main and call calculate(), saving the value in result.
4
Print the result
Add a line in the main function to print the text "The sum is: " followed by the value of result using println!.
Rust
Hint
Use println!("The sum is: {}", result); to print the message and the number.
Practice
(1/5)
1. What happens when a function is called in Rust?
easy
A. The program runs the function's code and then returns to the call point.
B. The program skips the function's code and continues after the call.
C. The function code runs only if called twice.
D. The program stops running after the function call.
Solution
Step 1: Understand function call behavior
When a function is called, the program jumps to the function's code and executes it.
Step 2: Return to the call point
After the function finishes, the program returns to the line after the call and continues running.
Final Answer:
The program runs the function's code and then returns to the call point. -> Option A
Quick Check:
Function call flow = run code then return [OK]
Hint: Function calls run code then return to caller [OK]
Common Mistakes:
Thinking function code is skipped
Believing program stops after call
Assuming function runs only if called twice
2. Which of these is the correct way to call a function named greet in Rust?
easy
A. call greet;
B. greet();
C. greet[];
D. function greet();
Solution
Step 1: Recall Rust function call syntax
Functions are called by writing their name followed by parentheses, even if no arguments are passed.
Step 2: Match the correct syntax
Only greet(); matches this pattern correctly.
Final Answer:
greet(); -> Option B
Quick Check:
Function call syntax = name + () [OK]
Hint: Use functionName() to call functions in Rust [OK]
Common Mistakes:
Omitting parentheses when calling functions
Using brackets [] instead of parentheses
Using keywords like 'call' or 'function' incorrectly
3. What will be the output of this Rust code?
fn add_one(x: i32) -> i32 {
x + 1
}
fn main() {
let result = add_one(5);
println!("{}", result);
}
medium
A. 5
B. Error: missing semicolon
C. 6
D. 0
Solution
Step 1: Understand the function behavior
The function add_one takes an integer and returns that integer plus one.
Step 2: Trace the function call and output
Calling add_one(5) returns 6, which is printed by println!.
Final Answer:
6 -> Option C
Quick Check:
5 + 1 = 6 [OK]
Hint: Follow function return values to find output [OK]
Common Mistakes:
Confusing input and output values
Thinking semicolon causes error here
Assuming function prints instead of returns
4. Identify the error in this Rust code snippet:
fn multiply(x: i32, y: i32) -> i32 {
x * y
}
fn main() {
let result = multiply(4);
println!("{}", result);
}
medium
A. Function multiply is missing a return statement.
B. Variable result is not declared.
C. println! macro is used incorrectly.
D. multiply is called with wrong number of arguments.
Solution
Step 1: Check function definition parameters
The function multiply requires two arguments: x and y.
Step 2: Check function call arguments
The call multiply(4) provides only one argument, causing an error.
Final Answer:
multiply is called with wrong number of arguments. -> Option D
Quick Check:
Function call args must match definition [OK]
Hint: Match function call arguments to definition [OK]