0:01 today we are going to talk about
0:04 functions in Python so let's begin with
0:06 function introduction functions are
0:08 nothing but a block of code that
0:12 performs a specific task think about
0:16 them as being a dishwasher so the way
0:20 this works is you give dirty dishes as
0:23 an input to the dishwasher when you
0:25 press the button internally it will
0:28 perform some tasks such as adding
0:31 detergent and water cleaning the dishes
0:34 and then drying them out in the end what
0:39 you get is clean dishes as an output so
0:43 functions are also similar they take
0:46 something as an input then depending on
0:47 what kind of code you write within a
0:49 function it will perform some task and
0:52 in the end you get something else back
0:57 as an output now why functions are
1:00 needed functions makes your code more
1:05 modular and more readable let me explain
1:09 this by giving a real-life example so if
1:12 you have two lists and you want to
1:15 perform summation of these two
1:17 individual lists how do you do that
1:20 if you are not using functions I'm going
1:23 to initialize or these two lists and I'm
1:25 just going to call this two lists as
1:29 Tom's expense lists and Joe's expense
1:31 lists so these are the expense lists
1:33 from two different people and what we
1:36 are trying to do here is find the total
1:40 of each individual list okay so without
1:42 using the functions what you will do is
1:45 you will create a variable called total
1:51 and then you will write a for loop to go
1:56 or each of these items and in the end
2:00 your will print a total I am just
2:02 copying and pasting the code from a
2:04 different document just to save time on
2:07 recording so
2:11 up here what you just did is you went
2:13 over each of the items from Tom's
2:16 expense list and you added it to this
2:22 variable okay now to find the total of
2:25 Joe's expense list you will do the same
2:27 thing so I will just copy paste this
2:30 code and I will replace this guy with
2:32 Joe's expense list because the second
2:34 thing you are doing is printing the list
2:38 of total expense of Joe's list and you
2:41 will say these are total expenses
2:44 incurred by Joe okay let's run the
2:48 program excellent so when you ran it you
2:51 got the total expenses from for both the
2:54 people the problem with this code is
2:58 that you are repeating these three lines
3:02 of code at two places imagine if you had
3:05 100 such list you will have to repeat
3:07 these three lines 100 times and that's
3:11 very cumbersome we can encapsulate this
3:14 particular code into a function so now
3:16 let me write the same code using a
3:20 function so I am going to remove this
3:23 thing here we will still keep our list
3:25 and I will start writing a function so
3:27 the way you write the function is first
3:30 you type d e f DF is a special keyword
3:33 that tells python that i am going to
3:37 write a function so calculate total is
3:43 the function name and exp is input here
3:45 you will create local variable called
3:50 total and you will I trip through the
3:54 items in exp and by the way these lists
3:56 we are when we call this function we are
3:58 going to pass these two lists as an
4:01 input so exp variable will have a list
4:05 here you will say total equal to total
4:18 in the end you return a total okay the
4:21 way you call this function is you create
4:24 another variable called Tom's total and
4:29 you call this function with Tom's list
4:33 as an input then for juice total you
4:36 create one more variable called juice
4:38 total and your game call this function
4:41 calculate total and you pass the juice
4:46 expense list as an input now it's the
4:51 time to print the total
4:59 so Tom's total X and this is Tom's total
5:02 and I can just copy paste this code and
5:07 I can say juice total expenses is juice
5:11 total okay let me run it and see what
5:17 happens excellent I got the same result
5:21 okay now let me explain the way this
5:24 worked is when you call calculate total
5:27 here by passing this list as an input
5:31 what happens is this list gets past or
5:36 into exp exp is a local variable for
5:39 this function okay and this much is a
5:42 function body and the body of the
5:44 function is defined by this indentation
5:47 you see this extra tab here so whenever
5:50 that ends or whenever you return the
5:55 function body will end here you will
5:57 perform some operations you will create
5:59 a local variable you will write it to
6:02 the items and in the end you will return
6:07 so this thing total here is called a
6:09 return value and you return a value from
6:11 a function by using using this return
6:14 keyword okay so these are the basic
6:17 components to remember number one is a
6:20 function argument number two is a function
6:22 function
6:24 a return value and this whole thing is
6:29 called function okay so if you compare
6:31 with this again with a dishwasher
6:35 example just imagine this is a
6:39 dishwasher then this is an input to the
6:41 dishwasher which is your dirty dishes
6:45 and these three lines is the set of
6:48 steps that dishwasher is performing like
6:51 adding water and detergent washing this
6:55 is tying them out etc in the end this
6:57 return statement returns you the clean
7:01 dishes which is an output okay so that
7:04 was a quick comparison with a dishwasher
7:08 now let's work on another example and
7:10 this one is going to be very very simple
7:12 let's say you want to do sum of two
7:16 numbers you can do it using a function
7:18 so I can write a function let me erase
7:20 the score I can write one more function
7:27 call sum and a comma B so a and B are
7:30 the arguments to this function here you
7:33 create a local variable total equal to
7:37 zero and a rather total equal to a plus
7:41 B and you can just return it return
7:48 total okay and you can say is some PI
7:53 and six okay now tell me what will be
8:03 so when I run it as you all expect I'm
8:06 going to get 11 because 5 plus 6 is 11
8:10 what happened here is let me debug the
8:12 program and then I'll explain how it
8:16 worked I will put a breakpoint at do
8:20 these two places I will say debug okay
8:29 and I will say okay next so it first
8:31 define a function it we have not called
8:33 the function yet that's why it didn't go
8:35 here okay we are calling function here
8:39 so when you say go inside the function
8:43 let me see which one this one is tap
8:49 into so stab into next as you see here a
8:52 has value Phi and B has value 6 whatever
8:55 you pass from here this guy will get
8:58 those values and when you say next total
9:02 is 11 and now we are returning this
9:05 total so this total will now get placed
9:08 into this and variable next so as you
9:11 see M still doesn't have any value when
9:16 it's annexed at that time n gets 11 okay
9:20 if you check console we got total 11 now
9:23 the way we are passing function
9:25 arguments here is by order so if you
9:28 want to pass something in a you put that
9:31 value here as a first value and B you
9:34 put that as a second value let me print
9:41 those values here I will say print a is
9:49 a let me just copy paste couple of time
9:56 B is my B and I will also print total
9:58 before returning
10:04 so my total is dota okay let me turn it
10:07 so fair on it is five is six total is
10:12 eleven and total inside function inside
10:21 function this is total outside okay now
10:24 what if I want to pass B first and then
10:27 a a second so you can use something
10:29 called named arguments so if you say D
10:32 explicitly say B equal to five and equal
10:35 to six what's gonna happen is then it
10:37 will not go by order these are called
10:39 named arguments and the other ones we're
10:42 not they're not named arguments they
10:47 were basically using the order that's on
10:50 this voila
10:54 so a a six B is five if you have long
10:56 list of arguments and you want to be
11:00 specific about which argument has which
11:03 value you can use these named arguments
11:06 otherwise you can just use plain order
11:11 cool okay next item is global versus
11:14 local variables so until now all these
11:18 variables that we created here a B total
11:20 these are local to this function if you
11:24 try to access this variable here for
11:27 example let me print total so we have
11:29 defined total variable here all I'm
11:31 trying to do is print total but you see
11:35 this red line it says okay let me run it
11:38 it says name error name total is not
11:41 defined which means total is not visible
11:44 outside the function body total is only
11:46 visible inside the function body and my
11:51 function body is this much okay so what
11:55 happens if I have one more variable
11:58 called total outside if I have total
12:02 equal to zero and if I say total outside
12:06 the function is total okay let's see
12:07 what happens
12:09 total was zero here and here we assigned
12:11 a plus B value we are printing total
12:13 the function and then outside the
12:18 function let's run it great so total
12:21 inside the function was 11 but outside
12:25 it is zero so you see this variable and
12:27 this variable although their names are
12:30 different their names are same their
12:32 values are different because those
12:35 variables essentially are different this
12:37 one is called a global variable because
12:40 it is outside any function it can be
12:41 accessed anywhere
12:45 this one we created locally so it is a
12:50 local variable okay let's now talk about
12:54 default arguments I will just revert
12:59 this back to what it was originally here
13:01 we are passing two arguments to the
13:04 function what if I don't want to pass
13:06 the second argument if you don't want to
13:07 pass the second argument and if you run
13:09 the program it won't run because it will
13:13 say it is missing the argument okay but
13:16 sometimes you may have a default value
13:19 for the second argument so I'm making an
13:22 assumption in my code that if my
13:24 function caller doesn't pass me an
13:27 argument assume it to be zero so when
13:31 you say this inside reference function
13:33 argument when you assign a specific
13:36 argument of value that becomes a default
13:38 value for that argument
13:41 so now if you run it it will work fine
13:44 what this is saying is if you don't pass
13:47 me the second argument then I'm going to
13:50 assume it to be zero but hey you are
13:53 free to pass the second argument if you
13:55 pass this imagine what's gonna happen
13:58 it's not gonna make that zero let me run
14:00 it and prove it
14:04 excellent so you see here B is 8 and
14:07 this didn't have any effect so this will
14:10 have any effect only if you don't pass
14:14 the argument okay we want to conclude
14:18 our session by covering document strings
14:21 a real fast documentation strings are
14:23 nothing but a way to document your
14:25 function this function was very
14:27 simple it was just doing a sum of two
14:29 numbers but you might have a big
14:32 complicated function in that case you
14:33 want to write some documentation to
14:36 explain the color of that function on
14:40 what that function is doing what does it
14:42 expect as an input and what does it
14:45 expect as an output so the way you write
14:47 documentation string is you use triple
14:55 quotes like this and by default py charm
14:58 is giving these default documentation
15:00 but if you're not using py charm if
15:01 you're using either or something you
15:03 have to manually type all these things
15:04 in okay
15:08 so let me just remove these things and
15:11 manually type it in so this function
15:18 takes two arguments which are integer
15:27 numbers and it will return some of them
15:32 as an output so when you do this if
15:34 someone is calling a function if he
15:36 reads these lines he will he will get an
15:38 ID on what this function is doing so
15:41 this is called a documentation strings
15:45 and it is a multi-line strings started
15:47 and ended with triple quotes all right
15:50 we cover pretty much everything about
15:54 functions don't forget to work on these
15:58 sample exercises at the end of my every
16:02 video I give these sample exercises too
16:05 for you to work upon and these are
16:06 extremely useful and it will help you a