0% found this document useful (0 votes)
7 views

4.3 - Function arguments.mp4

functions

Uploaded by

puneeth369369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

4.3 - Function arguments.mp4

functions

Uploaded by

puneeth369369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

So now a very important aspect of functions are called function arguments.

And function
arguments are basically those variables that you pass into a function. For example, in this
case, name and Msg are function arguments. Now there are some interesting operations and
some interesting ways of processing function arguments. Let's look at an example. So here
I've written a function called greet. What it does is it takes two variables or two arguments.
The first argument is called name. The second argument is called message. And what it
prints is it basically prints. So for example, if I say greet, I give a name, which is satish, one of
our engineers. And if my message was good morning, what it prints for me in this simple,
using the simple format statement, it prints hello Satish, good morning. Okay, so that's what
this function does. It takes two parameters or two arguments. It takes a name argument and
a message argument. Now, since it has two arguments, what if I only pass one argument? It
is expected to take two arguments. But imagine if I just say greet satish, what happens? You
will get an error. And what is that error? If you read the error, it says greet missing one
required positional argument called message. It says you're missing the message part and
hence I'm raising an error. Okay, now one way to get rid of this is by using something called
default arguments. Let's understand what a default argument looks like. Right? Imagine I'm
just changing my definition of this function slightly. What am I doing? I'm keeping the name
as is. But message when I'm declaring my function here, when I'm defining my function.
Sorry, what I'm saying here is the default message when I give something like this, what it
means is that the default message, that the default message is good morning. Now let's see
what happens if I say greet satish goodnight. Then what will do? It will use this as name and
it will use this as message and it will print hello Satish, good night. But what happens if you
just say greet Satish without the second argument here? Look at it, look here, there is no
message. There is no message argument here. When there is no message argument, the
default argument will take the value that is present here, which is good morning, which you
are giving when you're declaring or defining your function. So in this case there is no
message argument. So it takes the default value, which is good morning here, and it prints
hello Satish, good morning. So if you have a variable which is not compulsory, so message
here is not mandatory, right? It's not compulsory to be there, then you can create a default
argument. It's good to create a default argument. And here the default argument is message
with a value of good morning. Now, here comes a tricky thing. There is a small boundary
case here. Imagine if I did this. If I said define greet, the first value. If I say, if I give is the
message is the default value, and this is the non default value argument. Sorry. If this is the
non default argument, and if this is the default argument, this is the default. Now, if I say
greet, if I just call this function as greet satish, what happens now? It still tries to give the
first value here, satish to message, and it says there is no value for name. So it's always a
good programming practice to avoid syntax errors like this, to have non default arguments
before. So this is an error because non default argument is following a default argument. So
as a good programming practice, always have default arguments, default arguments at the
end of your arguments. At the end of your arguments. Right. So just like here, just like here,
your default argument, you could have multiple arguments here, right. Your default
argument should come after your non default arguments. That's the right programming
practice to make the most of your default arguments. Okay, now comes an other type of
arguments. These are very, very interesting. I really like the keyword arguments. Okay, let's
understand how the keyword argument works. Okay, so let's see how the function is called.
First, I'm calling the same function, greet. But what I'm passing here is sort of like a
dictionary, if you want to think about it. Okay, sort of like it. Okay, not exactly a dictionary,
but sort of like it. Let's see what I'm saying. I'm saying my name equals to satish and my
message equals to good morning. Okay, so I'm giving a keyword and a value. So these are
called keywords and values. I'm giving a keyword and a value. Okay, I'm saying name equals
to satish, meshes equals to good morning now. And I can give arbitrarily anything. I can give
anything that I want here. Now let's see how they are used internally. So now let's look at
our function definition. Same greet. Here you have double star keyword args. Okay, so these
are called keyword arguments. This is nothing but KW args is to be read as keyword
arguments because I'm giving keywords, I'm using keywords to give arguments. Now, if you
want to access anything, you can say keyword args name. What it gives you is whatever is
the value that you have for name here. It will return that value. Here or keyword args
message. So if you've given whatever message, whatever value you gave to the message, to
the message keyword, you'll get it here, and the final result is still the same. Right? So if you
have a variable list of arguments that you want to pass, if you have a variable length of
arguments to a function, and if you want to have it free form, see, this makes it much more
readable here. Keyword arguments is the way to go. Very, very simple example. Play with
this example, modify it so that you understand what's happening. Next comes something
called as arbitrary arguments, wherein you have arbitrary number of arguments. For
example, even here, right? My number of arguments is well defined, sort of. Now imagine
the other way around. So let's see how the function is called here. Okay? So imagine if I say
greet and I'm giving four arguments. I could have given five or ten or two or any number
here. Okay, there is no fixed number here. What it prints for me is, hello Sri Satish. Hello
Murali. Hello Naveen. Hello Srikanth. Let's see how it does it. Now, this is where I have my
function definition here. Instead of using double star, I'm using a single star and I'm calling
it names. Okay? So what happens is all the values that you enter here, this variable length or
arbitrary length, this is arbitrary length. Variables are now input into your function using
something called a tuple, your data structured. So when I say print names, see what I get.
When I say print names, I basically get a tuple. This is a tuple, right? Basically get a tuple of
all the arguments that I have passed here. And now I can print this by just writing a simple
for loop for name in names, print format name. Right? So what is doing is when I have an
arbitrary length of variables by using this syntax, which is a single stars and variable name,
what I'm getting here is I'm taking all of my arguments, arbitrary length arguments, and I'm
giving it to the function, I'm giving it to a function as a tuple. And I can do anything with this
in the tuple, not a list, but a tuple, remember? Right. So when you have arbitrary length
arguments to pass to functions, you could use this type of hack, and it works very well.
Okay, my favorite always has been keyword arguments, because it makes a code much more
readable for me. Lot of standard libraries use keyword arguments extensively in python.

You might also like