Bird
Raised Fist0
Rustprogramming~10 mins

Function parameters in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Function parameters
Define function with parameters
↓
Call function with arguments
↓
Parameters receive argument values
↓
Function body uses parameters
↓
Function returns result or ends
This flow shows how a function is defined with parameters, called with arguments, and how parameters receive those values to be used inside the function.
Execution Sample
Rust
fn greet(name: &str) {
    println!("Hello, {}!", name);
}

fn main() {
    greet("Alice");
}
This code defines a function 'greet' with one parameter 'name' and calls it with the argument "Alice" to print a greeting.
Execution Table
StepActionParameter 'name' ValueOutput
1Function 'greet' is defined with parameter 'name'N/AN/A
2Function 'main' starts and calls 'greet' with argument "Alice"N/AN/A
3'greet' starts, parameter 'name' receives value "Alice""Alice"N/A
4Print statement executes using 'name'"Alice"Hello, Alice!
5'greet' function endsN/AN/A
6'main' function endsN/AN/A
💡 Execution stops after 'main' function ends.
Variable Tracker
VariableStartAfter callAfter printFinal
nameundefined"Alice""Alice"undefined
Key Moments - 2 Insights
Why does the parameter 'name' have the value "Alice" inside the function?
Because when 'greet' is called (see step 2 in execution_table), the argument "Alice" is passed and assigned to the parameter 'name' (step 3).
Does the parameter 'name' keep its value after the function ends?
No, after the function ends (step 5), 'name' goes out of scope and is no longer defined, as shown in variable_tracker final column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what value does the parameter 'name' hold?
A"Alice"
B"Bob"
Cundefined
Dnull
💡 Hint
Check the 'Parameter 'name' Value' column at step 3 in execution_table.
At which step does the function print the greeting message?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Output' column in execution_table to find when 'Hello, Alice!' is printed.
If we call greet("Bob") instead of greet("Alice"), what changes in the execution_table?
AOutput changes but parameter 'name' stays "Alice"
BParameter 'name' value changes to "Bob" and output changes to 'Hello, Bob!'
CNo changes at all
DParameter 'name' value changes but output stays the same
💡 Hint
Parameter 'name' receives the argument value, so both parameter value and output depend on the argument passed.
Concept Snapshot
fn function_name(parameter: Type) {
    // function body uses parameter
}

- Parameters receive values when function is called.
- Arguments are passed to parameters.
- Parameters exist only during function execution.
- Use parameters inside function to work with passed data.
Full Transcript
This visual execution shows how function parameters work in Rust. First, a function is defined with a parameter named 'name'. When the function is called with an argument like "Alice", the parameter 'name' receives this value. Inside the function, the parameter is used to print a greeting message. After the function finishes, the parameter no longer exists. The execution table traces each step, showing how the parameter value changes and when output is produced. The variable tracker confirms the parameter's lifecycle. Key moments clarify common confusions about parameter values and scope. The quiz tests understanding by asking about parameter values at specific steps and effects of changing arguments.

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

  1. Step 1: Understand what parameters do

    Parameters allow functions to receive input data when called.
  2. Step 2: Identify the correct purpose

    Sending data into functions makes them flexible and reusable.
  3. Final Answer:

    To send data into functions so they can use it -> Option A
  4. Quick Check:

    Function parameters = input data [OK]
Hint: Parameters pass data into functions for flexibility [OK]
Common Mistakes:
  • Confusing parameters with return types
  • Thinking parameters declare variables inside functions
  • Mixing parameters with control flow like loops
2. Which of the following is the correct way to declare a function with one parameter named num of type i32 in Rust?
easy
A. fn example(num) -> i32 {}
B. fn example(i32 num) {}
C. fn example(num: i32) {}
D. fn example(num: int) {}

Solution

  1. Step 1: Recall Rust function parameter syntax

    Parameters are declared as name: type, e.g., num: i32.
  2. Step 2: Check each option

    fn example(num: i32) {} matches correct syntax. fn example(i32 num) {} reverses order. fn example(num) -> i32 {} misses type. fn example(num: int) {} uses wrong type name.
  3. Final Answer:

    fn example(num: i32) {} -> Option C
  4. Quick Check:

    Parameter syntax = name: type [OK]
Hint: Use name: type for parameters in Rust functions [OK]
Common Mistakes:
  • Swapping type and name order
  • Omitting the parameter type
  • Using incorrect type names like 'int'
3. What will be the output of this Rust code?
fn greet(name: &str) {
    println!("Hello, {}!", name);
}

fn main() {
    greet("Alice");
}
medium
A. Hello, Alice!
B. Hello, name!
C. Hello, &str!
D. Compilation error

Solution

  1. Step 1: Understand the function call and parameter

    The function greet takes a string slice &str and prints a greeting with the given name.
  2. Step 2: Trace the function call in main

    Calling greet("Alice") passes "Alice" as the parameter, so it prints "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    Function prints greeting with passed name [OK]
Hint: Parameters replace placeholders in print statements [OK]
Common Mistakes:
  • Printing the parameter name instead of its value
  • Confusing &str with variable name
  • Expecting a compilation error due to &str
4. Identify the error in this Rust function declaration:
fn add(a i32, b: i32) -> i32 {
    a + b
}
medium
A. Parameters should be declared as 'i32 a' and 'i32 b'
B. Missing return type declaration
C. Function body missing braces
D. Missing colon between parameter name and type for 'a'

Solution

  1. Step 1: Check parameter syntax

    Each parameter must have a colon between name and type, e.g., a: i32.
  2. Step 2: Identify the error

    Parameter a i32 is missing the colon, causing a syntax error.
  3. Final Answer:

    Missing colon between parameter name and type for 'a' -> Option D
  4. Quick Check:

    Parameter syntax requires colon [OK]
Hint: Always put colon between parameter name and type [OK]
Common Mistakes:
  • Omitting colon between name and type
  • Confusing parameter order syntax
  • Forgetting return type after ->
5. Given this function:
fn calculate_area(width: f64, height: f64) -> f64 {
    width * height
}

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

  1. Step 1: Check parameter types

    The function expects two f64 (floating-point) parameters.
  2. 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.
  3. Final Answer:

    calculate_area(5.0, 3.0) -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard