Rust/C2/Functions-and-Returning-value-from-function-using-Rust/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of the script: Functions and Returning Value from Functions

Author: Jayesh Katta Ramalingaiah

Domain Reviewer: Vigneshwer Dhinakaran

Novice Reviewer: Praveen S.

Keywords: Rust, cargo, function, rs


Visual Cue
Narration
Slide: Title Welcome to the spoken tutorial on “Functions and Returning Value from Functions in Rust”.
Slide:

Learning Objectives

In this tutorial, we will learn to:
  • Create a function
  • Create a parameterized function
  • Return a value from a function
Slide: System Specifications


This tutorial is recorded using:
  • Ubuntu Linux OS version 18.04
  • Rust version 1.47.0
  • Visual Studio Code version 1.45.0 (code editor)
Slide : Pre-requisites To practice this tutorial,
  • You should be familiar with compiling and running Rust files.
  • If not, please go through the prerequisite Rust tutorials on this website.
Slide: Code files
  • The file used in this tutorial is available in the Code files link on this tutorial page.
  • Pls download and extract the file.
  • Make a copy and then use it for practising.
Slide Functions:
  • Function is a block of organised code, used to perform a single related action.
  • Functions provide better modularity for our application and
  • High degree of code reusability
Only Narration Let us now take an example to understand functions better.
Press Ctrl+Alt+T keys Open the terminal by pressing Ctrl, Alt and T keys simultaneously on the keyboard.


Ensure that you have root permissions to run the commands.

Only Narration Here onwards, please remember to press the Enter key after typing each command.
[Terminal] Type:


cd Desktop/MyRustProject

[Enter]

Using cd command go to the Rust practice folder which we have created earlier.
[Terminal] Type:


cargo new functions [Enter]

Let us create a new project named functions.


Type the command as shown.

[Editor]


Welcome Page ->

Open Folder ->

functions

Open the created project by clicking on the Open folder link in the Welcome page.


Browse and locate the folder “functions”.


Then click on the OK button at the top right corner.

[Editor]

Click on functions

Under the EXPLORER section, expand the project folder “functions” by clicking on it.
[Editor] Expand src and click on main.rs Then expand src and open the main.rs file.
[Editor - main.rs] Type:


fn main(){

display();

}


fn display(){

println!(“Spoken Tutorial”);

}

In the editor, replace the code as shown.
[Editor] Highlight:

fn display()

Here, we are creating a new function named display.
[Editor] Highlight:


println!(“Spoken Tutorial”);

And, here we are printing the text Spoken Tutorials in the function.
[Editor] Highlight:


display();

To invoke the created function, we need to make a function call.


So, the display function is being called here.

Ctrl + S Save the file.
Switch to terminal Switch back to the terminal.
[Terminal] Type:


cd functions

[Enter]

The Cargo project is created now.


Navigate inside the Cargo project functions using the cd command.

[Terminal] Type:


cargo run [Enter]

Run the project.
[Terminal] Highlight:

Spoken Tutorial

We see the text Spoken Tutorial printed in the terminal.
Only narration So far, we have successfully written a basic function and have invoked it.


Now let us learn about parameterized function.

Switch to Editor Switch back to the editor.
[Editor - main.rs] Type:

fn main(){

display(1);

}


fn display(a:i32){

println!(“I’m getting {} from main function.”, a);

}

In the editor, replace the code as shown.
[Editor] Highlight:

fn display(a:i32)

Here, we are passing a parameter to the display function and we’ve named it as a.
[Editor] Highlight:


println!(“I’m getting {} from the main function.”, a);

Here we’re trying to print the value which is being passed as a parameter to the function.
[Editor] Highlight:


display(1);

As the function accepts a parameter, we’re passing an i32 value, say 1, here.
Ctrl + S Save the file.
Switch to terminal Switch back to the terminal.
[Terminal] Type:


cargo run [Enter]

And run the project.


[Terminal] Highlight:


I’m getting 1 from the main function.

Now we can see that the value 1 which is being passed, is printed.


Only narration So now, we have successfully written a parameterized function and have invoked it by passing a parameter.


