Rust/C2/Functions-and-Returning-value-from-function-using-Rust/English
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
|
|
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:
|
Slide: System Specifications
|
This tutorial is recorded using:
|
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide Functions: |
|
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.
|
Only Narration | Here onwards, please remember to press the Enter key after typing each command. |
[Terminal] Type:
[Enter] |
Using cd command go to the Rust practice folder which we have created earlier. |
[Terminal] Type:
|
Let us create a new project named functions.
|
[Editor]
Open Folder -> functions |
Open the created project by clicking on the Open folder link in the Welcome page.
|
[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:
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:
|
And, here we are printing the text Spoken Tutorials in the function. |
[Editor] Highlight:
|
To invoke the created function, we need to make a function call.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
[Enter] |
The Cargo project is created now.
|
[Terminal] Type:
|
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.
|
Switch to Editor | Switch back to the editor. |
[Editor - main.rs] Type:
fn main(){ display(1); }
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:
|
Here we’re trying to print the value which is being passed as a parameter to the function. |
[Editor] Highlight:
|
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:
|
And run the project.
|
[Terminal] Highlight:
|
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.
|
Switch to Editor | Switch back to the editor. |
[Editor - main.rs] Type:
fn main(){ println!(“The result is {}”,sum(1,2)); }
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.
|
[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:
} |
Here, using the print statement we are printing it.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
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:
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:
|
Run the project. |
[Terminal] Highlight:
Consider removing this semicolon |
This time we get an error, Consider removing this semicolon.
|
Switch to Editor | Switch back to the editor. |
[Editor] Modify:
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.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal |
[Terminal] Type:
|
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:
z*10 |
Here though the code works fine it is not properly readable.
|
[Editor] Modify:
let z = a + b; return z*10; } |
To make it readable, we will modify our function a little bit.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal |
[Terminal] Type:
|
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.
|
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt to:
|
Slide: Assignment | As an assignment,
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
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 |