WEBVTT

00:00.150 --> 00:01.050
After variables.

00:01.050 --> 00:02.940
Let's see what functions are.

00:02.940 --> 00:08.280
And as we did for valuables, let's first understand why you need functions.

00:08.280 --> 00:09.900
And I'm going to go quite quick here.

00:09.900 --> 00:10.100
Okay?

00:10.110 --> 00:16.620
The goal is to give you an overview of functions, not to go into much details about everything you

00:16.620 --> 00:22.470
can do about functions, so you will learn more about functions as you progress in the course through

00:22.470 --> 00:25.440
how the different activities and practice exercises.

00:25.710 --> 00:29.610
So if it doesn't make complete sense for now, well, don't worry too much.

00:29.610 --> 00:31.710
It will by the end of the course.

00:31.710 --> 00:32.940
So what do we have here?

00:32.940 --> 00:33.150
What?

00:33.150 --> 00:35.430
I have created two variables here.

00:35.430 --> 00:36.720
You can see number one.

00:36.720 --> 00:41.910
And number two, those variables are integer numbers three and five.

00:41.910 --> 00:48.300
And what I do in the void setup is I just well, I just create a new variable A that is the double of

00:48.300 --> 00:51.800
number one, and then B, that is the double of number two.

00:51.810 --> 00:55.560
So let's say that what you want to do is simply to multiply the number by two.

00:55.560 --> 00:59.280
And so every time you're going to need to do the variable multiplied by two.

00:59.330 --> 01:05.190
Thing is this would be very inconvenient to always need to repeat the same code every time you need

01:05.190 --> 01:06.810
to use this functionality.

01:06.810 --> 01:11.670
And here it's a very small functionality, but the functionality can be much bigger with many lines

01:11.670 --> 01:12.240
of code.

01:12.660 --> 01:15.390
So what we need here is a function.

01:15.840 --> 01:22.530
A function is a reusable block of code that you can call from different parts of your program.

01:22.530 --> 01:24.840
And let's just create one to see what it is.

01:24.840 --> 01:26.730
So I'm just going to create a function here.

01:26.730 --> 01:31.290
Actually, void setup is already a function and void loop is a function.

01:31.740 --> 01:38.850
So let's create a new one here on top and let's call it double number like this, because we want to

01:38.850 --> 01:45.300
double a number and I'm going to make sure that I don't add any spaces for any new world.

01:45.300 --> 01:48.840
I just added new uppercase and you can see that at the beginning of the function.

01:48.840 --> 01:49.920
So that will be the name.

01:49.920 --> 01:54.780
But at the beginning of the function, we have something and we need to give the data type that we want

01:54.780 --> 02:01.590
to return because the function will accept some parameters as an input and we'll return something as

02:01.590 --> 02:02.610
an output.

02:02.710 --> 02:06.150
And so here I'm just going to put INT and then space double number.

02:06.150 --> 02:10.230
That means that this function will return an integer number.

02:10.230 --> 02:16.260
So I put the data type the name, and then I open and close the parentheses and I have the definition

02:16.260 --> 02:17.070
of my function.

02:17.070 --> 02:19.720
You can see just like the void set up and the void loop.

02:19.740 --> 02:24.840
Now what I can also do is when I have a function, as I told you, we can give it an input.

02:24.840 --> 02:30.780
And so the input here, I'm just going to put int number, which means that the function can receive

02:30.780 --> 02:36.060
an integer value here that's going to be named number inside the function.

02:36.060 --> 02:41.610
And once I have this, I can just go back to a new line and open and close curly brackets.

02:41.820 --> 02:42.050
Okay.

02:42.060 --> 02:44.970
Very similar to what we did here with void, setup and void.

02:45.120 --> 02:48.900
But in this case you can see I have INT and I also have one parameter.

02:48.900 --> 02:51.600
You could have as many parameters as you want.

02:51.630 --> 02:54.180
You just separate them with a comma.

02:54.570 --> 02:58.110
But let's just keep things simple for now with just one parameter.

