[no audio]
Friends, here we are going to
discuss about functions with the default arguments.
Now to get clarity on your functions with the default arguments,
let me start with the functions with some arguments.
Already we know how to pass an argument to your function.
Right. Let me open my editor.
Let me create a simple Python Script.
So I am going to save it as 'fun_with_default_arguments'.
Now I'm not going to read any value from command line,
just simply I am writing directly some values.
Let's say I have simply a function called 'display',
and assume that this is taking any variable value or argument,
right, positional argument.
Let me print("The value of
a is: ",', just I am printing that
value. That's fine.
Then simply I am returning 'None'.
[no audio]
Right. Now guys, we have a simple function called 'display',
and if I run that you're not getting any output because you're
not calling that. Now let me call that, right?
I am calling that with value called 4. Now see the result.
You can also call this function for any other value as well.
Right. But while calling be clear. While calling in case if
I do not pass any argument what it is giving?
"display() missing 1 required positional argument: 'a'."
Actually, if you observe here this is your defined function.
Whenever if you want to call this function, this function
needs one argument, but you are not passing from here.
Right. You're not passing that. So whenever if you are not
passing that, you will get an error like this. Now, in case
if you are not passing then I want to work with some default value.
If I pass, my function has to take that passed value.
In case if I'm not passing then my function has to work with
some default value.
Let's say I am taking 'a = 1'.
[no audio]
Now if I write 'a= 1', see that. Let me save it and
run it. You are not getting any error. And the default value
of 'a' is 1. Right. In case if you pass any value, your 'a' is
taking that value instead of this 'a = 1', right? See in fifth line
I'm calling your function with the value called 4, right?
Then 'a' value you are getting 4 here. Observe that. Then second
I'm calling same function with value 5.
Yes, you are getting. But third I'm not calling,
I'm calling without any arguments, then it is getting default
value called 1. That default value I have given here.
So this is the way how you can define default value for your
functions, or default argument, right?
Now, let me take one more function.
I will keep this function as it is for your practice.
Let me define one more function,
or 'def add_numbers'. Just assumption.
So if you know the concept, while going forward you can apply
whenever if you get a situation like this. So actually I
am going to define a function. This function needs two variables,
two arguments, let's say 'a,b'.
[no audio]
Right. And I want to find simply 'result',
[no audio]
it's nothing but 'a + b', and let me simply print directly "The result
is: ",
your 'result'. That's fine.
Then finally, let me 'return None'. As of now
I'm not expecting anything from my function.
Now let me call this function with two values.
Yes, it's working. But suppose I am calling that without any
arguments, but you are getting an error.
[no audio]
Suppose in my hand I have only one value,
let's say 5. I don't have second value.
Then you're getting an error.
So what I am doing is, suppose for second value
I am giving a default value called 1. Now see the result.
[no audio]
But based on situation you need to select a default value.
Whenever, if you are adding two numbers, right,
and if you don't know second number, what is the value you
need to add for first number? Generally 0. That's why I can
take here now 0.
So 5 + 0 = 5 only you are getting. I'm calling this function
with one value only. But what about second value?
So if it is addition, the second value should be 0. If it is
multiplication, the second value should be 1.
So I mean based on your requirement
you need to select default values, right.
Maybe some true, false, some path, whatever it may be.
Right. So if you want to take both the default values, yes you
can take it. Now see that,
I am calling my function without any arguments.
If you are not having any arguments, what is the result?
Always 0. If you don't have anything it is 0, suppose.
[no audio]
Right. But suppose if I give only one argument, so 1 is
default. But what about first value? You need to pass it.
[no audio]
And you need to follow some simple rule here. Suppose if I take
'a = 0' and then 'b' normally, see that, you are getting
some error called 'SyntaxError'.
That is, "non-default argument follows default argument".
That means always, first you need to, in case if you have some
default arguments you need to mention always your default
arguments only at the end while defining your function inside
of your parentheses.
I should not write like 'a = 0' and then 'b', but I can
write 'a,b = 0', means default value
whatever you have, you have to write at the end in your definition.
That is the meaning of this error, right. Now see that.
It's working fine.
Okay. So guys this is the way how you can define a default value.
Suppose I assume that I am going to work with some database, or I
am going to work with some operation on my Linux server.
Let's say I need a username for that.
[no audio]
'working_on_some()', operation, on something, right.
I need a 'user' for that. Suppose
If you don't pass any 'user', at least I want to take 'root' as my 'user'.
[no audio]
"working with", whatever the '' you have.
That's it.
Guys these are just assumptions. Anyway you will get full
clarity while writing real-time scripts.
So first we need to learn syntaxes.
That's what we are doing here.
So 'working_on_some()'. Now see that. What happened? "'root' is not
defined". Sorry. This is a string,
right. That is not a variable, that is a string. You need to
mention inside of quotations. "working with root". Suppose
if I know what is the user I want to work, suppose
I want to work with some 'weblogic_admin'.
Let's say '("weblogic_admin")'. Now it's working perfectly.
If you don't pass any 'user' it will take 'root' as the default
user, but if you pass it will take that passed 'user'.
So that is the usage of default arguments inside of your
functions. It's very, very important guys, okay.
Just try to remember the syntaxes, how we are working with
the default arguments.
That's it. Okay. Okay guys,
thank you for watching this video.
[no audio]