WEBVTT

00:00.080 --> 00:06.020
One of the most important things you want to do when programming is to avoid repeating code too much.

00:06.020 --> 00:08.060
So let's consider this program.

00:08.060 --> 00:09.530
Here I have first.

00:09.530 --> 00:12.380
So I declare a variable a is equal to two.

00:12.410 --> 00:14.330
And then I have three more variables.

00:14.330 --> 00:18.200
And for each one I actually want to do the triple of number a.

00:18.230 --> 00:19.400
So that's going to work.

00:19.400 --> 00:25.280
But instead of repeating the same code to triple the number, every time that we need to triple the

00:25.280 --> 00:29.720
number, let's create a function that does just that.

00:29.720 --> 00:31.100
So what is a function?

00:31.130 --> 00:36.590
A function is a reusable block of code that you can call from another part of your program.

00:36.620 --> 00:42.860
A function has a name, and it can take some input parameters and also can return some values if you

00:42.860 --> 00:43.310
want to.

00:43.340 --> 00:46.580
Let's create one function here so you will understand.

00:46.580 --> 00:49.670
And I'm going to do that at the beginning of the program here.

00:49.670 --> 00:53.270
So I'm just going to put a few more lines and let's create it there.

00:53.270 --> 00:58.220
So there is a specific syntax that you will use to create any function.

00:58.220 --> 01:01.520
The first thing to write is the keyword def.

01:01.550 --> 01:04.370
You can see we have a syntax highlighting for def.

01:04.400 --> 01:10.340
Okay so you write def and then space and you rewrite the name of the function.

01:10.340 --> 01:17.000
And best practice here is to use a meaningful name so that when somebody or you in the future read your

01:17.000 --> 01:18.800
function it makes sense.

01:18.830 --> 01:19.220
Okay.

01:19.250 --> 01:22.460
So we want to triple a number.

01:22.490 --> 01:26.900
Let's just call our function triple number.

01:26.930 --> 01:27.440
Okay.

01:27.470 --> 01:28.280
As simple as that.

01:28.280 --> 01:33.590
And as you can see if we want to have several words inside the function no problem.

01:33.590 --> 01:35.540
You just add an underscore okay.

01:35.570 --> 01:37.400
No space just an underscore.

01:37.430 --> 01:44.180
Then after the name of the function you will open and close parentheses and finish with a colon.

01:44.210 --> 01:47.990
Now in those parentheses you can add input parameters.

01:48.020 --> 01:48.200
Okay.

01:48.200 --> 01:52.700
So when we call the function we can send some data to the function.

01:52.700 --> 01:54.890
And the function can process this data.

01:54.890 --> 01:57.440
So for example numbers well any variable you want.

01:57.440 --> 02:00.860
And let's call this parameter here Number.

02:00.890 --> 02:01.100
Okay.

02:01.130 --> 02:04.940
So we create here a number as an input parameter.

02:04.970 --> 02:07.310
You can have zero parameter.

02:07.310 --> 02:08.390
You can have one parameter.

02:08.390 --> 02:10.670
You can have as many parameters as you want.

02:10.700 --> 02:13.850
You just separate them with a comma.

02:13.880 --> 02:14.210
Okay.

02:14.240 --> 02:16.550
So here for example I have three parameters.

02:16.880 --> 02:18.320
Let's just keep one.

02:18.320 --> 02:19.730
And then you will press.

02:19.730 --> 02:21.950
So after the colon here you press enter.

02:21.950 --> 02:26.540
And you can see that when we go back to a new line we don't go to the beginning of the line.

02:26.540 --> 02:27.560
We go to.

02:27.590 --> 02:31.280
You can see here we have four spaces.

02:31.280 --> 02:34.520
So we have an indentation of four spaces.

02:34.520 --> 02:36.710
And this indentation is very important.

02:36.710 --> 02:38.660
I'm going to come back to this a bit later.

02:38.660 --> 02:39.890
And here.

02:39.890 --> 02:46.430
So just after this line with this indentation we can start to write the code for the function.

02:46.430 --> 02:47.480
So you can write some code.

02:47.480 --> 02:51.050
And we can use the keyword return.

02:51.050 --> 02:55.250
So we return you finish the function and you return a value.

02:55.280 --> 02:56.630
What do we want to return.

02:56.630 --> 02:59.180
Well we have the number parameter here.

02:59.180 --> 03:01.250
We want to return number.

03:01.280 --> 03:03.260
Multiply by three.

03:03.290 --> 03:03.620
Okay.

03:03.650 --> 03:05.300
And that's it for the function.

03:05.300 --> 03:09.770
So we write def then the name of the function we open and close parentheses.

03:09.800 --> 03:12.080
We can add some input parameters.

03:12.080 --> 03:16.040
Then a colon we go back to a new line with an indentation.

03:16.040 --> 03:17.150
We can write some code.

03:17.150 --> 03:19.520
You can write code on several lines if you want to.

03:19.550 --> 03:21.500
And then you may return something.

03:21.500 --> 03:27.020
So if it's the case you just use the return keyword with some value here.

03:27.020 --> 03:30.770
And then after we have defined the function okay.

