- [Instructor] Most applications,
regardless of the programming language they are written in
are broken up into smaller blocks known as functions
and Python is no different.
So let's start by defining a function.
So functions are defined with the Def keyword
and I'll call my function, function one
and then open close parenths.
Now this function doesn't take any arguments
but we'll get to that in a little bit.
And then a colon which indicates the start
of the function scope block.
And then when I hit return
you'll notice that the next line is indented.
So I'll write print
and then inside there I'll give it a string
I am a function.
All right, so a couple of things to notice here,
in some cases in other programming languages,
this colon will be replaced in say Java or C
with curly braces to indicate scope.
That's not how Python works.
Python uses a colon, and then the next line is indented.
And you can see that this print statement
is indented by one, two, three, four spaces,
but it's up to you how many spaces
you want to indent something
it's not limited to anything,
it could be two or three or four or whatever.
Let's add a few more lines of code
down at the bottom to exercise this.
All right, func one
and then I'll write print func one,
and then I'll write print and then func one
but no parentheses.
All right, so let's save
and let's go to the debugger and run this
and I'll show the output window there.
So I'll click the little green run button.
So in the result, you can see that
I am a function got printed
and then the same string followed by the word none
and then finally this string here.
So in the first case, the function is being called directly
which executes the contents of the function
causing the print statement to print the string
so that's simple enough.
In the second case,
the function is also being called inside the print function.
So the output is the same as in the first case
but then the outer print statement executes.
And since our function doesn't return a value
Python evaluates the return value
to be the Python constant of none
and then just prints the string representation of that.
In the last case,
the function itself is not being executed at all
since we're not including those little parentheses
that would call the function, we're just printing the value
of the function definition itself
which evaluates to this string that you see here.
This just demonstrates that functions themselves
are objects that can be passed around
to other pieces of Python code.
So let's try a couple of additional examples.
I'll define a function that takes some arguments.
So I'll call that func two
and it'll take arg one and arg two as arguments.
And I'll just print arg one followed by a space
followed by arg two.
And then I'll define a function named cube
which takes one argument.
And this one will return a value
and it will return X times X times X.
And now let's add a few more lines
to exercise these new functions.
So I'll do that down here.
So I've got func two
and I'll just call it with values of 10 and 20.
And then same as before
I'll just do print and then func two
with 10 and 20.
And then I'll print the result of cube
and I'll call it with a value of three.
And let me come at these guys out.
All right, so let's go ahead and run this.
You can see that on the first line,
10 and 20 get printed with a space in between
and that's similar to the first result.
And then the second line
we've got 10 and 20 being printed again
with the value of none.
And again, that's happening because there's no return value
from func two.
And then for the cube function,
when we print the cube of three, we get the value of 27.
So in this case, the function does return a value
and that's what gets printed.
So let's add another example
and I'll define a function that takes arguments
and has a default value for one of them.
So I'll call this one power
and it takes a number and then an X,
and I'm gonna write X equals one.
And then inside the function for power
I'll say result is equal to one
for I in range X
and another scope result equals result
times number
and then we'll return the result.
So it basically takes a number
and raises it to the given power, okay?
And in the function definition
I've got X is equal to one,
so this assigns a default value for that argument.
Let's go ahead and add some lines to exercise this.
All right, print power of two
and then I'll write print power of two, three.
And let me come at these out.
So here I'm calling the function power
but I'm not giving it a value for X,
so X is going to default to one.
The next invocation calls the function
with numb is equal to two, and the power is equal to three
so let's run this.
And you can see that two raise the first power is two
and then two to the third power is eight.
All right, now let's add one more invocation test.
So I'll scroll down here and I'll write print power.
And in this case I'll write X equals three
and then numb equals two.
I'm reversing the order in which the arguments are called.
So Python lets you call functions
with their named parameters along with their value.
And when you do this,
the Python interpreter figures out
which arguments to supply the values too.
You don't have to call the function
with the arguments in a particular order
if you simply supply the names along with the values.
So let's run this again
and sure enough you can see that the results are the same,
two to the third power is in fact eight in both cases.
We've got one more example to look at.
I'm going to define a function
this time I'll call it multi ad,
and the arguments are going to be a variable argument list.
So I'm gonna write star args
and then inside the function, I'll define a variable
that defaults to zero and then for X in args,
I'll write result equals result plus X
and then I'll return the result.
So what I've done here is define a function called multi ad
and the star character means
I can pass in a variable number of arguments.
The function then loops over each argument
and adds them all to a running total,
which is then return
so let's add a line to try this out.
And for multi ad
I'll just simply give it some arguments
like four, five, 10, four.
So now I'll save and I'll run this
and you can see that the result is 23.
So if I change the function call
to include another parameter,
in this case I'll give it a 10 upfront
and I'll run it again
and now the answer is 33.
You can combine a variable argument list
with a set of formal arguments,
but just keep in mind that the variable argument list
always has to be the last parameter.
(upbeat music)