1 00:00:00,000 --> 00:00:01,200 [no audio] 2 00:00:01,200 --> 00:00:02,300 Friends, here we are going to 3 00:00:02,300 --> 00:00:05,600 discuss about functions with the default arguments. 4 00:00:05,600 --> 00:00:10,100 Now to get clarity on your functions with the default arguments, 5 00:00:10,100 --> 00:00:12,700 let me start with the functions with some arguments. 6 00:00:12,800 --> 00:00:16,100 Already we know how to pass an argument to your function. 7 00:00:16,500 --> 00:00:18,700 Right. Let me open my editor. 8 00:00:19,900 --> 00:00:21,900 Let me create a simple Python Script. 9 00:00:22,000 --> 00:00:28,600 So I am going to save it as 'fun_with_default_arguments'. 10 00:00:28,900 --> 00:00:32,700 Now I'm not going to read any value from command line, 11 00:00:32,700 --> 00:00:34,800 just simply I am writing directly some values. 12 00:00:35,400 --> 00:00:39,300 Let's say I have simply a function called 'display', 13 00:00:40,700 --> 00:00:45,400 and assume that this is taking any variable value or argument, 14 00:00:45,400 --> 00:00:47,200 right, positional argument. 15 00:00:47,200 --> 00:00:50,000 Let me print("The value of 16 00:00:52,400 --> 00:00:54,500 a is: ",', just I am printing that 17 00:00:54,500 --> 00:00:55,500 value. That's fine. 18 00:00:55,800 --> 00:00:57,600 Then simply I am returning 'None'. 19 00:00:57,600 --> 00:00:59,400 [no audio] 20 00:00:59,400 --> 00:01:03,200 Right. Now guys, we have a simple function called 'display', 21 00:01:03,500 --> 00:01:05,900 and if I run that you're not getting any output because you're 22 00:01:05,900 --> 00:01:08,900 not calling that. Now let me call that, right? 23 00:01:08,900 --> 00:01:13,200 I am calling that with value called 4. Now see the result. 24 00:01:13,200 --> 00:01:16,600 You can also call this function for any other value as well. 25 00:01:17,800 --> 00:01:22,500 Right. But while calling be clear. While calling in case if 26 00:01:22,500 --> 00:01:25,900 I do not pass any argument what it is giving? 27 00:01:26,500 --> 00:01:30,600 "display() missing 1 required positional argument: 'a'." 28 00:01:30,600 --> 00:01:33,700 Actually, if you observe here this is your defined function. 29 00:01:34,000 --> 00:01:37,000 Whenever if you want to call this function, this function 30 00:01:37,000 --> 00:01:40,000 needs one argument, but you are not passing from here. 31 00:01:41,700 --> 00:01:44,900 Right. You're not passing that. So whenever if you are not 32 00:01:44,900 --> 00:01:48,900 passing that, you will get an error like this. Now, in case 33 00:01:48,900 --> 00:01:53,400 if you are not passing then I want to work with some default value. 34 00:01:53,400 --> 00:01:57,100 If I pass, my function has to take that passed value. 35 00:01:57,100 --> 00:02:00,700 In case if I'm not passing then my function has to work with 36 00:02:00,700 --> 00:02:01,800 some default value. 37 00:02:01,900 --> 00:02:04,200 Let's say I am taking 'a = 1'. 38 00:02:04,200 --> 00:02:06,100 [no audio] 39 00:02:06,100 --> 00:02:09,300 Now if I write 'a= 1', see that. Let me save it and 40 00:02:09,300 --> 00:02:13,699 run it. You are not getting any error. And the default value 41 00:02:13,699 --> 00:02:19,700 of 'a' is 1. Right. In case if you pass any value, your 'a' is 42 00:02:19,700 --> 00:02:24,200 taking that value instead of this 'a = 1', right? See in fifth line 43 00:02:24,200 --> 00:02:28,100 I'm calling your function with the value called 4, right? 44 00:02:28,100 --> 00:02:32,000 Then 'a' value you are getting 4 here. Observe that. Then second 45 00:02:32,000 --> 00:02:33,900 I'm calling same function with value 5. 46 00:02:33,900 --> 00:02:36,400 Yes, you are getting. But third I'm not calling, 47 00:02:36,400 --> 00:02:40,700 I'm calling without any arguments, then it is getting default 48 00:02:40,700 --> 00:02:43,300 value called 1. That default value I have given here. 49 00:02:44,000 --> 00:02:48,300 So this is the way how you can define default value for your 50 00:02:48,300 --> 00:02:52,300 functions, or default argument, right? 51 00:02:52,500 --> 00:02:55,200 Now, let me take one more function. 52 00:02:56,300 --> 00:02:59,300 I will keep this function as it is for your practice. 53 00:03:00,200 --> 00:03:02,400 Let me define one more function, 54 00:03:03,900 --> 00:03:08,500 or 'def add_numbers'. Just assumption. 55 00:03:09,300 --> 00:03:11,900 So if you know the concept, while going forward you can apply 56 00:03:11,900 --> 00:03:15,500 whenever if you get a situation like this. So actually I 57 00:03:15,500 --> 00:03:19,900 am going to define a function. This function needs two variables, 58 00:03:20,000 --> 00:03:22,000 two arguments, let's say 'a,b'. 59 00:03:22,000 --> 00:03:24,000 [no audio] 60 00:03:24,000 --> 00:03:27,100 Right. And I want to find simply 'result', 61 00:03:27,100 --> 00:03:30,300 [no audio] 62 00:03:30,300 --> 00:03:35,100 it's nothing but 'a + b', and let me simply print directly "The result 63 00:03:35,100 --> 00:03:36,100 is: ", 64 00:03:37,700 --> 00:03:39,500 your 'result'. That's fine. 65 00:03:39,500 --> 00:03:44,200 Then finally, let me 'return None'. As of now 66 00:03:44,200 --> 00:03:46,200 I'm not expecting anything from my function. 67 00:03:46,900 --> 00:03:51,200 Now let me call this function with two values. 68 00:03:51,200 --> 00:03:58,300 Yes, it's working. But suppose I am calling that without any 69 00:03:58,300 --> 00:04:00,000 arguments, but you are getting an error. 70 00:04:00,000 --> 00:04:01,900 [no audio] 71 00:04:01,900 --> 00:04:03,800 Suppose in my hand I have only one value, 72 00:04:04,300 --> 00:04:06,600 let's say 5. I don't have second value. 73 00:04:07,300 --> 00:04:09,200 Then you're getting an error. 74 00:04:10,000 --> 00:04:12,400 So what I am doing is, suppose for second value 75 00:04:12,400 --> 00:04:15,700 I am giving a default value called 1. Now see the result. 76 00:04:15,700 --> 00:04:18,300 [no audio] 77 00:04:18,300 --> 00:04:20,600 But based on situation you need to select a default value. 78 00:04:20,600 --> 00:04:23,600 Whenever, if you are adding two numbers, right, 79 00:04:23,600 --> 00:04:26,000 and if you don't know second number, what is the value you 80 00:04:26,000 --> 00:04:28,300 need to add for first number? Generally 0. That's why I can 81 00:04:28,300 --> 00:04:29,300 take here now 0. 82 00:04:31,100 --> 00:04:34,300 So 5 + 0 = 5 only you are getting. I'm calling this function 83 00:04:34,600 --> 00:04:38,000 with one value only. But what about second value? 84 00:04:39,000 --> 00:04:41,600 So if it is addition, the second value should be 0. If it is 85 00:04:41,600 --> 00:04:43,900 multiplication, the second value should be 1. 86 00:04:44,500 --> 00:04:46,100 So I mean based on your requirement 87 00:04:46,100 --> 00:04:48,600 you need to select default values, right. 88 00:04:48,600 --> 00:04:51,500 Maybe some true, false, some path, whatever it may be. 89 00:04:53,000 --> 00:04:55,900 Right. So if you want to take both the default values, yes you 90 00:04:55,900 --> 00:04:57,500 can take it. Now see that, 91 00:04:57,500 --> 00:05:00,300 I am calling my function without any arguments. 92 00:05:00,400 --> 00:05:02,500 If you are not having any arguments, what is the result? 93 00:05:02,500 --> 00:05:05,000 Always 0. If you don't have anything it is 0, suppose. 94 00:05:05,000 --> 00:05:07,000 [no audio] 95 00:05:07,300 --> 00:05:12,200 Right. But suppose if I give only one argument, so 1 is 96 00:05:12,200 --> 00:05:15,100 default. But what about first value? You need to pass it. 97 00:05:15,100 --> 00:05:18,900 [no audio] 98 00:05:18,900 --> 00:05:22,000 And you need to follow some simple rule here. Suppose if I take 99 00:05:22,000 --> 00:05:28,400 'a = 0' and then 'b' normally, see that, you are getting 100 00:05:28,400 --> 00:05:29,900 some error called 'SyntaxError'. 101 00:05:29,900 --> 00:05:33,900 That is, "non-default argument follows default argument". 102 00:05:34,200 --> 00:05:37,500 That means always, first you need to, in case if you have some 103 00:05:37,500 --> 00:05:40,300 default arguments you need to mention always your default 104 00:05:40,300 --> 00:05:43,800 arguments only at the end while defining your function inside 105 00:05:43,800 --> 00:05:44,900 of your parentheses. 106 00:05:45,700 --> 00:05:50,100 I should not write like 'a = 0' and then 'b', but I can 107 00:05:50,100 --> 00:05:53,700 write 'a,b = 0', means default value 108 00:05:53,700 --> 00:05:56,800 whatever you have, you have to write at the end in your definition. 109 00:05:57,900 --> 00:06:01,300 That is the meaning of this error, right. Now see that. 110 00:06:01,300 --> 00:06:02,300 It's working fine. 111 00:06:03,300 --> 00:06:08,900 Okay. So guys this is the way how you can define a default value. 112 00:06:08,900 --> 00:06:13,000 Suppose I assume that I am going to work with some database, or I 113 00:06:13,000 --> 00:06:16,700 am going to work with some operation on my Linux server. 114 00:06:17,500 --> 00:06:20,000 Let's say I need a username for that. 115 00:06:20,000 --> 00:06:22,100 [no audio] 116 00:06:22,100 --> 00:06:27,500 'working_on_some()', operation, on something, right. 117 00:06:27,500 --> 00:06:29,200 I need a 'user' for that. Suppose 118 00:06:29,200 --> 00:06:32,400 If you don't pass any 'user', at least I want to take 'root' as my 'user'. 119 00:06:32,400 --> 00:06:34,900 [no audio] 120 00:06:34,900 --> 00:06:38,300 "working with", whatever the '' you have. 121 00:06:39,600 --> 00:06:40,600 That's it. 122 00:06:41,600 --> 00:06:44,100 Guys these are just assumptions. Anyway you will get full 123 00:06:44,100 --> 00:06:46,600 clarity while writing real-time scripts. 124 00:06:47,300 --> 00:06:48,900 So first we need to learn syntaxes. 125 00:06:49,100 --> 00:06:50,100 That's what we are doing here. 126 00:06:50,600 --> 00:06:56,100 So 'working_on_some()'. Now see that. What happened? "'root' is not 127 00:06:56,100 --> 00:06:57,400 defined". Sorry. This is a string, 128 00:06:57,400 --> 00:06:59,700 right. That is not a variable, that is a string. You need to 129 00:06:59,700 --> 00:07:02,900 mention inside of quotations. "working with root". Suppose 130 00:07:02,900 --> 00:07:05,700 if I know what is the user I want to work, suppose 131 00:07:05,700 --> 00:07:08,200 I want to work with some 'weblogic_admin'. 132 00:07:08,200 --> 00:07:11,700 Let's say '("weblogic_admin")'. Now it's working perfectly. 133 00:07:12,000 --> 00:07:14,300 If you don't pass any 'user' it will take 'root' as the default 134 00:07:14,300 --> 00:07:17,200 user, but if you pass it will take that passed 'user'. 135 00:07:17,200 --> 00:07:20,700 So that is the usage of default arguments inside of your 136 00:07:20,700 --> 00:07:24,000 functions. It's very, very important guys, okay. 137 00:07:24,200 --> 00:07:28,100 Just try to remember the syntaxes, how we are working with 138 00:07:28,100 --> 00:07:29,300 the default arguments. 139 00:07:29,300 --> 00:07:32,100 That's it. Okay. Okay guys, 140 00:07:32,200 --> 00:07:33,600 thank you for watching this video. 141 00:07:33,600 --> 00:07:38,700 [no audio]