1. What is a Function?
A function is a reusable block of code that performs a specific task.
Instead of writing the same code repeatedly, we can create a function and call it whenever required.
Example
Output:
Understanding the Code
fnis the keyword used to define a function.say_hellois the function name.()contains function parameters, if any.{}contains the function body.say_hello()calls the function.main()is the entry point of a Rust program.
2. Function Syntax
To call a function:
A function does not execute simply because it has been defined. It must be called.
3. Function with Parameters
Parameters allow us to pass information to a function.
Example
Output:
Understanding Parameters
In this example:
nameis the parameter.&stris its data type.
When we call:
"Hemant" is the argument passed to the function.
Difference:
Parameter | Argument |
|---|---|
Variable defined in a function declaration | Actual value passed when calling a function |
|
|
4. Function with Multiple Parameters
A function can accept multiple parameters.
Example
Output:
Notice that each parameter has its own data type.
5. Functions with Return Values
Sometimes a function needs to calculate something and send the result back to the place where it was called.
For this, Rust provides return values.
We specify the return type using ->.
Syntax
Example
Output:
Here:
-> i32means the function returns an integer.10 + 20is the final expression.- The result is returned automatically because there is no semicolon after the expression.
6. Using the return Keyword
Rust also supports the return keyword to return a value explicitly.
Example
Output:
Both approaches are valid:
And:
The first approach is commonly used for simple functions.
7. Function with Parameters and Return Value
This is one of the most common ways to write functions.
Example: Calculate Square
Output:
Example: Calculate Total Price
Output:
Here, quantity as f64 converts the integer quantity into a floating-point value.
8. Function with Multiple Return Values
Rust can return multiple values together using a tuple.
Example
Output:
We can also use tuple destructuring:
Here, .0 accesses the first tuple value and .1 accesses the second.
9. Returning a Boolean Value
A function can also return true or false.
Example: Check Even or Odd
Output:
The function returns a Boolean value because its return type is bool.
10. Function Calling Another Function
One function can call another function.
Example
Output:
This helps us divide a large program into smaller, reusable functions.
11. Scope of Variables in Functions
A variable declared inside a function is generally accessible only within that function's scope.
Example
Output:
The variable number belongs to first_function(). We cannot directly access it from main().
For example, this will produce a compilation error:
To share information between functions, pass it through parameters or return values.
12. Important Rules of Rust Functions
- Use the
fnkeyword to define a function. - Function names usually use snake_case, such as
calculate_total. - Every function parameter must have a type.
- Use
->to specify the return type. - The returned value must match the declared return type.
- Rust uses the final expression of a function as its return value when there is no explicit
return. - A semicolon changes an expression into a statement, so it does not return that expression's value.
13. Quick Reference
Concept | Example |
|---|---|
Function definition |
|
Function call |
|
Parameter |
|
Multiple parameters |
|
Return type |
|
Explicit return |
|
Boolean return |
|
Tuple return |
|