- [Narrator] In this video we're going to continue
our discussion of custom function definitions,
this time with a function that has multiple parameters.
Now to save a little typing time here
I'm going to go ahead and paste in the function definition.
We're going to define a function here called maximum
which receives three parameters
because it wants to figure out the largest of three values.
So as you can see I started with the def keyword
followed by the name of my function, a parameter list,
this time with a comma separated list of names
which become the parameters and the colon then introduces
the body of the function which again is called a block.
Now the first thing inside of any function definition
should be a docstring so we've provided one here
which is saying that this function
will return the maximum of three values
and as we did back in the control statements chapter
we're going to make a little simplifying assumption here
which is to say that value1 is the maximum value so far.
So max_value is assigned value1.
Then we're going to use a couple of if statements
to figure out whether value2 is greater than max_value
and if so assign value2 back to max_value
and we're going to check if value3
is then greater than max_value and only if so
assign value3 to max_value.
Now you'll notice we're violating a little bit
of the Python style guide at the moment
by not putting a blank line above and below
each of these control statements
and there's a reason that we did that.
When you're inputting a function definition like this
directly in IPython,
if you press enter you get a blank line as you would expect,
but if you press enter again saying let's say
you wanted to start a new control statement,
it's going to assume when you press enter twice
that you're done with the definition
of whatever it is you were just creating
and it's going to then evaluate the snippet of code.
So if we were to have gone ahead up here and pressed enter
to drop to the line that currently starts with if
and then pressed enter again so that we could start
the if statement one line further down,
we actually would not have been able to complete
the whole function definition in that case.
So that's just something to keep in mind
when you are working in the context
of the interactive interpreter.
Now once we get past the two if statements
max_value contains the largest value so far
so we're going to use the return statement
at that point to give back the largest value
to whoever calls this function.
So let's go ahead and execute that
which defines the actual function.
When you execute a function definition
it creates the function in memory.
The function itself by the way is actually an object
and the docstring happens to be a piece of data
within the object that represents that function
and when you use the help system
it goes and looks at that object
and accesses the docstring piece of data
to get the string that you defined as the docstring.
So let's go ahead and call this function that we defined.
So we named it maximum so we'll start with that.
Remember when you call a function you need parentheses
and typically are going to need arguments
so usually when you're trying to figure out
the largest of three values,
it's typically going to be three items of the same type
so let's use three integers here
and you can see that the result
is the largest of those three integers.
Let's try it again with some floating point numbers
so let's do 12.3 and we'll do 45.6 and 9.7
so this time the largest value is in the middle position
rather than the third position,
but we just wanna make sure that indeed
it gives us back the largest value each time.
Now before I continue, one thing that's interesting
about the maximum function and about Python in general
is that again we have not specified
what the types of value1, value2 and value3 are.
Python figures that stuff out dynamically for you.
So as long as you can assign whatever type of value
you're dealing with and as long as you can compare
with the greater than sign
whatever types of values you're dealing with,
those values will work with the maximum function.
So it turns out for example that the maximum function has...
I'm sorry, not the maximum function.
The string type has the greater than operator defined
for comparing strings of information
so for example I can go ahead and take the maximum
of let's say yellow, red and orange.
Now I'm using all lowercase letters on purpose at the moment
because when you start dealing with case sensitivity,
strings are not considered to be the same
so you have to be careful with that
and we'll talk more about that subsequently,
but right now I have these three strings
and if you're thinking about strings alphabetically,
O comes before R and R comes before yellow
so yellow is the largest if you will of the string values
and orange is the smallest of the string values
based on alphabetic order in the alphabet.
So if I go ahead and run that
you can see that indeed it gives me back yellow in this case
and I'll talk more
about the case sensitivity issues in subsequent lessons.
That's beyond the scope of what I'm doing at the moment.
The main thing I wanted to point out here
is that you can compare strings using greater than,
greater than or equal, less than, less than or equal,
double equals and not equals as well.
So that just demonstrates to you
the fact that we can go ahead and compare
lots of different types of values,
provided that they support the greater than operator
that we're using in the context of the max,
maximum, excuse me, function in this case.
Now you can call maximum with values of different types,
provided that those types are compatible with one another
so if I do for example 13.5 minus three and seven,
we've got one floating point number and two integers
and floating point numbers and integers
are compatible with one another
so I can go and figure out that 13.5 is indeed
the largest of those three values.
On the other hand if I were to use
decimal objects and floating point numbers,
that would not work because decimal objects
and floating point numbers are actually not considered
to be compatible with one another.
So you can mix types in certain scenarios
which is also part of the flexibility
of the Python language.
Now once again the capability that I'm showing you here
is something that's actually built into Python
which is why it's so important that you spend time
looking at the online documentation
so that you can learn about the features
that already belong to the language
and to the Python libraries and even do some research
into other open source libraries as well
because chances are in the Python community
someone has developed a library
that does some of the capabilities that you need
or that implements some of the capabilities
that you need in your own applications.
So for example as I mentioned previously
the max function is built in so I can do things
like use the max function to compare some strings here
so let's try yellow, red, orange
and we'll add in a couple more here.
Let's do blue and green just to show you
that it can receive any number of arguments
so we did five in this case.
And yellow is still the largest of those three values
and similarly there's the min function
which takes any number of arguments
so let's do some integers.
Maybe we do nine and 27 and 14 so four integers in this case
and you can see that it happily figures out the minimum
of the values that we supplied.
So again you're going to want to take a look
at that online Python documentation at python.org
to familiarize yourself with all those built in functions
because there's a lot of convenience functions
already available to you
and you're going to want to take a look
at the top level of all those different libraries
that are part of Python so that you can start
getting a sense of the full set of features
that are available to you.