Difference between revisions of "Rust-Programming-Language/C2/Functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 6: Line 6:
  
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Functions '''</span><span style="color:#000000;">in </span><span style="color:#000000;">'''Rust.'''</span>
+
|| Welcome to the Spoken Tutorial on '''Functions '''in '''Rust.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 2'''</div>
+
|| '''Slide 2'''
  
<div style="color:#000000;">'''Learning Objectives'''</div>
+
'''Learning Objectives'''
 
|| In this tutorial, we will learn about
 
|| In this tutorial, we will learn about
* <div style="color:#000000;">Functions</div>
+
* Functions
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>3'''
+
|| '''Slide 3'''
  
<div style="color:#000000;">'''System Requirements'''</div>
+
'''System Requirements'''
|| <span style="color:#000000;">This tutorial</span> is recorded<span style="color:#000000;"> using</span>
+
|| This tutorial is recorded using
* <div style="color:#000000;">Ubuntu Linux OS version 22.04</div>
+
* Ubuntu Linux OS version 22.04
* <span style="color:#000000;">Rust 1.</span><span style="color:#000000;">80</span><span style="color:#000000;">.0 </span>
+
* Rust 1.80.0  
* <span style="color:#000000;">Visual </span>S<span style="color:#000000;">tudio </span>C<span style="color:#000000;">ode 1.19.0</span>
+
* Visual Studio Code 1.19.0
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>4'''
+
|| '''Slide 4'''
  
<div style="color:#000000;">'''Prerequisites'''</div>
+
'''Prerequisites'''
 
||  
 
||  
* <span style="color:#000000;">You should be familiar with compiling and running </span><span style="color:#000000;">a </span><span style="color:#000000;">Rust </span>program
+
* You should be familiar with compiling and running a Rust program
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide'''</span> '''5'''
+
|| '''Slide''' '''5'''
  
<div style="color:#000000;">'''Code File'''</div>
+
'''Code File'''
 
||
 
||
* <span style="color:#000000;">The following code files </span>is<span style="color:#000000;"> required to practise this tutorial</span>
+
* The following code files is required to practise this tutorial
* <span style="color:#000000;">Th</span>is<span style="color:#000000;"> files </span>is<span style="color:#000000;"> provided in the code files link of this tutorial page</span>
+
* This file is provided in the code files link of this tutorial page
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 6'''
 
|| '''Slide 6'''
  
 
'''Functions'''
 
'''Functions'''
||  
+
|| We will see about functions in Rust.
* It <span style="color:#000000;">is a set of statements to perform a specific task</span>
+
* <div style="color:#000000;">Functions organize the program into logical blocks of code.</div>
+
* <span style="color:#000000;">Functions </span>make<span style="color:#000000;"> the code reusable.</span>
+
* <div style="color:#000000;">It structures your programs and makes them more readable and maintainable.</div>
+
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
* 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:
 
}
 
}
 
||
 
||
* <span style="color:#000000;">Here, we have created a</span><span style="color:#000000;">''' greet()'''</span><span style="color:#000000;"> function that prints "Hello, World!" .</span>
+
* Here, we have created a''' greet()''' function that prints "Hello, World!" .
* <div style="color:#000000;">To invoke the created function, we need to make a function call.</div>
+
* To invoke the created function, we need to make a function call.
* <span style="color:#000000;">Notice that we are calling the function from inside </span><span style="color:#000000;">'''main()'''</span><span style="color:#000000;">.</span>
+
* Notice that we are calling the function from inside '''main()'''.
* <span style="color:#000000;">In Rust, </span><span style="color:#000000;">'''main() '''</span><span style="color:#000000;">is also a function known as a built-in function that has a special meaning. </span>
+
* In Rust, '''main() '''is also a function known as a built-in function that has a special meaning.  
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">Open </span><span style="color:#000000;">'''Visual'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''code editor'''</span>
+
|| Open '''Visual''' '''code editor'''
|| <span style="color:#000000;">Let us open the </span><span style="color:#000000;">'''visual code editor</span><span style="color:#000000;">.'''</span>
+
|| Let us open the '''Visual code editor.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''terminal'''</span><span style="color:#000000;"> and select</span><span style="color:#000000;">''' New Terminal'''</span>
+
|| In the menu bar, click on '''terminal''' and select''' New Terminal'''
  
