Difference between revisions of "Rust-Programming-Language/C2/Functions/English"
| Line 36: | Line 36: | ||
|| | || | ||
* The following code files is required to practise this tutorial | * The following code files is required to practise this tutorial | ||
| − | * This | + | * This file is provided in the code files link of this tutorial page |
|- | |- | ||
| Line 42: | Line 42: | ||
'''Functions''' | '''Functions''' | ||
| − | || | + | || We will see about functions in Rust. |
| + | |||
* It is a set of statements to perform a specific task | * It is a set of statements to perform a specific task | ||
* Functions organize the program into logical blocks of code. | * Functions organize the program into logical blocks of code. | ||
| + | |- | ||
| + | || '''Slide 7''' | ||
| + | |||
| + | '''Functions''' | ||
* Functions make the code reusable. | * Functions make the code reusable. | ||
* It structures your programs and makes them more readable and maintainable. | * It structures your programs and makes them more readable and maintainable. | ||
|- | |- | ||
| − | || '''Slide | + | || '''Slide 8''' |
'''Function - Example''' | '''Function - Example''' | ||
| Line 73: | Line 78: | ||
|- | |- | ||
|| Open '''Visual''' '''code editor''' | || Open '''Visual''' '''code editor''' | ||
| − | || Let us open the ''' | + | || Let us open the '''Visual code editor.''' |
|- | |- | ||
|| | || | ||
| Line 84: | Line 89: | ||
'''>''' '''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''' | ||
| Line 91: | Line 96: | ||
|- | |- | ||
|| | || | ||
| − | || In the '''main.rs '''file, copy and paste the code from the | + | || In the '''main.rs '''file, copy and paste the code from the Code file. |
|- | |- | ||
|| // define an add function that takes in two parameters | || // define an add function that takes in two parameters | ||
| Line 115: | Line 120: | ||
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 ''' | + | || Type ''' cargo run ''' |
|| In the terminal, type''' cargo run '''and see the output. | || In the terminal, type''' cargo run '''and see the output. | ||
| Line 139: | 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 159: | 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. | ||
| Line 166: | Line 170: | ||
|| Let us run the program and see how the program works. | || 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. | ||
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. |