Difference between revisions of "Rust-Programming-Language/C2/Functions/English"
(One intermediate revision by the same user not shown) | |||
Line 6: | Line 6: | ||
|| '''Slide 1''' | || '''Slide 1''' | ||
− | || | + | || Welcome to the Spoken Tutorial on '''Functions '''in '''Rust.''' |
− | |- | + | |- |
− | || | + | || '''Slide 2''' |
− | + | '''Learning Objectives''' | |
|| In this tutorial, we will learn about | || In this tutorial, we will learn about | ||
− | * | + | * Functions |
− | |- | + | |- |
− | || | + | || '''Slide 3''' |
− | + | '''System Requirements''' | |
− | || | + | || This tutorial is recorded using |
− | * | + | * Ubuntu Linux OS version 22.04 |
− | * | + | * Rust 1.80.0 |
− | * | + | * Visual Studio Code 1.19.0 |
− | |- | + | |- |
− | || | + | || '''Slide 4''' |
− | + | '''Prerequisites''' | |
|| | || | ||
− | * | + | * You should be familiar with compiling and running a Rust program |
− | |- | + | |- |
− | || | + | || '''Slide''' '''5''' |
− | + | '''Code File''' | |
|| | || | ||
− | * | + | * The following code files is required to practise this tutorial |
− | * | + | * This file is provided in the code files link of this tutorial page |
− | |- | + | |- |
|| '''Slide 6''' | || '''Slide 6''' | ||
'''Functions''' | '''Functions''' | ||
− | || | + | || We will see about functions in Rust. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |- | + | * It is a set of statements to perform a specific task |
+ | * Functions organize the program into logical blocks of code. | ||
+ | |- | ||
|| '''Slide 7''' | || '''Slide 7''' | ||
+ | |||
+ | '''Functions''' | ||
+ | * Functions make the code reusable. | ||
+ | * It structures your programs and makes them more readable and maintainable. | ||
+ | |||
+ | |- | ||
+ | || '''Slide 8''' | ||
'''Function - Example''' | '''Function - Example''' | ||
Line 66: | Line 71: | ||
} | } | ||
|| | || | ||
− | * | + | * Here, we have created a''' greet()''' function that prints "Hello, World!" . |
− | * | + | * To invoke the created function, we need to make a function call. |
− | * | + | * Notice that we are calling the function from inside '''main()'''. |
− | * | + | * In Rust, '''main() '''is also a function known as a built-in function that has a special meaning. |
− | |- | + | |- |
− | || | + | || Open '''Visual''' '''code editor''' |
− | || | + | || Let us open the '''Visual code editor.''' |
− | |- | + | |- |
− | | | + | || |
− | || | + | || In the menu bar, click on '''terminal''' and select''' New Terminal''' |
− | + | We can see a terminal window at the bottom. | |
− | |- | + | |- |
|| > cd '''MyRustProject''' | || > cd '''MyRustProject''' | ||
'''>''' '''cargo new functions''' | '''>''' '''cargo new functions''' | ||
− | || | + | || Let us go to our working directory '''MyRustProject''' as explained earlier. |
Type the command '''cargo new functions '''and press '''Enter''' | Type the command '''cargo new functions '''and press '''Enter''' | ||
Open the created project as shown. | Open the created project as shown. | ||
− | |- | + | |- |
− | || | + | || |
+ | || In the '''main.rs '''file, copy and paste the code from the Code file. | ||
+ | |- | ||
+ | || // define an add function that takes in two parameters | ||
− | + | fn add(a: i32, b: i32) { | |
− | + | let sum = a + b; | |
− | + | println!("Sum of a and b = {}", sum); | |
− | + | } | |
− | + | fn main() { | |
− | + | // call add function with arguments | |
− | + | add(12, 10); | |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|| In this example, the add function takes 2 parameters and gives the sum as output. | || In this example, the add function takes 2 parameters and gives the sum as output. | ||
Line 116: | Line 119: | ||
Save the file. | Save the file. | ||
− | |- | + | |- |
− | | | + | || Click on '''Terminal''' and select '''New Terminal.''' |
|| In the menu bar, click on '''Terminal''' and select '''New Terminal.''' | || In the menu bar, click on '''Terminal''' and select '''New Terminal.''' | ||
− | |- | + | |- |
− | | | + | || Type ''' cargo run ''' |
− | || | + | || In the terminal, type''' cargo run '''and see the output. |
It prints the sum of a and b as 22. | It prints the sum of a and b as 22. | ||
− | |- | + | |- |
|| | || | ||
|| Next we will see an example for a function with return value. | || Next we will see an example for a function with return value. | ||
Replace the code as shown. | Replace the code as shown. | ||
− | |- | + | |- |
− | || | + | || fn area(length: i32, breadth: i32) -> i32 { |
− | + | ||
− | fn area(length: i32, breadth: i32) -> i32 { | + | |
length * breadth | length * breadth | ||
} | } | ||
Line 143: | Line 144: | ||
Change the program to return length*breadth | Change the program to return length*breadth | ||
− | |||
− | In this example, we define a function to calculate the area of a rectangle. | + | ||In this example, we define a function to calculate the area of a rectangle. |
The area function takes two parameters, length and breadth, both of type i32. | The area function takes two parameters, length and breadth, both of type i32. | ||
− | '''-> i32 | + | '''-> i32 ''' right arrow i32 before the opening curly bracket indicates the function's return type. |
It returns an i32 value representing the area of the rectangle. | It returns an i32 value representing the area of the rectangle. | ||
Line 163: | Line 163: | ||
We can see the output as the area of the rectangle is 30. | We can see the output as the area of the rectangle is 30. | ||
− | Now let us add a semicolon at the end of length into Breadth statement . | + | Now let us add a semicolon at the end of length into Breadth statement. |
Save the file. | Save the file. | ||
|- | |- | ||
− | | | + | || |
− | | | + | || Let us run the program and see how the program works. |
|- | |- | ||
− | | | + | || Show the error. |
− | | | + | || It would not return a value and cause a compilation error. |
− | + | ||
− | It would not return a value and cause a compilation error. | + | |
− | + | This time we get an error, '''remove this semicolon to return this value.''' | |
This is because it is being considered as a '''statement''' and not as a '''return''' value. | This is because it is being considered as a '''statement''' and not as a '''return''' value. | ||
− | You can use either conventions * | + | You can use either conventions |
− | * | + | * length* breadth without semicolon or |
+ | * return length * breadth with semicolon | ||
for returning the value. | for returning the value. | ||
Line 187: | Line 186: | ||
We can observe that the area of the rectangle is 30. | We can observe that the area of the rectangle is 30. | ||
− | |- | + | |- |
|| '''Slide 11''' | || '''Slide 11''' | ||
− | + | '''Summary''' | |
In this tutorial, we learn about | In this tutorial, we learn about | ||
− | * | + | * '''Functions''' |
|| This brings us to the end of this tutorial. | || This brings us to the end of this tutorial. | ||
Let us summarize. | Let us summarize. | ||
− | |- | + | |- |
|| '''Slide 12''' | || '''Slide 12''' | ||
Line 204: | Line 203: | ||
|| As an Assignment, do the following: | || As an Assignment, do the following: | ||
− | * | + | * 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. | * '''Compile '''and '''execute '''the project. | ||
* Observe the output in the '''Terminal''' | * Observe the output in the '''Terminal''' | ||
− | |- | + | |- |
|| '''Slide 18''' | || '''Slide 18''' | ||
− | + | '''Thank You''' | |
|| Thank you for joining. | || Thank you for joining. | ||
|- | |- | ||
|} | |} | ||
− |
Latest revision as of 17:38, 14 August 2025
Visual Cue | Narration |
Slide 1 | Welcome to the Spoken Tutorial on Functions in Rust. |
Slide 2
Learning Objectives |
In this tutorial, we will learn about
|
Slide 3
System Requirements |
This tutorial is recorded using
|
Slide 4
Prerequisites |
|
Slide 5
Code File |
|
Slide 6
Functions |
We will see about functions in Rust.
|
Slide 7
Functions
| |
Slide 8
Function - Example // define a function fn greet() { println!("Hello, World!"); } fn main() { //function call greet();
|
|
Open Visual code editor | Let us open the Visual code editor. |
In the menu bar, click on terminal and select New Terminal
We can see a terminal window at the bottom. | |
> cd MyRustProject
> cargo new functions |
Let us go to our working directory MyRustProject as explained earlier.
Type the command cargo new functions and press Enter Open the created project as shown. |
In the main.rs file, copy and paste the code from the Code file. | |
// define an add function that takes in two parameters
fn add(a: i32, b: i32) { let sum = a + b; println!("Sum of a and b = {}", sum); } fn main() { // call add function with arguments add(12, 10); } |
In this example, the add function takes 2 parameters and gives the sum as output.
Here, a and b are function parameters. i32 is the data type of parameters. 12 and 10 are known as function arguments that are passed to the add function. That means 12 is assigned to a and 10 is assigned to b. Save the file. |
Click on Terminal and select New Terminal. | In the menu bar, click on Terminal and select New Terminal. |
Type cargo run | In the terminal, type cargo run and see the output.
It prints the sum of a and b as 22. |
Next we will see an example for a function with return value.
Replace the code as shown. | |
fn area(length: i32, breadth: i32) -> i32 {
length * breadth } fn main() { let k = area(5, 6); println!("Area of the rectangle = {k}"); } Change the program to return length*breadth |
In this example, we define a function to calculate the area of a rectangle.
The area function takes two parameters, length and breadth, both of type i32. -> i32 right arrow i32 before the opening curly bracket indicates the function's return type. It returns an i32 value representing the area of the rectangle. In the next line, notice the lack of a semicolon after length * breadth. In Rust, the absence of a semicolon signifies that this line is an expression. The result value is returned by the function. Save the file and run the program. We can see the output as the area of the rectangle is 30. Now let us add a semicolon at the end of length into Breadth statement. Save the file. |
Let us run the program and see how the program works. | |
Show the error. | It would not return a value and cause a compilation error.
This time we get an error, remove this semicolon to return this value. This is because it is being considered as a statement and not as a return value. You can use either conventions
for returning the value. Save the file and run the program We can observe that the area of the rectangle is 30. |
Slide 11
Summary In this tutorial, we learn about
|
This brings us to the end of this tutorial.
Let us summarize. |
Slide 12
Assignment |
As an Assignment, do the following:
|
Slide 18
Thank You |
Thank you for joining. |