Next let us learn about returning a value from a function.

Switch to Editor Switch back to the editor.
[Editor - main.rs] Type:

fn main(){

println!(“The result is {}”,sum(1,2));

}


fn sum(a:i32, b:i32) -> i32{

a+b

}

In the editor, replace the code as shown.
[Editor] Highlight:

fn sum(a:i32, b:i32) -> i32

Here, we are passing a and b as parameters to the sum function.


The function should return the added value.


So, we are mentioning the type of value which would be returned after the arrow.

[Editor] Highlight:

a+b

In the function body we are adding the two numbers which are being passed as parameters.
[Editor] Highlight:

sum(1,2));

As the function accepts 2 parameters, we are passing two values and the function returns the result.
[Editor] Highlight:


println!(“The result is {}”,sum(1,2));

}

Here, using the print statement we are printing it.


The returned value would be replaced by the format specifier.

Ctrl + S Save the file.
Switch to terminal Switch back to the terminal.
[Terminal] Type:


cargo run [Enter]

Run the project.


[Terminal] Highlight:

The result is 3

We can see the sum being returned by the function and the result being displayed.
Switch to Editor Switch back to the editor.
[Editor] Modify:


fn sum(a:i32, b:i32) -> i32{

a+b;

}

Now, add a semicolon at the end of b.
Ctrl + S Save the file.
Switch to terminal Switch back to the terminal.
[Terminal] Type:


cargo run [Enter]

Run the project.
[Terminal] Highlight:

Consider removing this semicolon

This time we get an error, Consider removing this semicolon.


This is because it is being considered as a statement and not as a return value.


In order to return the value directly without using the return keyword we need to remove the semicolon.

Switch to Editor Switch back to the editor.
[Editor] Modify:


fn sum(a:i32, b:i32) -> i32{

let z = a + b;

z*10

}

In the editor, replace the sum function as shown.
[Editor] Highlight:

let z = a + b;

Here we have created a variable z and assigned a + b to it.
[Editor] Highlight:

z*10

Here, we modify the variable and return the same.


Notice that there is no semicolon.


So the value will be returned correctly.

Ctrl + S Save the file.
Switch to terminal Switch back to the terminal
[Terminal] Type:


cargo run [Enter]

Run the project.


[Terminal] Highlight:

The result is 30

Now we can see the value being returned by the function and the result being displayed correctly.
Switch to Editor Switch back to the editor.
[Editor] Highlight:


let z = a + b;

z*10

Here though the code works fine it is not properly readable.


This is because we have a statement and value being returned.

[Editor] Modify:


fn sum(a:i32, b:i32) -> i32{

let z = a + b;

return z*10;

}

To make it readable, we will modify our function a little bit.


Add a return keyword before z*10 and add a semicolon after z*10

Ctrl + S Save the file.
Switch to terminal Switch back to the terminal
[Terminal] Type:


cargo run [Enter]

Run the project.


[Terminal] Highlight:

The result is 30

Now we can see the value being returned by the function and the result being displayed correctly.


So we have learnt to make the code readable.


We also learnt different ways to return a value from a function.

Only narration With this we have come to the end of this tutorial.


Let’s summarize.

Slide: Summary In this tutorial, we have learnt to:
  • Create a function
  • Create a parameterized function
  • Return a value from a function
Slide: Assignment As an assignment,
  • Go to the project folder rust-assignment
  • In the main.rs file
    • Create a function that accepts a number as a parameter
    • Print EVEN if the number is even
    • Else print ODD
  • Compile and execute the project.
  • Observe the output in the Terminal
Slide: About Spoken Tutorial Project
  • The video at the following link summarises the Spoken Tutorial project.
  • Please download and watch it
Slide: Spoken tutorial workshops
  • We conduct workshops using spoken tutorials and give certificates.
  • For more details, please write to us.
Slide: Forum questions Pls post your timed queries in this forum
Slide: Acknowledgement Spoken Tutorial Project is funded by Ministry of Education, Government of India
Slide: Thanks This is Jayesh signing off. Thank you for joining

Contributors and Content Editors

Kr.jayesh, Nancyvarkey, Pravin1389