02:58.200 --> 03:01.530
So this what is this function doing?

03:01.530 --> 03:06.210
Well, this function will need to actually multiply the number here by two.

03:06.300 --> 03:09.360
So let's just create a variable.

03:09.360 --> 03:15.030
INT result, for example, is equal to number multiplied by two.

03:15.840 --> 03:17.760
The number is the variable.

03:17.760 --> 03:21.750
As you can see that we get here in the parameter of the function.

03:21.750 --> 03:23.430
So that's what we do in the function.

03:23.430 --> 03:26.340
And then there is a keyword that you need to use in a function.

03:26.340 --> 03:31.350
It's called return can see turns green return result.

03:31.440 --> 03:33.240
So what do I return?

03:33.240 --> 03:37.800
I return this variable and this variable is an integer number.

03:37.800 --> 03:40.200
So that match is the type that I want to return.

03:40.440 --> 03:42.240
So now I have this block of code.

03:42.240 --> 03:48.390
You can see this block of code that's going to take as an input a number here, an integer number,

03:48.390 --> 03:51.150
and that's going to return another integer.

03:51.150 --> 03:57.960
But you can see this time the returned number is the double of the number that we pass as an input.

03:57.960 --> 04:01.410
And so once I have created a function, I can actually use it.

04:01.410 --> 04:08.280
And so here, instead of doing number one multiplied by two, I can just what we call calling the function.

04:08.280 --> 04:10.470
Actually, I can call the function.

04:10.470 --> 04:15.990
So double number and I pass number one.

04:16.920 --> 04:23.670
Double number, I call this function and I pass what I pass the number one variable here that I will

04:23.670 --> 04:26.280
pass to the input of the function.

04:26.280 --> 04:27.870
And so the number one will go here.

04:28.380 --> 04:34.560
It will be multiplied by two, and then we return the result, which means that it will go back here

04:34.560 --> 04:41.430
and then a will be equal to the result of this function, which is the double of the number.

04:41.730 --> 04:50.820
And I can do the same thing here, double number with number two, for example, I can give a different

04:50.820 --> 04:51.810
number every time.

04:51.810 --> 04:57.150
Okay, so that's a reusable block of code that I can use with a different number every time.

04:57.150 --> 04:57.600
Great.

04:57.600 --> 04:59.850
And that's a first example of a.

04:59.870 --> 05:03.680
Function and well, you can combine functions as much as you want.

05:03.680 --> 05:08.300
For example, you could call a function inside another function, and that's what we are going to do

05:08.300 --> 05:08.690
here.

05:08.690 --> 05:13.790
I'm going to create a function and I'm going to name it Void, and I'm going to explain to you void

05:13.790 --> 05:20.420
print double number and this one gets also int number.

05:20.660 --> 05:21.620
So what is this?

05:21.620 --> 05:24.890
Well, you can see that with a function you need to return data type.

05:24.890 --> 05:31.700
So you need to return, for example, an int or a float number or any other kind of data.

05:31.700 --> 05:36.260
But if you put void, void means that you will not return anything.

05:36.260 --> 05:39.530
And you can see that's actually what's happening in the void setup.

05:39.530 --> 05:45.560
This is a function and the void loop also is a function, but you don't have any return keyword because

05:45.560 --> 05:46.370
this is void.

05:46.790 --> 05:49.550
So we don't have any return keyword and that's what we do here.

05:49.550 --> 05:55.700
What we want to do is just to print the double of the number that we get from the parameter.

05:55.940 --> 06:00.710
So I put the void and then I put my opening and closing curly brackets.

06:00.710 --> 06:05.540
And what I can do here, let's say I want to so I want to print the double of this number.

06:05.540 --> 06:12.650
But instead of doing number multiplied by two, I can just call this function so I could do serial dot

06:13.040 --> 06:16.640
print ln to print something.

06:16.640 --> 06:23.960
And what I can print is I can just do double number and I pass the number that I receive.

