Difference between revisions of "Rust/C2/Functions-and-Returning-value-from-function-using-Rust/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "Title of the script: Functions and Returning Value from Functions Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: Rust, cargo, function, rs...")
 
Line 24: Line 24:
 
Learning Objectives
 
Learning Objectives
 
|| In this tutorial, we will learn to:
 
|| In this tutorial, we will learn to:
 
 
 
* Create a '''function'''
 
* Create a '''function'''
 
* Create a '''parameterized function'''
 
* Create a '''parameterized function'''
 
* '''Return''' a value from a '''function'''
 
* '''Return''' a value from a '''function'''
 
 
  
 
|-
 
|-
Line 38: Line 34:
  
 
|| This tutorial is recorded using:
 
|| This tutorial is recorded using:
 
 
 
* '''Ubuntu Linux''' OS version 18.04
 
* '''Ubuntu Linux''' OS version 18.04
 
* '''Rust''' version 1.47.0
 
* '''Rust''' version 1.47.0
Line 49: Line 43:
 
|| Slide : Pre-requisites
 
|| Slide : Pre-requisites
 
|| To practice this tutorial,
 
|| To practice this tutorial,
 
 
 
* You should be familiar with '''compiling''' and '''running Rust''' files'''.'''
 
* You should be familiar with '''compiling''' and '''running Rust''' files'''.'''
 
* If not, please go through the prerequisite '''Rust''' tutorials on this website.
 
* If not, please go through the prerequisite '''Rust''' tutorials on this website.
 
 
  
 
|-
 
|-
 
|| Slide: Code files
 
|| Slide: Code files
 
||  
 
||  
* The files used in this tutorial are available in the''' Code files''' link on this tutorial page.
+
* The file used in this tutorial is available in the''' Code files''' link on this tutorial page.
 
* Pls download and extract the file.
 
* Pls download and extract the file.
 
* Make a copy and then use it for practising.
 
* Make a copy and then use it for practising.
 
 
  
 
|-
 
|-
Line 71: Line 59:
 
* '''Functions''' provide better modularity for our application and
 
* '''Functions''' provide better modularity for our application and
 
* High degree of code reusability
 
* High degree of code reusability
 
 
  
 
|-
 
|-
Line 80: Line 66:
 
|-
 
|-
 
|| Press '''Ctrl+Alt+T '''keys
 
|| Press '''Ctrl+Alt+T '''keys
|| Open the '''terminal''' by pressing '''Ctrl,Alt''' and '''T''' keys simultaneously on the keyboard.
+
|| Open the '''terminal''' by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on the keyboard.
  
  
Ensure that you have '''root''' permissions to run the '''commands'''.
+
Ensure that you have '''root permissions''' to run the '''commands'''.
  
 
|-
 
|-
Line 107: Line 93:
  
 
Type the '''command''' as shown.
 
Type the '''command''' as shown.
 
|-
 
|| Open Visual Studio Code editor.
 
|| You may use any '''editor''' of your choice.
 
 
 
I will use '''Visual Studio Code editor '''for this demonstration'''.'''
 
  
 
|-
 
|-
Line 171: Line 150:
  
 
'''println!(“Spoken Tutorial”);'''
 
'''println!(“Spoken Tutorial”);'''
|| And, here we are printing the text '''Spoken Tutorial '''in the '''function'''.''' '''
+
|| And, here we are printing the text '''Spoken Tutorials '''in the '''function'''.
  
 
|-
 
|-
Line 278: Line 257:
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
|| And '''run''' the '''project'''.
 
|| And '''run''' the '''project'''.
 
 
  
  
Line 287: Line 264:
  
 
'''I’m getting 1 from the main function.'''
 
'''I’m getting 1 from the main function.'''
|| Now we can see''' '''that the value 1 which was '''passed, '''is printed.
+
|| Now we can see that the value 1 which is being '''passed, '''is printed.
 
+
 
+
  
  
Line 324: Line 299:
  
 
'''fn sum(a:i32, b:i32) -> i32'''
 
'''fn sum(a:i32, b:i32) -> i32'''
|| Here, we are passing '''a''' and '''b''' as '''parameters''' to the '''sum function.'''
+
|| Here, we are '''passing a''' and '''b''' as '''parameters''' to the '''sum function.'''
  
  
Line 370: Line 345:
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
|| '''Run''' the '''project'''.
 
