WEBVTT

00:00.120 --> 00:01.040
Welcome back.

00:01.040 --> 00:03.400
Now let's start with functions.

00:03.640 --> 00:08.000
A function is a block of code that runs only when called.

00:08.000 --> 00:13.200
You can pass data known as parameters, into a function.

00:13.240 --> 00:16.720
A function can return data as a result.

00:16.760 --> 00:20.720
Use def the keyword def to define a function.

00:20.760 --> 00:30.040
Here we are creating a function called greet taking name as parameter and inside this function we have

00:30.040 --> 00:35.480
this print statement printing hello and the passed variable.

00:35.600 --> 00:38.080
This is the function declaration.

00:38.080 --> 00:43.960
So here we are creating and defining a function using the def keyword.

00:44.080 --> 00:49.800
To call a function, use the function name followed by parentheses.

00:50.080 --> 00:55.680
Here we have this function greet and a parameter name.

00:55.840 --> 01:03.000
In order to call the function, we need to call the the the name of the function followed by parentheses.

01:03.040 --> 01:11.180
Inside the parenthesis we define the arguments which later becomes parameters and use them.

01:11.340 --> 01:11.900
Okay.

01:12.060 --> 01:14.020
So let me run this function.

01:14.020 --> 01:15.020
And here we go.

01:15.340 --> 01:17.940
We have hello Alice.

01:18.060 --> 01:20.740
Another run of this function.

01:20.980 --> 01:28.340
Another call passing Bob as a parameter will give us hello Bob.

01:28.380 --> 01:33.220
Information can be passed into functions as arguments.

01:33.420 --> 01:39.620
Arguments are specified after the function name inside the parenthesis.

01:39.860 --> 01:46.220
You can add as many arguments as you want, just separate them with a comma.

01:46.220 --> 01:55.580
So here if I want to add another argument which is of type integer if you want your age years old.

01:55.620 --> 02:03.140
If I run the application we get an error because here we need to pass two parameters the name and age.

02:03.540 --> 02:08.650
So pass the age of Alice 18 Run!

02:08.650 --> 02:09.530
And here we go.

02:09.690 --> 02:10.890
Hello, Alice.

02:10.930 --> 02:13.890
Your age is 18 years old.

02:14.130 --> 02:14.770
Okay.

02:14.810 --> 02:21.730
When the function is called, we pass along the first name and the age.

02:21.970 --> 02:27.370
The terms parameter and argument can be used for the same thing.

02:27.530 --> 02:31.210
Information that are passed into a function.

02:31.210 --> 02:39.930
From a function's perspective, a parameter is a variable listed inside the parentheses in the function.

02:39.930 --> 02:40.570
Define.

02:40.610 --> 02:46.010
An argument is the value that is sent to the function when it's called.

02:46.010 --> 02:50.250
So this is called argument and this is called parameter.

02:50.250 --> 02:56.210
By default, a function must be called with the correct number of arguments, meaning that if the function

02:56.210 --> 03:02.610
expects two arguments exactly like the greet function, you have to call the function with two arguments,

03:02.730 --> 03:04.850
not more and not less.

03:04.850 --> 03:11.130
I can't pass here, for example, 99.56.

03:11.170 --> 03:15.990
If I run, we get an error because this is an extra argument.

03:16.030 --> 03:24.390
One last interesting thing about functions in Python we can pass the default parameter value.

03:24.630 --> 03:33.550
For example, here inside this function let me create a another parameter.

03:33.670 --> 03:38.910
It's of name email and I can give it a default value.

03:38.910 --> 03:44.030
For example a at gmail.com okay.

03:44.190 --> 03:47.590
And here print your email is email.

03:47.710 --> 03:56.230
If I run the application the application runs perfectly because here we have three parameters.

03:56.350 --> 04:00.110
And I pass two parameters and two arguments.

04:00.390 --> 04:02.670
Why it works.

04:02.870 --> 04:05.470
Because here we have a default value.

04:05.470 --> 04:10.150
And if we see your email is a at gmail.com.

04:10.350 --> 04:21.850
If I need to override the default value of the email here we can pass for example CC at com.

04:21.890 --> 04:24.810
Run the application again and here we go.

04:25.050 --> 04:28.370
Your email is CC at gmail.com.

04:28.370 --> 04:36.730
So the default parameter value can be specified by the equal sign in the function.

04:36.890 --> 04:42.210
If we call the function without argument, it uses the default value.

04:42.410 --> 04:51.290
If we call the function with the argument, it will use the argument and override the default value.

04:51.410 --> 04:58.050
Functions can return a value using return statement, for example.

04:58.330 --> 04:59.890
Def square.

05:00.210 --> 05:08.210
We are defining a new function named as square, taking into the parameters a variable x.

05:08.250 --> 05:09.810
X return.

05:10.290 --> 05:17.610
The return statement will return a value of this function x times x.

05:17.650 --> 05:20.320
This is the arithmetic operation.

05:20.360 --> 05:29.720
The product of x times x, which is x squared or x to power x or um sorry not x.

05:30.160 --> 05:35.000
It's x squared because here we have to x times x okay.

05:35.160 --> 05:42.960
The result here is a valuable equals to the returning type of the square.

05:42.960 --> 05:48.480
So here I am calling the function square passing five as an argument.

05:48.840 --> 05:52.560
The returning type will be five times five.

05:52.560 --> 05:56.240
So it will return 25 okay.

05:56.640 --> 06:04.360
The result will be stored inside the result variable and would be printed here using the statement.

06:04.400 --> 06:09.080
The square of f or of five is 25.

06:09.240 --> 06:14.600
Okay, so this is how we define functions in Python.

06:14.800 --> 06:17.320
How to pass parameters.

06:17.320 --> 06:22.920
How to return a resulting value of this function.
