Playing with Functions in Rust
Tags: rust, tutorial, learning, programming
Published: 2020-07-19 10:19:14

*This post published on my blog [before](https://aligoren.com/playing-with-functions-in-rust/)*

-----

Hi everyone. Before that, I wrote a post called [A Trip to Data Types in Rust](https://dev.to/aligoren/a-trip-to-data-types-in-rust-41dd).

Today I'll try to explain functions in Rust.

Before starting, I'll create a project with cargo;

```bash
cargo new functions

cd functions
```

## Introduction

Functions are common in the Rust programming language. We're already creating a function for each program. We're calling it `main`. Yes, `main` is the most important function in the Rust programming language. This function is the entry point of our programs.

### What is A Function

![What is A Function](https://dev-to-uploads.s3.amazonaws.com/i/2x9zz7gl4bh5b19a6m0e.png)

A function is a group of statements. Their main purpose performing tasks you specified. A function can take parameters and can return them. Statements in the functions run in a block called function body or function scope.

## Creating a Function

![Creating a Function](https://dev-to-uploads.s3.amazonaws.com/i/apwj0j6688he1j19wahv.png)

We're using the `function` keyword to create functions in JavaScript, PHP, and some other programming languages. Go use the `func` keyword to define a function.

We'll use the `fn` keyword to define a new function in Rust. This is our very first function;

```rust
fn hello() {
  println!("Hello everyone!");
}
```

Yes, we've created our first function. When you call it, it will say **Hello everyone!**. Rust uses a snake case style for function and variable names. But it allows you about any type of style.

```rust
fn hello_world() {
  println!("Hello world!");
}
```

Let's call these functions in the main function.

```rust
fn main() {
  hello();
  hello_world();
}
```

## Functions Parameters

![Functions Parameters](https://dev-to-uploads.s3.amazonaws.com/i/jr0ad1yjexi3dwnqymtb.png)

Parameters are inputs. They can be variable or directly passed values. All parameters must have a type. Because anonymous parameters removed in the 2018 edition. Let's create a function with parameters;

```rust
fn user(age: u8, name: String) {
    println!("I'm {} years old. My name is {}", age, name);
}

fn main() {
  user(27, "Ali".to_string());
}

// I'm 27 years old. My name is Ali
```

Functions can take the types you defined. For example, it can be a struct like a type. But this is another topic. We'll see it later.

## Function Bodies

> Function bodies are made up of a series of statements optionally ending in an expression. So far, we’ve only covered functions without an ending expression, but you have seen an expression as part of a statement. Because Rust is an expression-based language, this is an important distinction to understand. Other languages don’t have the same distinctions, so let’s look at what statements and expressions are and how their differences affect the bodies of functions.

For example `let age: u8 = 10;` is a statement. Because `let` keyword is a statement. Functions definitions are also statements in Rust. Statements don't return values. So, if you try to write a statement like below, you'll get an error.

```rust
fn main() {
  let age = (let my_age = 12);
}
```

In this case, the compiler expects an expression but it found a statement. This isn't allowed Rust. Let's see an example of expression;

```rust
fn main() {
  let born_year = 1993;

  let year = {
    let born_year = 1990;
    born_year + 1
  };

  println!("Born Year {}", year);
}
```

The `year` variable holds an expression. In this expression, `let born_year = 1993;` statement won't change. Because year expression is a different scope. I'll try to explain like that;

```rust
let born_year = 1993; // IDENTITY NUMBER: 11131211113
```

and

```rust
let year = {
  let born_year = 1990; // IDENTITY NUMBER: 0192585111
}
```

So, these are different. They don't know each other. We didn't use a semi-colon in this part;

```rust
born_year + 1
```

This tells the compiler to return a value.

## Return Values

![Return Values](https://dev-to-uploads.s3.amazonaws.com/i/ybcb4xc394auphjgyw8h.png)

In all programming languages, functions can return values. In some dynamically programming languages don't have to annotate the type of the return value. But you should annotate type of the return value in Rust.

```rust
fn sum_two_values(number1: i32, number2: i32) -> i32 {
  return number1 + number2;
}
```

### Return a Value without Return Keyword

You can return values without a `return` keyword. Because the last line doesn't end a semi-colon.

```rust
fn sum_two_values(number1: i32, number2: i32) -> i32 {
  number1 + number2
}
```

```rust
fn main() {
  println!("Sum Two Values {}", sum_two_values(3, 5));
}

// Sum Two Values 8
```

That's all for now. In the future, we'll see functions deeply. Thanks for reading!

## Resources

- [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html)