|| '''Run''' the '''project'''.
 
 
  
  
Line 420: Line 393:
  
  
In order to '''return the''' value directly without using the '''return keyword '''we need to remove the '''semicolon.'''
+
In order to '''return''' the value directly without using the '''return keyword '''we need to remove the '''semicolon.'''
  
 
|-
 
|-
Line 443: Line 416:
  
 
'''let z = a + b; '''
 
'''let z = a + b; '''
|| Here we have created a '''variable''' '''z '''and assigned '''a + b''' to it.
+
|| Here we have created a '''variable z '''and assigned '''a + b''' to it.
  
 
|-
 
|-
Line 471: Line 444:
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
|| '''Run''' the project.
 
|| '''Run''' the project.
 
 
  
  
Line 511: Line 482:
  
  
Add a '''return keyword''' before '''z*10 '''and a '''semicolon''' after '''z*10'''
+
Add a '''return keyword''' before '''z*10 '''and add a '''semicolon''' after '''z*10'''
  
 
|-
 
|-
Line 527: Line 498:
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
'''cargo run '''<nowiki>[Enter]</nowiki>
 
|| '''Run''' the project.
 
|| '''Run''' the project.
 
 
  
  
Line 535: Line 504:
  
 
'''The result is 30'''
 
'''The result is 30'''
|| Now we can see''' '''the value being returned by the '''function''' and the result being displayed correctly.
+
|| Now we can see the value being '''returned''' by the '''function''' and the result being displayed correctly.
  
  
Line 553: Line 522:
 
|| Slide''': '''Summary''' '''
 
|| Slide''': '''Summary''' '''
 
|| In this tutorial, we have learnt to:
 
|| In this tutorial, we have learnt to:
 
 
 
* Create a '''function'''
 
* Create a '''function'''
* Create a '''parameterized''' '''function'''
+
* Create a '''parameterized function'''
 
* '''Return''' a value from a '''function'''
 
* '''Return''' a value from a '''function'''
 
 
  
 
|-
 
|-
 
|| Slide: Assignment
 
|| Slide: Assignment
 
|| As an assignment,
 
|| As an assignment,
 
 
 
* Go to the project folder '''rust-assignment'''
 
* Go to the project folder '''rust-assignment'''
 
* In the '''main.rs''' file
 
* In the '''main.rs''' file
** Create a '''function''' that accepts a '''number '''as''' '''a''' parameter'''
+
** Create a '''function''' that accepts a '''number '''as a''' parameter'''
 
** Print '''EVEN '''if the number is even
 
** Print '''EVEN '''if the number is even
 
** Else print '''ODD'''
 
** Else print '''ODD'''
 
* '''Compile '''and '''execute '''the project.
 
* '''Compile '''and '''execute '''the project.
 
* Observe the output in the '''Terminal'''
 
* Observe the output in the '''Terminal'''
 
 
  
 
|-
 
|-
Line 581: Line 542:
 
* The video at the following link summarises the Spoken Tutorial project.
 
* The video at the following link summarises the Spoken Tutorial project.
 
* Please download and watch it
 
* Please download and watch it
 
 
  
 
|-
 
|-
Line 589: Line 548:
 
* We conduct workshops using spoken tutorials and give certificates.
 
* We conduct workshops using spoken tutorials and give certificates.
 
* For more details, please write to us.
 
* For more details, please write to us.
 
 
  
 
|-
 
|-
 
|| Slide: Forum questions
 
|| Slide: Forum questions
||  
+
|| Pls post your timed queries in this forum
* Pls post your timed queries in this forum
+
 
+
 
+
  
 
|-
 
|-
 
|| Slide: Acknowledgement
 
|| Slide: Acknowledgement
||  
+
|| Spoken Tutorial Project is funded by Ministry of Education, Government of India
* Spoken Tutorial Project is funded by Ministry of Education, Government of India
+
 
+
 
+
  
 
|-
 
|-

Revision as of 21:54, 24 January 2021

Title of the script: Functions and Returning Value from Functions

Author: Jayesh Katta Ramalingaiah

Domain Reviewer:

Novice Reviewer:

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)

However you may use any other editor of your choice.

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