Rust-Programming-Language/C2/Functions/English
| 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. |