WEBVTT

00:01.120 --> 00:08.040
After all, you learn about loops, conditions, casting, data type variable, and a lot of things.

00:08.280 --> 00:13.440
Now it is time for learning something about functions.

00:13.480 --> 00:24.800
Okay, first let's create a punk.py file and we are going to learn function examples and everything

00:24.800 --> 00:25.080
here.

00:25.120 --> 00:25.760
Okay.

00:25.800 --> 00:26.840
What is a function.

00:27.000 --> 00:32.040
A function is a block of code which only runs when it is called.

00:32.080 --> 00:32.680
Okay.

00:32.720 --> 00:36.840
You can pass data known as parameter into a function.

00:37.240 --> 00:41.240
And also a function can return data as a result.

00:41.280 --> 00:44.520
Okay, so how can we create a function.

00:45.000 --> 00:45.920
Okay.

00:46.000 --> 00:48.400
Actually we have two kind of function okay.

00:48.440 --> 00:51.040
One of them is built in function.

00:51.040 --> 00:55.360
And also pre the function that you can define it okay.

00:55.400 --> 01:01.920
So in Python a function is defined using a def keyword or def like this.

01:02.910 --> 01:05.150
And let's create a function.

01:05.150 --> 01:06.710
Name it my function.

01:07.270 --> 01:11.870
And like this you can create a function like this.

01:11.910 --> 01:15.630
Okay you use diff keyword and then you give a name to the function.

01:15.630 --> 01:20.510
And this use this parenthesis and use colon head enter.

01:20.510 --> 01:28.790
And right here for example let's print a text here and say hello from a function.

01:29.470 --> 01:30.270
Very easy.

01:30.550 --> 01:31.390
Okay.

01:31.390 --> 01:35.510
Now if I run this it will do nothing and I will tell you why.

01:36.390 --> 01:40.190
Let's use Python function.py.

01:40.190 --> 01:43.230
And you see that we have no output.

01:44.190 --> 01:44.550
Why?

01:45.030 --> 01:47.870
Because we haven't called this.

01:48.230 --> 01:57.630
Okay, I told you, function can be run and can be executed whenever it is called only it is if it is

01:57.630 --> 01:59.910
called so it will be executed.

01:59.910 --> 02:03.030
If not, it is not going to be executed.

02:03.510 --> 02:08.830
So to call a function, use the use the function name followed by parentheses.

02:09.310 --> 02:18.710
So here let's use my function and then use these parentheses.

02:19.110 --> 02:22.750
Now let's save it and then run it again.

02:23.070 --> 02:24.790
You see now we have it.

02:24.830 --> 02:26.230
Hello from a function.

02:26.830 --> 02:36.990
So again if I use Ctrl c Ctrl V and save it again if I execute this code again you see that this time

02:36.990 --> 02:38.710
it gives me twice.

02:39.390 --> 02:41.910
And as much as I ran.

02:41.910 --> 02:47.550
As much as I call the function, it will print me this text, this function.

02:48.110 --> 02:50.390
So also we have parameters.

02:50.390 --> 02:54.310
Information can be passed to function as parameters.

02:54.750 --> 03:00.870
So parameters are specified after the function name.

03:01.630 --> 03:02.070
Okay.

03:02.550 --> 03:04.550
Here inside.

03:05.070 --> 03:05.550
Okay.

03:05.590 --> 03:06.990
Inside the parenthesis.

03:07.110 --> 03:08.300
This parenthesis Parentheses.

03:08.700 --> 03:14.740
You could, uh, you can add as many parentheses parameters as you want.

03:14.780 --> 03:15.180
Okay.

03:15.500 --> 03:21.980
You can add a lot of parameters here, but just be careful to separate them with a camera.

03:22.620 --> 03:23.860
How can we do that?

03:23.940 --> 03:31.580
Let's, uh, in this function, let's say we have, uh, something like if name.

03:31.620 --> 03:32.980
Okay, anything.