<div style="color:#000000;">We can see a terminal window at the bottom.</div>
+
We can see a terminal window at the bottom.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| > cd '''MyRustProject'''
 
|| > cd '''MyRustProject'''
  
 
'''>''' '''cargo new functions'''
 
'''>''' '''cargo new functions'''
  
|| Go to our working directory '''MyRustProject''' as explained earlier.
+
|| 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.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;"></div>
+
||
 +
|| In the '''main.rs '''file, copy and paste the code from the Code file.
 +
|-
 +
|| // define an add function that takes in two parameters
  
<div style="color:#000000;"></div>
+
fn add(a: i32, b: i32) {
|| <span style="background-color:#ffffff;color:#000000;">In the </span><span style="background-color:#ffffff;color:#000000;">'''main.rs '''</span><span style="background-color:#ffffff;color:#000000;">file, </span><span style="background-color:#ffffff;">copy and paste the code from the code file.</span>
+
let sum = a + b;
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
println!("Sum of a and b = {}", sum);
|| <div style="color:#000000;">// define an add function that takes in two parameters</div>
+
}
 
+
fn main() {
<div style="color:#000000;">fn add(a: i32, b: i32) {</div>
+
// call add function with arguments
<div style="color:#000000;">let sum = a + b;</div>
+
add(12, 10);
<div style="color:#000000;">println!("Sum of a and b = {}", sum);</div>
+
}
<div style="color:#000000;">}</div>
+
<div style="color:#000000;">fn main() {</div>
+
<div style="color:#000000;">// call add function with arguments</div>
+
<div style="color:#000000;">add(12, 10);</div>
+
<div style="color:#000000;">}</div>
+
 
|| 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.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
|| 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.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
|| Type ''' cargo run '''
|| <span style="color:#000000;">In the terminal, type</span><span style="color:#000000;">''' cargo run '''</span><span style="color:#000000;">and see the output.</span>
+
|| 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.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| 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.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| fn area(length: i32, breadth: i32) -> i32 {
 
+
fn area(length: i32, breadth: i32) -> i32 {
+
 
&nbsp;&nbsp;&nbsp;length * breadth
 
&nbsp;&nbsp;&nbsp;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 -''' right arrow i32 before the opening curly bracket indicates the function's return type.  
+
'''-> 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.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
||  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | Let us run the program and see how the program works.
+
|| Let us run the program and see how the program works.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
+
|| Show the error.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
|| It would not return a value and cause a compilation error.
 
+
It would not return a value and cause a compilation error.
+
  
<span style="background-color:#ffffff;">This time we get an error, </span><span style="background-color:#ffffff;">'''remove this semicolon to return this value.'''</span>
+
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&nbsp;* <div style="margin-left:2.018cm;margin-right:0cm;">length*breadth without semicolon or</div>
+
You can use either conventions&nbsp;
* <div style="margin-left:2.018cm;margin-right:0cm;">return length*breadth with semicolon</div>
+
* 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.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 11'''
 
|| '''Slide 11'''
  
<div style="color:#000000;">'''Summary'''</div>
+
'''Summary'''
  
 
In this tutorial, we learn about
 
In this tutorial, we learn about
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">'''Functions'''</div>
+
* '''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.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 12'''
 
|| '''Slide 12'''
  
Line 204: Line 203:
 
|| As an Assignment, do the following:
 
|| As an Assignment, do the following:
  
* <span style="background-color:#ffffff;">In the </span><span style="background-color:#ffffff;">'''main.rs'''</span><span style="background-color:#ffffff;"> file</span>
+
* 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'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 18'''
 
|| '''Slide 18'''
  
<div style="color:#000000;">'''Thank You'''</div>
+
'''Thank You'''
 
|| Thank you for joining.
 
|| Thank you for joining.
  
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

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
  • 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

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

Functions

  • Functions make the code reusable.
  • It structures your programs and makes them more readable and maintainable.
Slide 8

Function - Example // define a function fn greet() {

println!("Hello, World!");

} fn main() {

//function call greet();


}

  • 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

> 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 

  • length* breadth without semicolon or
  • return length * breadth with semicolon

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

  • Functions
This brings us to the end of this tutorial.

Let us summarize.

Slide 12

Assignment

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.
  • Observe the output in the Terminal
Slide 18

Thank You

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat