Function parameters in Rust - Time & Space Complexity
When we use function parameters, we want to know how the time to run the function changes as the input changes.
We ask: How does the size or number of parameters affect the work done inside the function?
Analyze the time complexity of the following code snippet.
fn sum_elements(numbers: &[i32]) -> i32 {
let mut total = 0;
for &num in numbers {
total += num;
}
total
}
This function takes a list of numbers and adds them all together, returning the total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list.
- How many times: Once for every number in the input list.
As the list gets bigger, the function does more additions, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the size of the input list.
[X] Wrong: "Function parameters always make the function run in constant time."
[OK] Correct: The size of the input parameter matters. If the parameter is a list, the function might need to look at every item, so time grows with input size.
Understanding how function parameters affect time helps you explain your code clearly and shows you think about efficiency, a skill that is useful in many coding situations.
"What if the function took two lists and added their elements pairwise? How would the time complexity change?"
Practice
Solution
Step 1: Understand what parameters do
Parameters allow functions to receive input data when called.Step 2: Identify the correct purpose
Sending data into functions makes them flexible and reusable.Final Answer:
To send data into functions so they can use it -> Option AQuick Check:
Function parameters = input data [OK]
- Confusing parameters with return types
- Thinking parameters declare variables inside functions
- Mixing parameters with control flow like loops
num of type i32 in Rust?Solution
Step 1: Recall Rust function parameter syntax
Parameters are declared asname: type, e.g.,num: i32.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.Final Answer:
fn example(num: i32) {} -> Option CQuick Check:
Parameter syntax = name: type [OK]
- Swapping type and name order
- Omitting the parameter type
- Using incorrect type names like 'int'
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("Alice");
}Solution
Step 1: Understand the function call and parameter
The functiongreettakes a string slice&strand prints a greeting with the given name.Step 2: Trace the function call in
Callingmaingreet("Alice")passes "Alice" as the parameter, so it prints "Hello, Alice!".Final Answer:
Hello, Alice! -> Option AQuick Check:
Function prints greeting with passed name [OK]
- Printing the parameter name instead of its value
- Confusing &str with variable name
- Expecting a compilation error due to &str
fn add(a i32, b: i32) -> i32 {
a + b
}Solution
Step 1: Check parameter syntax
Each parameter must have a colon between name and type, e.g.,a: i32.Step 2: Identify the error
Parametera i32is missing the colon, causing a syntax error.Final Answer:
Missing colon between parameter name and type for 'a' -> Option DQuick Check:
Parameter syntax requires colon [OK]
- Omitting colon between name and type
- Confusing parameter order syntax
- Forgetting return type after ->
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?
Solution
Step 1: Check parameter types
The function expects twof64(floating-point) parameters.Step 2: Evaluate each call
calculate_area(5, 3) uses integer literals, which Rust automatically coerces tof64(implicit conversion). calculate_area(5.0, 3.0) uses floating-point literals matching thef64parameter 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.Final Answer:
calculate_area(5.0, 3.0) -> Option BQuick Check:
Use correct types and order for parameters [OK]
- Using integers instead of floats without suffix
- Passing strings instead of numbers
- Trying named arguments (not supported in Rust)
Ready to prove it?
Pass the test and this pattern moves unknown