03:33.580 --> 03:38.140
And right here I pass a parameter by this name.

03:38.140 --> 03:41.620
And I'm going to use that here if name okay.

03:42.780 --> 03:47.060
And function name.

03:47.060 --> 03:51.700
And here I am going to pass this if name here.

03:51.780 --> 03:58.340
Because while I created this function I told it that it needs to get an argument.

03:58.340 --> 04:02.580
And whenever I am calling the function I need to pass something to this.

04:02.580 --> 04:06.060
Okay, so here I am going to.

04:08.340 --> 04:11.780
Say my function Okay.

04:13.580 --> 04:14.900
It is something that you pass.

04:14.900 --> 04:16.740
You could write anything here.

04:17.100 --> 04:22.780
Now, if I execute the program again, you say that hello from from my function.

04:22.780 --> 04:23.900
From my function.

04:24.900 --> 04:25.260
Okay.

04:27.380 --> 04:31.540
So it is how you can pass a parameter to a function.

04:32.020 --> 04:42.940
And also we can um, we can use a default parameter, which is very, uh, very good sometimes.

04:43.260 --> 04:46.060
For example, right here.

04:48.260 --> 04:56.540
Um, I'm going to pass a default parameter to this if name is equal to my function.

04:56.540 --> 05:01.900
And okay, by default it is my function.

05:02.300 --> 05:06.060
And right now it is not necessary to give this okay.

05:06.100 --> 05:07.220
It's optional now.

05:10.700 --> 05:11.180
Okay.

05:11.220 --> 05:13.690
See hello from my function.

05:13.690 --> 05:15.090
It comes from here.

05:15.410 --> 05:25.890
But if I use Ctrl C and Ctrl V and this time I give something else, for example, name it and execute

05:25.890 --> 05:26.370
this.

05:26.650 --> 05:32.570
So this time it will give me hello from name instead of my function.

05:32.570 --> 05:34.050
It will give me this one.

05:34.210 --> 05:41.090
Now if I comment this you can comment in Visual Studio Code using Ctrl and hitting the slash.

05:41.650 --> 05:44.730
So it will give you a comment.

05:45.490 --> 05:46.770
So here see here.

05:46.770 --> 05:48.050
Hello from name.

05:48.530 --> 06:00.530
It is how you can use a default parameter using function and also function can return something.

06:00.690 --> 06:03.570
Okay, the later function will return a value.

06:03.610 --> 06:06.970
Use the return keyword or use the return statement.

06:07.290 --> 06:08.890
So now let's.

06:11.490 --> 06:19.090
Use another function and I'm going to name it this time.

06:19.570 --> 06:22.210
Mull means multiplication.

06:22.810 --> 06:25.930
In here I'm going to give an argument that is going to be x.

06:26.330 --> 06:27.770
Here I'm going to return.

06:28.290 --> 06:33.610
For example, two is going to be multiplied by x.

06:34.530 --> 06:35.690
And that is it.

06:35.770 --> 06:40.170
Now whenever I call this function and give a number to this.

06:40.610 --> 06:47.570
So it needs to multiply that number by two and then return the result for me.

06:47.570 --> 06:49.690
Let's print that function mul.

06:49.970 --> 06:54.050
And here give the number two and save it.

06:54.050 --> 06:55.330
Then call it.

06:55.370 --> 06:56.530
It gives me four.

06:56.690 --> 06:56.970
Why?

06:57.010 --> 07:02.930
Because four comes here and then two comes here that we give it right here.

07:03.050 --> 07:03.210
Okay.

07:03.210 --> 07:03.890
Two multiply.

07:03.930 --> 07:05.330
Two is equal to four.

07:05.810 --> 07:07.570
Now if I give four.

07:10.570 --> 07:12.370
Now you know that it is eight.

07:12.410 --> 07:13.050
Okay.

07:13.050 --> 07:17.450
That is how you can use return keyword with function.