03:30.800 --> 03:34.010
So def is for define, then you can use it in the code.

03:34.010 --> 03:43.910
So instead of writing this here I can just do triple number and then pass a as an input parameter okay.

03:43.940 --> 03:49.340
So what will happen here is that I'm going to call this triple number function that's defined here.

03:49.340 --> 03:51.620
And at this point I'm going to pass a.

03:51.650 --> 03:55.220
So not a but actually the value inside a okay.

03:55.250 --> 04:02.780
So when I do this I'm going to call triple number with the value two okay is going to be received as

04:02.780 --> 04:04.550
the number parameter.

04:04.550 --> 04:07.460
So the number variable inside this function.

04:07.460 --> 04:09.680
And then it's going to return the number.

04:09.680 --> 04:12.890
So two times three is going to return six.

04:12.890 --> 04:14.660
So we have six as a result here.

04:14.660 --> 04:17.090
And let's just do the same.

04:17.090 --> 04:25.490
So let's just copy this one and put it there as well and there as well.

04:25.490 --> 04:31.550
And let's just because it's not going to print anything let's just print B for example okay.

04:31.580 --> 04:37.940
We print this variable here and you can see the result is six as I just explained.

04:37.940 --> 04:39.890
So that's how you create a function.

04:39.890 --> 04:41.390
And now you can do whatever you want here.

04:41.390 --> 04:52.280
For example we could say d is equal to triple number a and then plus triple number of c for example.

04:52.310 --> 04:54.170
Let's see what will happen.

04:54.170 --> 04:56.540
So I'm going to print D instead.

04:57.440 --> 05:00.500
And this time we have the value 24 Okay.

05:00.530 --> 05:00.770
Why?

05:00.800 --> 05:01.700
24.

05:01.730 --> 05:05.780
Because you can see we first call triple number A.

05:06.050 --> 05:06.260
Okay.

05:06.290 --> 05:09.200
So that will return six as we just saw previously.

05:09.200 --> 05:13.580
And then triple number C what's the value of C.

05:13.610 --> 05:18.320
Well the value of c is six because we have the triple of two.

05:18.350 --> 05:20.630
So the value of c is six.

05:20.720 --> 05:25.370
Here we pass six as a parameter to this function.

05:25.370 --> 05:28.520
And then it returns six times three.

05:28.550 --> 05:31.070
So it will return 18.

05:31.130 --> 05:31.310
Okay.

05:31.340 --> 05:33.350
So 18 plus six.

05:33.350 --> 05:34.400
We have 24.

05:34.430 --> 05:34.910
All right.

05:34.910 --> 05:38.600
And you have seen how to create a function that returns a value.

05:38.600 --> 05:40.220
But let's just create another function.

05:40.220 --> 05:41.900
So I'm going to I'm going to go back to a new line.

05:41.900 --> 05:46.520
You can see I go back to the previous indentation I go back to new line.

05:46.520 --> 05:48.800
Let's add some space for readability.

05:48.800 --> 05:53.120
And I'm going to create a new function that doesn't return anything.

05:53.120 --> 05:57.890
So I use def and let's say I want a function that prints the triple number.

05:57.890 --> 06:03.100
So let's just call it print Triple number.

06:04.540 --> 06:04.900
Okay.

06:04.930 --> 06:11.440
So instead of actually calling the function and then using print, we can do all of that with just one

06:11.440 --> 06:12.070
function.

06:12.070 --> 06:15.820
And I'm going to create an input parameter named number.

06:16.480 --> 06:19.870
I close the parentheses I add a colon.

06:19.870 --> 06:23.560
I go to a new line with enter okay new indentation.

06:23.560 --> 06:25.960
And how can I print the triple number.

06:25.990 --> 06:28.090
Well let's create a variable here.

06:28.090 --> 06:36.580
Result is equal to triple number and I will pass the number.

06:36.610 --> 06:37.060
Okay.

06:37.090 --> 06:41.980
So when I get to this print triple number function I receive a parameter called number.

06:41.980 --> 06:43.810
And I call the triple number function.

06:43.810 --> 06:46.120
So I call another function from this function.

06:46.180 --> 06:49.030
And I pass the value of number.

06:49.120 --> 06:49.360
Okay.

06:49.390 --> 06:53.290
This is going to go into this other number parameter.

06:53.290 --> 06:55.780
And it's going to return the number times three.

06:55.780 --> 06:57.400
So we get the result here.

06:57.400 --> 06:58.630
And then I can print.

06:58.630 --> 07:03.640
So you can see I can just press enter again and print the result.

07:03.670 --> 07:03.910
Okay.

07:03.940 --> 07:04.810
And we stay.

07:04.810 --> 07:08.200
You can see in the same indentation so that we are still in the function.

07:08.200 --> 07:10.840
So now I can call this function I'm just going to remove that.

07:10.840 --> 07:15.010
And let's say print triple number.

07:15.010 --> 07:18.790
And note here you can use Ctrl and space.

07:18.790 --> 07:20.290
And you will see this kind of stuff.

07:20.290 --> 07:21.820
And then you can press enter.

07:21.850 --> 07:25.300
Ctrl space is basically the Autocompletion tool okay.

