Functions help organize code by breaking tasks into small steps. Understanding how function calls flow helps you follow what the program does step-by-step.
Function call flow in Rust
fn function_name(parameters) {
// code to run
}
function_name(arguments);Functions start with fn, followed by the name and parentheses.
You call a function by writing its name and giving any needed values inside parentheses.
greet() runs the code inside.fn greet() {
println!("Hello!");
}
greet();fn add(a: i32, b: i32) -> i32 {
a + b
}
let sum = add(3, 4);
println!("Sum is {}", sum);This program shows how functions call each other. main calls start, which calls middle, which calls end. The print statements show the order of calls and returns.
fn start() {
println!("Start function called");
middle();
println!("Back to start function");
}
fn middle() {
println!("Middle function called");
end();
}
fn end() {
println!("End function called");
}
fn main() {
println!("Program begins");
start();
println!("Program ends");
}When a function calls another, the program waits for the called function to finish before continuing.
Functions can call themselves (recursion), but be careful to avoid infinite loops.
Use clear function names to make the flow easier to understand.
Functions break code into small, reusable parts.
Calling a function runs its code and then returns to where it was called.
Understanding call flow helps you follow what your program does step-by-step.
Practice
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 AQuick Check:
Function call flow = run code then return [OK]
- Thinking function code is skipped
- Believing program stops after call
- Assuming function runs only if called twice
greet in Rust?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
Onlygreet();matches this pattern correctly.Final Answer:
greet(); -> Option BQuick Check:
Function call syntax = name + () [OK]
- Omitting parentheses when calling functions
- Using brackets [] instead of parentheses
- Using keywords like 'call' or 'function' incorrectly
fn add_one(x: i32) -> i32 {
x + 1
}
fn main() {
let result = add_one(5);
println!("{}", result);
}Solution
Step 1: Understand the function behavior
The functionadd_onetakes an integer and returns that integer plus one.Step 2: Trace the function call and output
Callingadd_one(5)returns 6, which is printed byprintln!.Final Answer:
6 -> Option CQuick Check:
5 + 1 = 6 [OK]
- Confusing input and output values
- Thinking semicolon causes error here
- Assuming function prints instead of returns
fn multiply(x: i32, y: i32) -> i32 {
x * y
}
fn main() {
let result = multiply(4);
println!("{}", result);
}Solution
Step 1: Check function definition parameters
The functionmultiplyrequires two arguments:xandy.Step 2: Check function call arguments
The callmultiply(4)provides only one argument, causing an error.Final Answer:
multiply is called with wrong number of arguments. -> Option DQuick Check:
Function call args must match definition [OK]
- Ignoring argument count mismatch
- Thinking missing return causes error here
- Assuming println! usage is wrong
fn first() {
println!("Start");
second();
println!("End");
}
fn second() {
println!("Middle");
}
fn main() {
first();
}What is the order of printed lines when
main runs?Solution
Step 1: Trace the call flow from main
Main callsfirst(), which prints "Start" then callssecond().Step 2: Follow nested function calls
second()prints "Middle" and returns. Thenfirst()prints "End".Final Answer:
Start\nMiddle\nEnd -> Option AQuick Check:
Call flow order = Start, Middle, End [OK]
- Ignoring nested function call order
- Assuming prints happen after all calls
- Mixing up print sequence
Ready to prove it?
Pass the test and this pattern moves unknown
