1 00:00:02,420 --> 00:00:13,360 Hello welcome to this tutorial on functions so a function is a block of code that performs a specific 2 00:00:13,360 --> 00:00:14,600 task. 3 00:00:14,650 --> 00:00:26,410 Python allows us to define functions according to our need known as user defined function and a python 4 00:00:26,410 --> 00:00:31,600 function may or may not have a name. 5 00:00:31,770 --> 00:00:36,870 And in this tutorial we will talk about functions which have a name 6 00:00:40,400 --> 00:00:48,590 and here does these syntax for defining a function and a function counters of two. 7 00:00:48,600 --> 00:01:01,960 But the head and the body function head the dot with Dickie Bird D E F which is the short form for definition 8 00:01:02,950 --> 00:01:13,960 and this keyword is followed by the function name and the function name can be any valid identifier 9 00:01:15,300 --> 00:01:19,710 and after the function name we have a list of arguments 10 00:01:23,350 --> 00:01:37,900 inside but and this is separated by comma and we use these arguments to parse required a data to a function 11 00:01:39,160 --> 00:01:44,700 and a function can take any number of arguments odd none at all. 12 00:01:45,810 --> 00:01:56,170 If a function doesn't take any argument then the parent misses is left empty the function header and 13 00:01:56,170 --> 00:02:00,220 the body separated by a call in here. 14 00:02:01,590 --> 00:02:14,160 After a debate and this is in the next line we have a block of statements or the function body the function 15 00:02:14,160 --> 00:02:21,230 body contains statements which defined what the function does. 16 00:02:21,390 --> 00:02:28,140 Remember all these statements in the function body must be equally indented. 17 00:02:29,010 --> 00:02:42,410 Otherwise we will get a syntax error as you can see all of these statements are equally indented Python 18 00:02:42,530 --> 00:02:50,810 uses indentation of statements to determine when blogs start and end 19 00:02:54,240 --> 00:02:55,500 the definition. 20 00:02:55,500 --> 00:02:58,380 The function definition ends there. 21 00:02:58,380 --> 00:03:00,590 The indentation ends. 22 00:03:00,690 --> 00:03:06,870 So as you can see with this statement The indentation has ended. 23 00:03:08,720 --> 00:03:13,550 So does the function definition the print statement he would 24 00:03:16,170 --> 00:03:21,530 is define out of the function. 25 00:03:21,550 --> 00:03:26,340 So here's an example off of user defined function. 26 00:03:26,410 --> 00:03:34,650 So I have used the key word D E F to define the function great. 27 00:03:34,900 --> 00:03:40,370 And this particular function does not take any argument. 28 00:03:40,540 --> 00:03:43,240 The parenthesis is left empty. 29 00:03:44,320 --> 00:03:52,670 Next in the function body I have a print function which prints the string hello. 30 00:03:53,110 --> 00:04:04,560 This is the function definition but a function definition does nothing by its an if we want to use the 31 00:04:04,560 --> 00:04:05,340 function. 32 00:04:05,340 --> 00:04:12,240 We must call it in order to execute the code inside the function definition. 33 00:04:12,240 --> 00:04:19,220 We need to call a function and the correct term is a function call. 34 00:04:19,230 --> 00:04:24,300 Here it is how you call a function your function name is great. 35 00:04:25,550 --> 00:04:34,740 And then you have deep it and this is and since the function greed does not take me any argument in 36 00:04:34,740 --> 00:04:44,940 the function call as well you do not pass any arguments to the function remember the function call must 37 00:04:45,060 --> 00:04:49,220 appear after the function is defined. 38 00:04:49,350 --> 00:05:00,820 You define your function first and then make a function called and you will encounter a name error exception 39 00:05:03,200 --> 00:05:12,650 you have your function call your ID and when a function is call the program control jumps to that function 40 00:05:12,650 --> 00:05:18,900 definition and executes this statement inside the function body. 41 00:05:19,010 --> 00:05:28,250 Once you make a function call the control jumps to this point in the program and after executing the 42 00:05:28,250 --> 00:05:37,600 body of the function the program control jumps back to the part of the program which going to function. 43 00:05:37,640 --> 00:05:46,500 So after all of these statements here and executed in the function body the control jumps back to this 44 00:05:46,500 --> 00:05:52,410 point in the program and it assumes execution at that point. 45 00:05:53,630 --> 00:06:01,760 Let us now execute the simple function in the Jupiter notebook. 46 00:06:01,780 --> 00:06:07,030 This is of a function definition as you can see here. 47 00:06:07,150 --> 00:06:19,690 The statement in the function body has been indented now in order to end the function definition the 48 00:06:19,690 --> 00:06:24,370 indentation should also end the next statement. 49 00:06:24,370 --> 00:06:33,520 Another example is a function call to the function greet call the function read using this statement 50 00:06:33,520 --> 00:06:43,340 here let execute the cell and decode inside the function greed has been executed. 51 00:06:44,630 --> 00:06:47,390 And here is another example. 52 00:06:47,510 --> 00:06:58,770 We have a function which takes two arguments a and b and within the function body we are adding the 53 00:06:58,770 --> 00:07:09,110 arguments a and b and assigning the result to a variable C and finally we are printing the value of 54 00:07:09,110 --> 00:07:14,340 debatable C and in the function call. 55 00:07:14,460 --> 00:07:25,650 I have parse the argument seven and eight execute this code in the Jupiter notebook function definition 56 00:07:26,070 --> 00:07:27,850 for the function. 57 00:07:28,730 --> 00:07:35,770 And we are passing the argument seven and eight to the function using a function called blitz. 58 00:07:35,830 --> 00:07:42,630 Execute this piece of code the function has added the argument seven and eight men. 59 00:07:42,640 --> 00:07:53,480 But in today's result no a function can also have an optional that don't statement. 60 00:07:54,940 --> 00:08:06,540 Up to this bind we have been discussing or creating functions which do not return any values and when 61 00:08:06,540 --> 00:08:17,790 a written statement is encountered inside a function the function terminate and the value of the expression 62 00:08:18,270 --> 00:08:28,290 followed by the the return keyword is sent back to the part of the program that call the function and 63 00:08:28,290 --> 00:08:38,050 the return statement can appear anywhere in the body of the function and the functions which returns 64 00:08:38,190 --> 00:08:42,640 why use are known as why you the daunting functions 65 00:08:45,050 --> 00:08:52,130 functions which do not return any values are also known as wide functions. 66 00:08:52,130 --> 00:08:55,110 So let's discuss an example. 67 00:08:55,430 --> 00:09:06,340 I have defined two functions the function and here takes in two arguments a and b and both of these 68 00:09:06,430 --> 00:09:14,360 arguments and returned the value nixed in my second function. 69 00:09:14,600 --> 00:09:28,810 Also known as + this function also takes two arguments A and B and D two argument but in this particular 70 00:09:28,810 --> 00:09:33,370 function we are printing the result. 71 00:09:35,450 --> 00:09:38,710 So what does it differentiate as we have discussed. 72 00:09:39,020 --> 00:09:51,360 This function is called O value the turning function and the second function is known as a wide function. 73 00:09:51,460 --> 00:10:01,920 We do not have the written statement in this function so let us define both of these functions in the 74 00:10:01,920 --> 00:10:10,590 Jupiter notebook and see the differences between value returning function and avoid function D function 75 00:10:10,710 --> 00:10:14,110 and which has the return statement. 76 00:10:14,430 --> 00:10:17,330 So let execute this piece of code. 77 00:10:18,670 --> 00:10:27,040 So the function has added to arguments and adorned the sum of the two arguments. 78 00:10:27,040 --> 00:10:36,180 Now let's also see an example of a wide function we want to define the function. 79 00:10:36,180 --> 00:10:39,980 And so this is the body of the function. 80 00:10:40,060 --> 00:10:47,770 We will now make a function call bypassing the argument forward and fight to this function. 81 00:10:47,780 --> 00:10:51,220 Let's execute this piece of code. 82 00:10:51,400 --> 00:11:02,170 The output of this function is 9 and the value added Dawn by the function and is also 9 in order to 83 00:11:02,170 --> 00:11:05,480 understand the difference between these two functions. 84 00:11:05,620 --> 00:11:17,280 Letters assign this function call in this scene to a variable see in this one line we are assigning 85 00:11:17,280 --> 00:11:29,340 the function card to a variable C meaning the value returned by this particular function is assigned 86 00:11:29,730 --> 00:11:35,280 to this variability C the output in this cell. 87 00:11:35,500 --> 00:11:45,440 Let's add another statement so we will use a print function to print the value of the variable C so 88 00:11:45,440 --> 00:11:51,610 let's execute this the value of the variability is nine. 89 00:11:51,670 --> 00:12:00,840 That means the result of this particular function has been assigned to this variable C and finally began 90 00:12:00,840 --> 00:12:02,410 C devalue here. 91 00:12:02,570 --> 00:12:06,490 Now let us check these type of the variable C 92 00:12:09,810 --> 00:12:13,310 the variable C here is an integer. 93 00:12:13,320 --> 00:12:19,110 This function good has returned an integer data type. 94 00:12:19,110 --> 00:12:27,800 Now let's go to this function which contains a print function in the function body here as well. 95 00:12:28,100 --> 00:12:33,500 We will assign a variable to the function called 96 00:12:36,970 --> 00:12:41,160 and execute this say the code in this cell. 97 00:12:44,030 --> 00:12:49,940 This function has been executed with this function call. 98 00:12:50,360 --> 00:12:55,280 And then the function body has a print function. 99 00:12:55,280 --> 00:12:59,890 The charge these arguments a and b as you can see the output here. 100 00:13:00,080 --> 00:13:08,290 Now let us check the type of the variable D which has been assigned to a function call. 101 00:13:09,020 --> 00:13:12,480 So let's check the data type of variable. 102 00:13:12,610 --> 00:13:20,380 The add function here which does not contain a return statement has returned the data type. 103 00:13:20,630 --> 00:13:31,880 None none the print function does not assign value to a variable and it doesn't return a value from 104 00:13:32,200 --> 00:13:33,020 a function called. 105 00:13:33,320 --> 00:13:41,790 So that is the reason the data type of this variable is of none type. 106 00:13:41,870 --> 00:13:44,680 Now let us consider another example here. 107 00:13:44,690 --> 00:13:52,230 We have defined a function test which has a written statement in the function body. 108 00:13:52,250 --> 00:13:56,420 There is no expression after the return keyword. 109 00:13:57,200 --> 00:14:02,220 So the expression followed by the return keyword is omitted. 110 00:14:02,270 --> 00:14:09,530 In this case the value none is returned by this function. 111 00:14:09,560 --> 00:14:12,450 So let's execute this cell. 112 00:14:12,500 --> 00:14:16,190 This particular function does not return anything. 113 00:14:16,430 --> 00:14:28,060 Let's assign this function call to a variable and then check the type of debatable x. 114 00:14:28,430 --> 00:14:34,280 So this is also the darned the special value none. 115 00:14:34,310 --> 00:14:41,030 Even if you have other done statement within the function body but if the expression is omitted the 116 00:14:41,120 --> 00:14:43,700 return value would be none. 117 00:14:44,690 --> 00:14:53,270 So consider another example here we have a function at 80 which returns the sum of the argument a and 118 00:14:53,270 --> 00:14:54,730 b. 119 00:14:54,740 --> 00:15:04,570 Now I want to use the desired of the value returned by this function. 120 00:15:04,600 --> 00:15:11,100 In short I want to multiply the result of the return value by minus one. 121 00:15:11,800 --> 00:15:23,530 So in order to do that I can assign this particular function call to a variable C I want to multiply 122 00:15:23,560 --> 00:15:26,510 the result by minus one. 123 00:15:26,920 --> 00:15:36,550 So the result is multiplied by minus 1 and then assigned to this variable C and then finally we will 124 00:15:36,550 --> 00:15:42,980 print debatable C here so let's execute this piece of code. 125 00:15:43,420 --> 00:15:53,300 The function D has added these two numbers A and B and then multiplied by minus 1. 126 00:15:53,350 --> 00:16:02,290 Assign this value to debatable C and finally printed this variable we can use the values written by 127 00:16:02,350 --> 00:16:04,600 a function in a program. 128 00:16:04,660 --> 00:16:06,180 Using this example. 129 00:16:06,280 --> 00:16:13,900 See this example here we have defined a function and and it returns this expression. 130 00:16:14,980 --> 00:16:21,320 And in the function call here we have two arguments. 131 00:16:21,520 --> 00:16:24,220 One is this value. 132 00:16:24,340 --> 00:16:32,630 And the other is the function at which is adding the numbers 2 and 3. 133 00:16:32,820 --> 00:16:41,740 A function can also parsed as a parameter in this particular function call D and function within the 134 00:16:41,740 --> 00:16:42,380 parents. 135 00:16:42,370 --> 00:16:47,160 This is executed first and the at dawn by D. 136 00:16:47,160 --> 00:16:52,170 Function is parsed as an argument to the function call. 137 00:16:52,420 --> 00:16:57,050 And here let let's execute this piece of code. 138 00:16:57,190 --> 00:17:00,310 So this is about the return statement.