Rust
December 2, 2026 5 min read

function in rust

HS
HEMANT SINGH
Technical Writer & Educator

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

fn say_hello() {
println!("Hello, Rust!");
}

fn main() {
say_hello();
}

Output:

Hello, Rust!

Understanding the Code

  1. fn is the keyword used to define a function.
  2. say_hello is the function name.
  3. () contains function parameters, if any.
  4. {} contains the function body.
  5. say_hello() calls the function.
  6. main() is the entry point of a Rust program.

2. Function Syntax

fn function_name() {
// Code to execute
}

To call a function:

function_name();

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

fn greet(name: &str) {
println!("Hello, {}!", name);
}

fn main() {
greet("Hemant");
greet("Rahul");
}

Output:

Hello, Hemant!
Hello, Rahul!

Understanding Parameters

In this example:

fn greet(name: &str)
  1. name is the parameter.
  2. &str is its data type.

When we call:

greet("Hemant");

"Hemant" is the argument passed to the function.

Difference:


Parameter

Argument

Variable defined in a function declaration

Actual value passed when calling a function

name: &str

"Hemant"

4. Function with Multiple Parameters

A function can accept multiple parameters.

Example

fn add(a: i32, b: i32) {
let result = a + b;
println!("Addition: {}", result);
}

fn main() {
add(10, 20);
add(50, 30);
}

Output:

Addition: 30
Addition: 80

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

fn function_name() -> data_type {
// Return a value
}

Example

fn add() -> i32 {
10 + 20
}

fn main() {
let result = add();

println!("Result: {}", result);
}

Output:

Result: 30

Here:

  1. -> i32 means the function returns an integer.
  2. 10 + 20 is the final expression.
  3. 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

fn multiply(a: i32, b: i32) -> i32 {
return a * b;
}

fn main() {
let result = multiply(5, 4);

println!("Multiplication: {}", result);
}

Output:

Multiplication: 20

Both approaches are valid:

fn add(a: i32, b: i32) -> i32 {
a + b
}

And:

fn add(a: i32, b: i32) -> i32 {
return a + b;
}

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

fn square(number: i32) -> i32 {
number * number
}

fn main() {
let result = square(5);

println!("Square: {}", result);
}

Output:

Square: 25

Example: Calculate Total Price

fn calculate_total(price: f64, quantity: u32) -> f64 {
price * quantity as f64
}

fn main() {
let total = calculate_total(250.0, 4);

println!("Total Price: {}", total);
}

Output:

Total Price: 1000

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

fn calculate(a: i32, b: i32) -> (i32, i32) {
let sum = a + b;
let difference = a - b;

(sum, difference)
}

fn main() {
let result = calculate(20, 10);

println!("Sum: {}", result.0);
println!("Difference: {}", result.1);
}

Output:

Sum: 30
Difference: 10

We can also use tuple destructuring:

fn main() {
let (sum, difference) = calculate(20, 10);

println!("Sum: {}", sum);
println!("Difference: {}", difference);
}

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

fn is_even(number: i32) -> bool {
number % 2 == 0
}

fn main() {
let result = is_even(10);

println!("Is Even: {}", result);
}

Output:

Is Even: true

The function returns a Boolean value because its return type is bool.

10. Function Calling Another Function

One function can call another function.

Example

fn add(a: i32, b: i32) -> i32 {
a + b
}

fn display_result() {
let result = add(10, 20);

println!("Result: {}", result);
}

fn main() {
display_result();
}

Output:

Result: 30

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

fn first_function() {
let number = 100;

println!("Number: {}", number);
}

fn main() {
first_function();
}

Output:

Number: 100

The variable number belongs to first_function(). We cannot directly access it from main().

For example, this will produce a compilation error:

fn first_function() {
let number = 100;
}

fn main() {
println!("{}", number);
}

To share information between functions, pass it through parameters or return values.

12. Important Rules of Rust Functions

  1. Use the fn keyword to define a function.
  2. Function names usually use snake_case, such as calculate_total.
  3. Every function parameter must have a type.
  4. Use -> to specify the return type.
  5. The returned value must match the declared return type.
  6. Rust uses the final expression of a function as its return value when there is no explicit return.
  7. A semicolon changes an expression into a statement, so it does not return that expression's value.

13. Quick Reference


Concept

Example

Function definition

fn greet() {}

Function call

greet();

Parameter

fn greet(name: &str) {}

Multiple parameters

fn add(a: i32, b: i32) {}

Return type

fn add() -> i32 {}

Explicit return

return result;

Boolean return

fn check() -> bool {}

Tuple return

fn calculate() -> (i32, i32) {}