Difference between revisions of "Rust/C2/Functions-and-Returning-value-from-function-using-Rust/English"
(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...") |
Nancyvarkey (Talk | contribs) |
||
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 | + | * 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''' | + | Ensure that you have '''root permissions''' to run the '''commands'''. |
|- | |- | ||
Line 107: | Line 93: | ||
Type the '''command''' as shown. | Type the '''command''' as shown. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 171: | Line 150: | ||
'''println!(“Spoken Tutorial”);''' | '''println!(“Spoken Tutorial”);''' | ||
− | || And, here we are printing the text '''Spoken | + | || 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 | + | || 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 | + | || 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 | + | 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 | + | || 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''' ''' | + | || 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 | + | * 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 | + | ** 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 |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
|| Slide: Acknowledgement | || Slide: Acknowledgement | ||
− | || | + | || 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
|
|
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:
However you may use any other editor of your choice. |
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 |