Bird
Raised Fist0
Rustprogramming~10 mins

Function declaration in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Function declaration
Start
↓
Declare function with name and parameters
↓
Function body: code inside braces
↓
Optional return statement
↓
Function ready to be called
↓
End
This flow shows how a function is declared: start with the name and parameters, write the code inside braces, optionally return a value, then the function is ready to use.
Execution Sample
Rust
fn greet(name: &str) {
    println!("Hello, {}!", name);
}

fn main() {
    greet("Alice");
}
This code declares a function named greet that takes a name and prints a greeting, then calls it from main.
Execution Table
StepActionEvaluationResult
1Declare function greet with parameter name of type &strFunction greet createdFunction greet ready
2Enter main functionStart mainmain started
3Call greet("Alice")Call greet with name = "Alice"greet runs
4Inside greet: println!("Hello, {}!", name)Print greetingOutput: Hello, Alice!
5greet endsReturn to mainBack to main
6main endsProgram endsProgram finished
💡 Program ends after main finishes
Variable Tracker
VariableStartAfter greet callFinal
nameundefined"Alice"undefined
Key Moments - 3 Insights
Why do we write the function code inside curly braces {}?
The curly braces define the function body where the code runs. See execution_table step 4 where greet's code inside braces executes.
What does the parameter name: &str mean?
It means the function expects a text slice called name. In step 3, greet is called with "Alice" passed as name.
Why is there no return statement in greet?
Because greet returns nothing (unit type). It just prints. The function ends after printing as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'name' when greet is called?
A"Alice"
B"Bob"
Cundefined
Dempty string
💡 Hint
Check execution_table row 3 where greet is called with name = "Alice".
At which step does the program print the greeting?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at execution_table row 4 where println! outputs the greeting.
If we add a return type to greet, which step would change?
AStep 1 only
BStep 3 only
CStep 4 and 5
DNo steps change
💡 Hint
Returning a value affects the function body and return steps (rows 4 and 5).
Concept Snapshot
fn function_name(parameters) {
    // code inside braces
    // optional return
}

- Declare function with fn keyword
- Parameters have name and type
- Code runs inside braces {}
- Use println! to print
- Functions can return values or nothing
Full Transcript
This example shows how to declare a function in Rust. We start with fn, then the function name and parameters with types. The code to run is inside curly braces. Here, greet takes a name and prints a greeting. The main function calls greet with "Alice". The execution table shows each step: declaring greet, starting main, calling greet, printing, and ending. The variable tracker shows the parameter 'name' gets the value "Alice" during the call. Key moments explain why braces are needed, what parameters mean, and why no return is used here. The quiz checks understanding of parameter values, when printing happens, and what changes if a return is added.

Practice

(1/5)
1. What keyword is used to declare a function in Rust?
easy
A. fn
B. function
C. def
D. func

Solution

  1. Step 1: Recall Rust function syntax

    In Rust, functions are declared using the keyword fn.
  2. Step 2: Compare options with Rust syntax

    Only fn matches the correct Rust keyword for function declaration.
  3. Final Answer:

    fn -> Option A
  4. Quick Check:

    Function declaration keyword = fn [OK]
Hint: Remember Rust uses 'fn' to start functions [OK]
Common Mistakes:
  • Using 'function' instead of 'fn'
  • Confusing with Python's 'def'
  • Using 'func' which is not Rust syntax
2. Which of the following is the correct syntax to declare a function named greet that takes no parameters and returns nothing in Rust?
easy
A. def greet() {}
B. fn greet() {}
C. fn greet {}
D. function greet() {}

Solution

  1. Step 1: Check function declaration syntax

    In Rust, a function with no parameters and no return value is declared as fn name() {}.
  2. Step 2: Validate each option

    fn greet() {} matches Rust syntax exactly. Options A and B use keywords from other languages. fn greet {} misses parentheses.
  3. Final Answer:

    fn greet() {} -> Option B
  4. Quick Check:

    Empty parameter list requires () in Rust [OK]
Hint: Functions always need () even if no parameters [OK]
Common Mistakes:
  • Omitting parentheses
  • Using other language keywords
  • Adding unnecessary keywords
3. What will be the output of this Rust code?
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    println!("{}", add(3, 4));
}
medium
A. 7
B. 34
C. a + b
D. Error: missing return

Solution

  1. Step 1: Understand the add function

    The function takes two integers and returns their sum using expression a + b.
  2. Step 2: Trace the main function call

    Calling add(3, 4) returns 7, which is printed by println!.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Sum of 3 and 4 is 7, printed as output [OK]
Common Mistakes:
  • Thinking 'a + b' prints as string
  • Concatenating numbers as strings
  • Assuming missing return error
4. Identify the error in this Rust function declaration:
fn multiply(x i32, y: i32) -> i32 {
    x * y
}
medium
A. Function name is invalid
B. Missing return type
C. Missing colon after parameter x
D. Missing semicolon after multiplication

Solution

  1. Step 1: Check parameter syntax

    Each parameter must have a colon between name and type. Here, x i32 misses the colon.
  2. Step 2: Verify other parts

    Return type is present, function name is valid, and semicolon is not needed after expression in Rust.
  3. Final Answer:

    Missing colon after parameter x -> Option C
  4. Quick Check:

    Parameters need ':' between name and type [OK]
Hint: Parameters need ':' between name and type [OK]
Common Mistakes:
  • Omitting colon in parameters
  • Adding unnecessary semicolons
  • Confusing return type syntax
5. How would you declare a Rust function named is_even that takes an integer parameter and returns a boolean indicating if the number is even?
hard
A. fn is_even(n: i32) -> boolean { n % 2 == 0 }
B. fn is_even(n: i32) { return n % 2 == 0; }
C. fn is_even(n: int) -> bool { n % 2 = 0 }
D. fn is_even(n: i32) -> bool { n % 2 == 0 }

Solution

  1. Step 1: Check parameter and return types

    Parameter type must be i32 and return type bool in Rust.
  2. Step 2: Validate function body syntax

    Expression n % 2 == 0 correctly returns a boolean. fn is_even(n: i32) -> bool { n % 2 == 0 } uses correct syntax and types.
  3. Final Answer:

    fn is_even(n: i32) -> bool { n % 2 == 0 } -> Option D
  4. Quick Check:

    Use i32 and bool with correct expression [OK]
Hint: Use '-> bool' and 'n % 2 == 0' for even check [OK]
Common Mistakes:
  • Using wrong type names like int or boolean
  • Omitting return type
  • Using '=' instead of '==' in condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard