Parameterized Functions | Python for Beginners [31 of 44]

>>So now you’ve learned how
to create basic functions. Let’s look at how we can play
with the parameters we passed into those functions
because there are few things we can do in that space. You already saw that we can
create a function which accepts a parameter and that
can return values. So just to review, we’d seen we created a function
here called Get initial. When you call get initial,
you pass in a name. So here we have the call
and our code that’s calling get initial and it
passes on a first-name. Whatever is passed in
gets used in the code, wherever you see
the Word name and when this function is completed
it returns a value, and that value that’s returned when I call the function I have
to have a place to put it. So in this case when
I call get initial, the value I receive back
from the function gets put into the variable first name
initial. All right. One of the things I’ve realized in my last one when I was writing
this function is that, maybe if somebody enters their
name in lowercase letters, I still want initials to be returned an uppercase letters when I’m maybe if I’m using this said to create
a user ID or an e-mail address. So I’ve added this.upper here to always make sure V initial
returns an uppercase. That I think makes a lot
of sense if we’re using it to extract initials for user ID. But what if it’s an e-mail address? E-mail addresses I don’t
always want uppercase letters. So I want sometimes I want you to be able to say
make it uppercase, but maybe not always. Well, it’s okay because
I can actually pass multiple parameters to my function. So what I can do is I add
a second parameter called force uppercase and when
you call my function, you tell me whether you
want false, don’t force. The initial be uppercase or true, do force the initial be uppercase
and then inside my function, I say if you said I wanted
to forced uppercase to true, then return it forced uppercase otherwise whatever case
was passed in. So you can pass multiple
parameters to a function, but make sure if you do
have multiple parameters, make sure you pass the parameters
in the order they’re declared. So you’ll notice I have
the parameter name and the parameter force uppercase
when I call it I pass in the first name the value for
name and I pass him false. The value for force
uppercase in that order. So now I have
a little more flexibility in what I can do with my function. It can’t get a little confusing
though when you’re worrying about the order and all these
different situations. So we’ll see some more
tricks we can do. One of the things we can do is we can specify a default
value for a parameter. Now what’s neat about this is
I might say you know most of the time you probably want me to always return
initials and uppercase. So what I’m going to do is say that the force uppercase is
defaulting to true. So what that means is if you don’t give me a value for force uppercase, I’m always going to say
true for force uppercase. So now when I call
the function in this case, you’ll see I didn’t even pass
in a value for force uppercase. I can pass in a value if I want to. I can pass in the value true. I can pass in value false or I
might just say you know what, I’m going to give you a
value first name just use the default for
the other parameters. This is another one of
the things you’ll see a lot when you’ll get
tutorials and blog posts. You’ll be like, how come
over here when they call it, they passed in four or five values
and over here when I called it, I only passed in one value. What’s going on? What’s happened is the person who created the function has
some default values for the parameters and it may be that the default values worked
fine for a certain situation. So when you’re calling a parameter looking up documentation
for functions, take a look and see if some of the
parameters have default values. If they do, that means you don’t have to pass in a value for
them. It’s optional. Something else we can
do that I find is a really good habit because
it makes your code a lot more readable is to do what’s called Named Notation when you
pass in parameters. Earlier I said you have to pass in the parameters in the order
in which they’re defined. So if I have a parameter name
in force uppercase, you have to pass me in the name and then the value force uppercase. That was only a half-truth. If you’re using what we
call positional notation, meaning the position
of the parameters. Let’s the function know which
parameters go with which values, that’s what you have to do. But there’s something else
called named notation and what that does is when I
call the function I can say, hey for that parameter
force uppercase, I would like you to
set the value to true. For the parameter called name, I would like you to use
the value of first name. So in this case, It doesn’t matter whether I specify the name because
I’m telling the function. Here’s the value for
forced uppercase. Here’s the value for name. By naming the parameters, it doesn’t matter what order
I specify them in, and I still have an option if
there are default parameters. Default values for the parameters, they’re still optional to specify. What I love about doing it this way. I’ll execute the same is how much easier it makes
your code to read. I have done a lot of systems where we had to log errors when
things went wrong in our code, and you would sometimes
see code like this. One of the coders on
our team creates a function that we pass it the error
message and the error code. What time the error occurred and all these parameters
and it would then say, if that’s the error that happened, I’m going to log in in this
database over here and put it in this file with
all these particular actions, and you’ll see calls
like this in the code. Somebody calls the
error logger and it says, call error logger passing
the value 45, one true. Second number greater
than first my map method. Okay. I’m not sure what
those values are telling me, but I guess that’s right values. So if I’m told later I need
to go fix this person’s code, I get really confused when I
read a statement like this. What if I rewrite that a little bit, and I use the named notation here and suddenly it says
call the error logger, pass in an error code of 45
and error severity of one. Log to database set that to true. The error messages, second
number greater than first and the source module
was my math method. Suddenly I can almost
guess what the values there I should pass him whenever I call error logger because
of his name notation. So it’s a really useful trick and makes your code much
easier to read for yourself when you come
back to it later or for someone else who has to
try and understand your code. So the named notation makes
your code a lot more readable. Let’s go try this out in some code.

Leave a Reply

Your email address will not be published. Required fields are marked *