Concept Flow - Function call flow
Start main
Call function foo
Execute foo body
Return from foo
Continue main
End main
This flow shows how the main function calls another function, waits for it to finish, then continues.
fn foo() {
println!("Inside foo");
}
fn main() {
println!("Start main");
foo();
println!("End main");
}| Step | Function | Action | Output |
|---|---|---|---|
| 1 | main | Start main function | Start main |
| 2 | main | Call foo() | |
| 3 | foo | Enter foo function | Inside foo |
| 4 | foo | Return from foo | |
| 5 | main | Continue after foo() | End main |
| 6 | main | End main function |
| Variable | Start | After Step 2 | After Step 4 | Final |
|---|---|---|---|---|
| call_stack | main | main -> foo | main | empty |
Function call flow in Rust: - main() starts execution - main calls another function (e.g., foo()) - Execution jumps to called function - Called function runs and returns - Execution resumes in main after call - Call stack tracks active functions
greet in Rust?greet(); matches this pattern correctly.fn add_one(x: i32) -> i32 {
x + 1
}
fn main() {
let result = add_one(5);
println!("{}", result);
}add_one takes an integer and returns that integer plus one.add_one(5) returns 6, which is printed by println!.fn multiply(x: i32, y: i32) -> i32 {
x * y
}
fn main() {
let result = multiply(4);
println!("{}", result);
}multiply requires two arguments: x and y.multiply(4) provides only one argument, causing an error.fn first() {
println!("Start");
second();
println!("End");
}
fn second() {
println!("Middle");
}
fn main() {
first();
}main runs?first(), which prints "Start" then calls second().second() prints "Middle" and returns. Then first() prints "End".Pass the test and this pattern moves unknown