1 00:00:00,140 --> 00:00:08,640 In this tutorial we will see the different method of passing arguments into functions in Python. 2 00:00:08,640 --> 00:00:12,230 We will start with positional argument. 3 00:00:12,330 --> 00:00:16,690 Let's consider then an example for positional arguments. 4 00:00:16,710 --> 00:00:22,320 So I have defined a function your student with these parameters. 5 00:00:22,320 --> 00:00:25,980 Name each class and Mark. 6 00:00:26,700 --> 00:00:33,330 And within this function we are printing each of the arguments that we have received during the function 7 00:00:33,330 --> 00:00:33,600 call. 8 00:00:34,350 --> 00:00:47,030 So let's make a call to this function student by passing positional parameters and when passing positional 9 00:00:47,030 --> 00:00:52,990 parameter it is important to get the argument in the right order. 10 00:00:53,870 --> 00:01:05,280 That's because Python matches the position of the argument with the parameters defined in the function. 11 00:01:05,300 --> 00:01:13,310 So the forced argument that I'm going to parse is matched with the first parameter which just name. 12 00:01:13,520 --> 00:01:28,710 So the Fausto argument that I'm going to parse is the name Sam and then h B10 class is great. 13 00:01:28,720 --> 00:01:40,030 5 and then marks 70 when we make a function call when passing these bodies positional parameters Python 14 00:01:40,090 --> 00:01:50,290 matches this value some with the parameter name and then it goes to the next while you 10 it matches 15 00:01:50,440 --> 00:01:57,840 with the parameter age and then decode and default values and so on. 16 00:01:57,850 --> 00:02:06,250 So this is pretty straightforward but we have to make sure to get the arguments in the right order and 17 00:02:06,310 --> 00:02:08,140 if we mess up the order. 18 00:02:08,380 --> 00:02:10,240 This would result in an error. 19 00:02:10,630 --> 00:02:20,260 So let's execute this function in the Jupiter note so here is our function. 20 00:02:20,260 --> 00:02:28,610 Student in 4 and we are going to print the arguments received through the function call in this function. 21 00:02:28,750 --> 00:02:31,440 So let's make a call to this function. 22 00:02:31,440 --> 00:02:42,850 Student in 4 and then parse the parameters the argument so the first argument is Sam and then the h 23 00:02:42,910 --> 00:02:48,390 is in grade is Grade 5. 24 00:02:50,160 --> 00:02:56,820 And then the marks so let's execute this and we execute this. 25 00:02:57,070 --> 00:03:00,040 This is the output that we have got. 26 00:03:00,070 --> 00:03:09,750 Python has matched each of the values and the parameters and then displayed the values. 27 00:03:09,760 --> 00:03:16,840 Now let us change the order of the first and the last argument that we have passed. 28 00:03:16,900 --> 00:03:34,580 So letters use the first argument as 70 and the last argument as the name Sam and then execute this. 29 00:03:34,630 --> 00:03:44,410 So as you can see if you change the order of the positional parameters this will result in arrows unwanted 30 00:03:44,560 --> 00:03:52,370 arrows because Python matches the values to deepen amateurs by position. 31 00:03:52,400 --> 00:03:55,200 This is how the output would look like. 32 00:03:55,230 --> 00:04:05,040 Now so it is important to give the right position for each of the arguments. 33 00:04:05,060 --> 00:04:08,820 Now let's discuss about the second method. 34 00:04:08,830 --> 00:04:17,540 Keyword arguments Python allows us to use a syntax called keyword argument. 35 00:04:17,780 --> 00:04:22,680 In this case we can get the argument in any order. 36 00:04:23,240 --> 00:04:32,420 When we call the function as long as we use the name of the argument in the function call as you can 37 00:04:32,420 --> 00:04:40,160 see we have used the name of the argument when we are passing the argument. 38 00:04:40,370 --> 00:04:45,050 John H is equal to 10. 39 00:04:45,150 --> 00:04:55,910 Class is equal to grade 5 and so on because we are associating the parameter name the D values the order 40 00:04:55,910 --> 00:04:59,180 of the argument in the function call doesn't matter. 41 00:04:59,660 --> 00:05:09,180 When we add passing keyword arguments but in the case of positional arguments we pass the arguments 42 00:05:09,210 --> 00:05:17,050 to a function in the same ordered as these respective parameters in D function header. 43 00:05:17,070 --> 00:05:23,300 Now let's pass keyword argument to this function in the Jupiter notebook. 44 00:05:23,340 --> 00:05:24,650 In this function call. 45 00:05:24,690 --> 00:05:34,020 I have parsed keyword arguments to the function in to this function student info so let's execute this 46 00:05:35,150 --> 00:05:36,240 cell. 47 00:05:36,460 --> 00:05:37,850 And here is the out. 48 00:05:38,530 --> 00:05:46,330 So as discussed before the order of the argument in the function call doesn't matter when we are passing 49 00:05:46,330 --> 00:05:47,880 keyword arguments. 50 00:05:47,980 --> 00:05:49,750 So let 51 00:05:53,020 --> 00:06:02,110 give a different order for the argument that we have passed so in this function call I have given the 52 00:06:02,140 --> 00:06:05,000 first keyword argument as marks. 53 00:06:05,050 --> 00:06:10,490 And then it is the age and then name and great. 54 00:06:10,540 --> 00:06:17,290 So let's execute this cell and then see the output. 55 00:06:17,320 --> 00:06:23,650 So as you can see even though we have changed the order of the argument that we have passed to the function 56 00:06:23,650 --> 00:06:31,750 call Python matches the parameter name that devalues the order of the arguments and the function call 57 00:06:32,020 --> 00:06:34,000 doesn't matter. 58 00:06:34,000 --> 00:06:39,630 Next we will discuss about default argument in Python. 59 00:06:39,640 --> 00:06:49,870 We can define a function that default parameter values and this default value will be used when a function 60 00:06:49,930 --> 00:06:52,690 is invoked without any argument. 61 00:06:53,470 --> 00:06:56,920 So let's see this in the Jupiter non book. 62 00:06:56,920 --> 00:07:04,380 So here to define this function student info that some default parameter values. 63 00:07:04,450 --> 00:07:12,280 And next we will make a function call and when we make a function call without passing any argument 64 00:07:12,670 --> 00:07:20,920 the default values that we have defined using the function definition will be used when a function is 65 00:07:20,980 --> 00:07:24,010 invoked without any argument. 66 00:07:24,010 --> 00:07:32,850 And in order to specify default values for a parameter just specify the value using the assignment operator 67 00:07:33,070 --> 00:07:43,350 followed by the parameter name and defining default values for parameters is useful when we have a number 68 00:07:43,350 --> 00:07:52,400 of arguments in our function and some of these arguments almost always have the same value. 69 00:07:52,440 --> 00:08:02,130 Now after defining a function that default parameters I want to make a function call but with the different 70 00:08:02,430 --> 00:08:12,090 parameter value for the parameter name and all other values should remain the same in that case. 71 00:08:12,100 --> 00:08:21,620 Python allows us to make a function call by passing a single argument like this. 72 00:08:21,690 --> 00:08:27,980 So let make a function call. 73 00:08:28,030 --> 00:08:36,310 I only want to have the name changed and all other default while loose fixed. 74 00:08:36,400 --> 00:08:43,070 In that case I can only parsed a single argument like this and then execute. 75 00:08:43,090 --> 00:08:52,400 So as you can see the first value that we have parsed here has been mapped to the first parameter and 76 00:08:52,460 --> 00:09:00,500 all are the default values have been picked from the function definition and also we can mix positional 77 00:09:00,500 --> 00:09:06,930 arguments and keyword arguments in a function called see this example. 78 00:09:08,390 --> 00:09:12,640 So I'm gonna pass positional argument like this. 79 00:09:12,670 --> 00:09:23,330 I'm going to pass muster as the first positional argument and then I'm going to see the marks. 80 00:09:23,660 --> 00:09:28,370 Hundred then let's execute this. 81 00:09:28,480 --> 00:09:39,040 So the first argument that we have past has been matched to the first parameter name and then the second 82 00:09:39,160 --> 00:09:46,020 and the third parameters have been taken from the default values that we have defined. 83 00:09:46,180 --> 00:09:55,690 And the last parameter has been matched to the last argument is picked from the keyword argument that 84 00:09:55,690 --> 00:09:57,360 we have passed in the function call. 85 00:09:57,700 --> 00:10:08,320 So here there is a mix of positional and keyword arguments when passing positional and keyword arguments 86 00:10:08,410 --> 00:10:10,110 in a function call. 87 00:10:10,150 --> 00:10:18,100 You need to remember that positional arguments must appear before any keyword arguments. 88 00:10:18,100 --> 00:10:25,890 In this example I have past three arguments that are two positional arguments and that is one keyword 89 00:10:25,940 --> 00:10:26,820 arguments. 90 00:10:26,980 --> 00:10:35,440 The requirement when we make a function call when passing both positional and keyword arguments is positional 91 00:10:35,530 --> 00:10:39,390 arguments have to be followed by the keyword arguments. 92 00:10:39,430 --> 00:10:49,060 But in this case the last argument that we have past is a positional argument and hence we have a syntax 93 00:10:49,150 --> 00:10:51,930 error when we execute this code. 94 00:10:51,940 --> 00:10:54,850 This brings us to the end of this tutorial.