# Functions in Rust programming language

Rust ProgrammingServer Side ProgrammingProgramming

Functions are building blocks of code. They allow us to avoid writing similar code and also help in organizing large section of code into reusable components.

In Rust, functions can be found everywhere. Even function definitions are also statements in Rust.

One of the most important function in Rust is the main function, which is the entry point of any software or application.

We make use of the fn keyword to declare a function in Rust.

In Rust, the names of the functions make use of snake case as the conventional style. In snake case, all the letters of the word are in lower cases and the _ (underscore) are used to separate two words.

Consider a simple function in which we are printing “Hello, Tuts!”

### Filename: src/main.rs

#### Example

Live Demo

fn main() {
println!("Hello, Tuts!");
}

#### Output

The output of the above code is −

Hello, Tuts!

In the above code snippet, the function definition begins with a fn keyword followed by a pair of parentheses after the function name. The curly brackets are a part of the function body and they tell the compiler where the function body starts and where it ends.

## Invoking another function

Invoking/Calling a function in Rust is similar to its peer languages. We just need to enter its name followed by the parentheses.

Consider the following code snippet as an example.

### Filename: src/main.rs

#### Example

Live Demo

fn main() {
sample();
println!("Hello, Tuts!");
}
fn sample(){
println!("another function”);
}

#### Output

The output of the above code is −

   Compiling hello-tutorialspoint v0.1.0 (/Users/immukul/hello-tutorialspoint)
Finished dev [unoptimized + debuginfo] target(s) in 1.04s
Running target/debug/hello-tutorialspoint
another function
Hello, Tuts!

Rust doesn’t care where you define the function that you are calling, the only thing that matters is that it should be defined.

## Function Parameters

Parameters are values that we put inside the parentheses when we are defining a function. They are parts of function signatures.

Let’s slightly modify the above example and pass an argument to the function that we are invoking and see how it works.

### Filename: src/main.rs

#### Example

Live Demo

fn main() {
sample(5);
println!("Hello, Tuts!");
}
fn sample(x : i32){
println!("The value of x is : {} ", x);
}

#### Output

   Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running target/debug/hello-tutorialspoint
The value of x is : 5
Hello, Tuts!

We invoke the function by calling sample(5); which in turn calls the sample function which has a parameter named x. The parameter x is assigned a type i32. The println()! marco then prints the value of this parameter.

It should be noted that if there are multiple parameters defined in the function parentheses, then all these parameters must have a type declared.

Consider the below code snippet as reference.

### Filename: src/main.rs

#### Example

Live Demo

fn main() {
sample(5,7);
println!("Hello, Tuts!");
}
fn sample(x : i32, y : i32){
println!("The value of x is : {} ", x);
println!("The value of y is : {} ", y);
}

#### Output

   Compiling hello-tutorialspoint v0.1.0 (/Users/immukul/hello-tutorialspoint)
Finished dev [unoptimized + debuginfo] target(s) in 0.51s
Running target/debug/hello-tutorialspoint
The value of x is : 5
The value of y is : 7
Hello, Tuts

## Function with Return values

Functions can return values to the caller function. It is a very common use case of functions.

### Filename: src/main.rs

fn main() {
let z = sample(5);
println!("Output is: {}",z);
}
fn sample(x : i32) -> i32{
x + 1
}

Calling a function sample and passing 5 as an argument and then storing the returned value from that function to variable named z and finally printing the value using the println()! Marco.

It should be noted that if we change the function sample from an expression to a statement, then the Rust compiler will throw a compilation error.

### Filename: src/main.rs

#### Example

Live Demo

fn main() {
let z = sample(5);
println!("Output is: {}",z);
}
fn sample(x : i32) -> i32{
x + 1;
}

#### Output

Compiling hello-tutorialspoint v0.1.0 (/Users/immukul/hello-tutorialspoint)
error[E0308]: mismatched types
--> src/main.rs:6:23
|
6 | fn sample(x : i32) -> i32{
| ------ ^^^ expected i32, found ()
| |
| implicitly returns () as its body has no tail or return expression
7 | x + 1;
| - help: consider removing this semicolon
error: aborting due to previous error
For more information about this error, try rustc --explain E0308.
error: could not compile hello-tutorialspoint

This happens because the function sample is expected to return an i32 value, but inside it, a statement is present which doesn’t evaluate to a value, hence contradicting the function declaration. Simply remove the semicolon to convert the statement to an expression, and the code will work fine.

Published on 20-Feb-2021 07:09:13
Advertisements