07:25.330 --> 07:27.880
In programming you will see you will use that a lot.

07:27.910 --> 07:29.080
Autocompletion.

07:29.080 --> 07:36.430
You can start a variable name, a function name for example, and then press Ctrl Space Anthony and

07:36.430 --> 07:41.320
you will get suggestions about how to finish your variable or your function.

07:41.320 --> 07:46.420
And as you progress with programming, you will use that a lot, because what it will make you gain

07:46.420 --> 07:52.360
a lot of time and also you will make less errors because the name for sure will be correct.

07:52.390 --> 07:56.200
Okay, you don't have any risk of making a typo if you use Autocompletion.

07:56.230 --> 07:56.560
All right.

07:56.560 --> 08:00.910
So I'm going to do print triple number and let's give five.

08:01.000 --> 08:01.450
Okay.

08:01.480 --> 08:02.620
Just like that.

08:02.620 --> 08:04.990
So to call a function, you can see that.

08:05.020 --> 08:06.370
Yeah we have to call.

08:06.370 --> 08:09.040
So we have to write the name of the function.

08:09.040 --> 08:14.980
And then open and close parentheses and provide the required amount of parameters.

08:14.980 --> 08:16.060
We just have one.

08:16.060 --> 08:21.850
But if you had several parameters you would need to add a comma between each parameter.

08:22.330 --> 08:23.590
Let's run that.

08:23.590 --> 08:25.510
And you can see we have 15.

08:25.570 --> 08:28.000
So in this case we go.

08:28.000 --> 08:29.560
So we call print number.

08:29.560 --> 08:32.080
We go here five goes here.

08:32.260 --> 08:36.070
Then five goes to triple number function okay.

08:36.100 --> 08:37.180
Multiply by three.

08:37.210 --> 08:39.790
We return it inside the result variable.

08:39.790 --> 08:42.340
And then we print the result variable okay.

08:42.370 --> 08:45.220
You can see the flow of execution here.

08:45.220 --> 08:49.060
And one thing I can improve here is well I just create a variable and I print it.

08:49.060 --> 08:52.210
But I don't really use that that much.

08:52.210 --> 08:55.510
So I can just instead do print.

08:56.470 --> 08:59.890
I open the parentheses and I close the parentheses here.

09:00.010 --> 09:02.620
And I'm going to remove this line.

09:03.490 --> 09:10.750
So you can see here I use a function okay inside a function.

09:10.750 --> 09:15.340
So the result of triple number is going to be used for the print function.

09:15.700 --> 09:15.910
All right.

09:15.940 --> 09:18.370
So you can combine functions like this.

09:18.370 --> 09:23.500
And let's finish with a third function I'm just going to call it say hello.

09:23.530 --> 09:25.240
This function just says hello.

09:25.240 --> 09:28.090
And I'm not going to pass any parameter okay.

09:28.090 --> 09:31.870
So you can see def then say hello open close parentheses colon.

09:31.870 --> 09:35.560
Go to a new line print just hello.

09:35.680 --> 09:36.160
All right.

09:36.160 --> 09:39.880
And that's a very simple and basic function I can call it here.

09:39.910 --> 09:47.200
Say hello okay I need to open and close parentheses always to call the function.

09:47.200 --> 09:50.680
Let's run this and you can see we have hello.

09:50.710 --> 09:50.920
Okay.

09:50.950 --> 09:54.190
So that's another example of a function that doesn't return anything.

09:54.190 --> 09:57.970
And it also doesn't get any input parameters.

09:57.970 --> 10:02.230
So to recap a function is a reusable block of code.

10:02.230 --> 10:07.960
It has a name, it can have some input parameters, and it can also return something.

10:07.960 --> 10:09.520
And there's a lot of flexibility.

10:09.550 --> 10:12.460
You can call a function within another function.

10:12.460 --> 10:15.520
You can combine functions together, etc..

10:15.550 --> 10:15.910
Okay.

10:15.940 --> 10:16.780
And a function.

10:16.780 --> 10:21.040
As you can see, a function that you define does nothing on its own okay.

10:21.070 --> 10:24.640
It's kind of a template that you can use to be able to use the function.

10:24.640 --> 10:27.820
You actually need to call the function as we did here.

10:27.820 --> 10:30.640
And finally you should always right.

10:30.640 --> 10:32.890
So you should define the function before you use it.

10:32.920 --> 10:33.280
Okay.

10:33.310 --> 10:38.050
If I try to run this line before I define the function, it's not going to work.

10:38.050 --> 10:41.770
So you always have to define the function first and then use it.

10:41.800 --> 10:45.100
If you don't do that, you will simply get an undefined error.

10:45.130 --> 10:45.310
Okay.

10:45.340 --> 10:50.650
That says that for example the triple number is undefined and then you know that you need to define

10:50.650 --> 10:52.510
it before you use it.

10:52.540 --> 10:52.930
All right.

10:52.960 --> 10:55.720
And we will come back very often to functions in this course.

10:55.720 --> 10:57.460
We will use them everywhere.

10:57.460 --> 11:00.400
So they will be more opportunities for you to practice.
