CHAPTER 09
Beginner
Functions in Rust
Updated: May 18, 2026
5 min read
# CHAPTER 9
Functions in Rust
1. Chapter Introduction
If you write a 5,000-line application entirely insidefn main(), it will be unreadable and impossible to test. Functions allow you to break your code into small, modular, reusable blocks. They take inputs, perform logic, and optionally return outputs. In this chapter, we will learn how to declare functions, pass data into them, and return data back to the caller.
2. Learning Objectives
By the end of this chapter, you will be able to:- Declare basic functions.
- Pass parameters into functions.
- Understand why type signatures are mandatory for parameters.
- Return values from functions.
- Apply the concept of "implicit returns" using expressions.
3. Declaring a Basic Function
You define a function using thefn keyword, followed by the function name (in snake_case), parentheses (), and a block of code {}.
Functions can be defined before or after main. Rust doesn't care about the order.
rust
4. Parameters and Arguments
You can pass data *into* a function. The variables in the definition are called Parameters. Rule: In function signatures, you MUST explicitly declare the type of each parameter. The compiler will not infer it.
rust
5. Returning Values
If a function performs a calculation, it can hand the result *back* to the code that called it. You must declare the return type using an arrow-> after the parameters.
rust
6. Implicit Returns (The Rust Way)
Using thereturn keyword is perfectly valid, but it is not the "Rust Way".
Because Rust is expression-based, the final expression in a block is automatically returned if you omit the semicolon!
rust
*If you accidentally add a semicolon (x + y;), the line becomes a statement, returns nothing (), and the compiler will throw an error because it expected an i32!*
7. Early Returns
If you need to return a value *before* the very last line (for example, inside anif statement to handle an error early), you must use the return keyword.
rust
8. Common Mistakes
-
Forgetting Parameter Types: Writing
fn calculate(a, b)instead offn calculate(a: i32, b: i32). The compiler strictly requires type annotations in function signatures.
-
Accidental Semicolons on Returns: Writing
x + y;at the end of a function that is supposed to return ani32. The compiler will say "expectedi32, found(). Consider removing this semicolon."
9. Best Practices
- Single Responsibility Principle: A function should do exactly ONE thing. If a function connects to a database, calculates user age, and prints to the screen, break it up into three smaller functions.
-
Use Implicit Returns: Drop the
returnkeyword at the end of functions to write clean, idiomatic Rust.
10. Exercises
-
1.
Write a function
iseven(num: i32) -> bool.
-
2.
Have it return
trueif the number is even, andfalseif odd. Use an implicit return!
-
3.
Call it from
mainand print the result.
11. MCQs with Answers
Question 1
What keyword is used to declare a function in Rust?
Question 2
What is the standard naming convention for functions in Rust?
x: i32)
Answer: b) No, you MUST explicitly declare them.
Question 4
What syntax is used to declare the return type of a function?
Question 5
How do you return a value idiomatically at the end of a function in Rust?
x + y;)?
a) It compiles normally b) It converts the expression into a statement, causing it to return () instead of the expected value, resulting in a compiler error.
Answer: b) It returns () and causes a compiler error.
Question 7
When MUST you use the return keyword?
main b) No, Rust handles the order automatically
Answer: b) No.
Question 9
If a function does not have a -> return type declared, what does it return by default?
12. Interview Questions
-
Q: Explain the difference between
return x;and just writingxat the end of a function. Which is preferred in Rust?
- Q: Why does Rust force you to write type signatures for parameters when it has type inference for regular variables? (Answer: Because function signatures act as a contract for the API, ensuring external code respects the rules without the compiler having to guess).