What are Rust Functions?

 What are Rust Functions?

What are Rust Functions?

Description

  • The “fn” keyword allows us to declare new functions.
  • Rust code uses snake case because the conventional style for function and variable names..
  • All letters are lowercase and underscores separate the words in snake case.

Example

fn main() {

println!(“Hello, world!”);

another_function();

}

fn another_function() {

println!(“Another function.”);

}

Result:

Hello, world

Another function.

Note: The Rust doesn’t care where you define your functions after or before the main function

Function Parameters

  • Function parameters are the special variables that are part of a function’s signature.
  • We can provide it with concrete values for those parameters when a function has parameters.
  • Technically, the concrete values are called arguments
  • We can use both i.e parameter or argument in casual conversation.
  • While calling a function, function’s parameter value h as to be set.
  • fn main ( ) {}
  • println!(“The value of x is: {}”, }
  • The value of x is: 5.
  • We must declare the type of each parameter in function’s signature.
  • Example
  • fn main ( ) {

    another_function(5);

    }

    fn another_function(x: i32) {

    println!(“The value of x is: {}”,

    x);

    }

    Result: The value of x is: 5

We must declare the type of each parameter in function’s signature.

Statements and Expressions

  • Rust is an expression-based language
  • The Function bodies are made up of series of statements optionally ending in an expression.
  • Statements are instructions that perform some action and don’t return a worth .
  • Expressions evaluate to a resulting value.
  • The statement is to creating a variable and assigning a value to it with the let keyword.let y = 6;
  • }
  • fn main( ) {
  • The let y = 6 statement doesn’t return a worth
  • Note: Statement contains (;) at its end.
  • Consider a simple math operation, such as 5 + 6, which is an expression that evaluates to the value 11.5
  • }
  • fn five() i32 {
  • Expressions can be part of statements
  • Note: Expression does not contain (;) at its end.fn main() {let y = {x +1 println!(“The value of y is:} 
  • The value of y is: 4
  • Note: x+1 is an expression without semicolon. If we give a semicolon to the end of an expression then we turn it into a statement, which would then not return a value.
  • Example

    fn main() {

    let x = 5;

    let y = {

    let x = 3;

    x + 1

    };

    println!(“The value of y is:

    { }”, y);

    }

  • Result:
  • (“The value of y is:}

    The value of y is: 4

    Note: x+1 is an expression without semicolon. If we include a semicolon to the end of an expression then we turn it into a statement, which will then not return a value.

FUNCTION WITH RETURN VALUE

  • Functions may return values to the code that calls them. We don’t name return values, but we do declare their type after an arrow (      ).fn five() i32 {}let x = five();x);Result:Note: We can’t use semicolon(;) after 5 because it is an expression.
  • Example
  • fn five() i32 {

    5

    }

    fn main() {

    let x = five();

    println!(“The value of x is: {}”,

    x);

    }

  • Result: The value of x is: 5

     Note: We can’t use semicolon(;) after 5 because it is an expression.

Leave a Comment