06:23.960 --> 06:28.100
I pass it here and I close all the parentheses.

06:29.000 --> 06:35.090
So what's going to happen here is that if I call this function from here, let's say call it here,

06:35.180 --> 06:44.060
print double number with number one, what's going to happen is so we have created number one here and

06:44.060 --> 06:48.710
then we call the function named print double number with number one.

06:48.710 --> 06:49.910
So we go here.

06:51.140 --> 06:53.120
The number one is going to be passed here.

06:53.120 --> 06:58.220
So the int number in this function is going to become the value of number one.

06:58.220 --> 06:59.240
So it's going to be three.

06:59.750 --> 07:00.920
So here we have three.

07:01.430 --> 07:06.080
And then what we do is we do select println with double number.

07:06.080 --> 07:08.720
So here we are calling another function.

07:08.720 --> 07:12.110
So you see from one function we call another function.

07:12.350 --> 07:16.700
And so I call the double number and I pass what I get here as an input.

07:16.700 --> 07:20.060
I pass it here as an input for this function.

07:20.060 --> 07:23.330
So you can see I pass the values from function to function.

07:23.330 --> 07:29.000
And then this function is going to compute the double of the number, return the double of the number.

07:29.000 --> 07:30.430
So return the result.

07:30.440 --> 07:36.770
And so basically this here is going to be evaluated to the result of the function.

07:37.310 --> 07:39.470
So this will become the double of the number.

07:39.470 --> 07:42.050
And then what we do is we print the double of the number.

07:42.050 --> 07:42.680
All right?

07:42.770 --> 07:50.570
And so we can we can actually call it once I can call it twice with different numbers, etc., Actually,

07:50.570 --> 07:53.490
if we want to test, I would also need to initialize sales.

07:53.500 --> 07:55.190
So begin.

07:56.150 --> 07:57.980
Let's put this.

07:58.930 --> 07:59.770
Moderate.

08:00.840 --> 08:03.150
And let's actually run this.

08:04.990 --> 08:06.730
Lets name it functions.

08:07.790 --> 08:08.210
Okay.

08:08.330 --> 08:09.170
Don't uploading.

08:09.170 --> 08:14.780
If I open the sale monitor, you will see that we have six and ten.

08:15.260 --> 08:18.230
So six is the double of the number one.

08:18.230 --> 08:22.040
Three and ten is the double of the number two five here.

08:22.550 --> 08:24.680
So what I do, I just print double number.

08:24.680 --> 08:31.340
So I pass the three is going to go here to double number is going to go here is going to be return and

08:31.340 --> 08:32.120
we print it here.

08:32.480 --> 08:35.000
And then number two is going to be five.

08:35.000 --> 08:37.670
So we call this function with five.

08:37.910 --> 08:42.710
It goes here on line eight and then we call the one number on line two.

08:43.340 --> 08:49.460
So the number five is passed until here and then we reach on ten and then here we can print ten.

08:49.640 --> 08:56.540
And one thing we can maybe do also here as an improvement is that, well, in the double number function,

08:56.540 --> 09:00.980
what we do is we create a viable that we just return.

09:01.220 --> 09:05.880
So we don't use this variable for anything else than to return it.

09:05.900 --> 09:12.500
So instead of just creating that, I can just actually do return number multiplied by two.

09:13.730 --> 09:19.340
So if it's the last thing to do is you don't need to use the variable that you create, then you just

09:19.340 --> 09:22.580
can return whatever that you compute here directly.

09:22.580 --> 09:23.940
And this is going to work the same.

09:23.960 --> 09:24.350
All right.

09:24.350 --> 09:27.350
So that was a very quick introduction about functions.

09:27.350 --> 09:32.180
I know this is a quite complex topic when you begin, but don't be afraid if you don't understand everything,

09:32.180 --> 09:37.970
it will all make sense while we go through how the course with all of the different exercises.

09:37.970 --> 09:38.160
Okay.

09:38.210 --> 09:43.610
And later on, you can come back to this lesson and eventually it will make more sense.
