In the last few videos, we wrote a nice little program here
that asks the user for some type of an input,
and then computes the factorial of that number,
and then prints it out.
And that's nice.
But you could imagine a world where
you would want to calculate the factorial multiple places,
or in multiple different programs,
or in the same program, you want to calculate
the factorial multiple times.
And you don't want to have to write this code over
and over again every time you want to calculate a factorial.
So what I'm going to do in this video
is define a function that calculates the factorial.
And then we can use that function every time
we want to calculate that factorial.
So essentially, what that function is going to do
is going to put all of this code right here in one place.
And then any other program that I
want to calculate the factorial, I can just recall that code.
I don't have to rewrite it.
So let me just show you what I'm talking about.
So I'm going to define a function.
And the key word in Python-- so it's a special word that
tells the interpreter that something special is
about to happen-- the key word in Python
for defining a function is d-e-f.
So I'm going to define a function.
I'm going to call it the factorial function.
And it tends to be a good idea to name things
indicative of what they actually do.
Sometimes beginning programmers have
a habit of naming things like x425.
And someone who comes in later will
have no clue of what that variable or that function
is all about.
So you definitely want to somehow name it
so it gives an idea of what it's supposed to do.
My function's called factorial.
And the user, when they call factorial,
they need to tell factorial what number
am I going to take the factorial of.
So they're going to pass it an argument called number.
And if these words look a little confusing,
I'll explain them in detail in a second.
But you can just-- hopefully the more you see this,
the more familiar you'll get with these ideas.
And so they're going to pass its number.
And I'm just going to have to return back
to the calling program.
And I actually shouldn't even say
the user's going to pass a number.
The calling program is going to pass a number.
And I need to return back the factorial of that number
to the calling program.
So let me write this down as a comment, actually.
So returns the factorial of the argument
of the argument number.
And sometimes you'll hear the word parameter.
Sometimes you'll hear the word argument.
When you're making a definition of a function,
this is more formally called a parameter.
Number is one of the parameters to the factorial function.
The actual number that someone else passes to it--
so let's say someone calls factorial with a 3
here-- then more formally, that 3 would be the argument.
So it returns the factorial of the argument number.
And this is the argument number right over here.
So I literally mean-- I don't mean
this is just arbitrarily the word number.
I'm literally talking about this number right over here.
Maybe I'll say the argument number.
Maybe I'll put it in quotes to make it clear
that this is the argument.
I'm not talking about any general number.
And let's define it.
So we're going to use the same code essentially.
Actually, let me just copy and paste
a lot of what I took over here.
So I'm going to cut that out.
And I'm going to repaste it over here.
But I have to be careful about my indentations,
because indentations are how Python
knows what's part of what.
It knows how to interpret things.
So everything that's part of this function definition
has to be indented.
And we do it by four spaces-- one, two, three, four.
And this is another four, two, three, four.
And then this is part of the for loop, one, two, three, four.
And let's think about what we have so far.
So it's going to be passed some number.
We define this variable product equals 1.
And we'll talk more about scoping of variables.
But this variable is only going to be usable
within this factorial definition,
because the first time I defined it right here is inside
of this right here.
So we'll go more into scoping of variables in the future.
And then the same logic we did before, for i
in range of number-- now we're not getting number
using the input function.
It's being passed to the function.
Then product is equal to product times i plus 1, same logic
as we had before.
So after you go through this for loop--
after you go through it-- you're essentially
going to go through number times,
product will have in it the factorial of number.
Instead of outputting it directly, what we want to do
is return.
We're going to return it to the calling program.
And in the next video, I'm going to diagram this
out a little bit cleaner if this seems
a little bit confusing to you.
I'm going to return product.
So it's essentially the exact same code as we had before.
But we've packaged it inside of a function.
We've defined a function, takes it,
it has a parameter called a number.
If you pass factorial-- if you want a factorial of 3,
you'd write factorial of 3.
And 3 would be the argument, the thing that's
being put in the place of the variable number,
or the thing that number is referring to.
You define product is equal to 1.
And then, you go number times.
So for i in range of number-- and we've
explained the logic of that in the previous video-- every time
you start with 1.
And then you do 1 times 1 is going to be 1.
And then product's going to be 1.
But then i's going to be 1.
i starts off at 0.
So it's going to be 1 times 0 plus 1 gives you 1.
Then i is 1.
And so it's going to be 1 times 1 plus 1, which is 1 times 2.
And so it's going to be 2, then.
And it'll keep incrementing that way.
And we explain that in detail in the last video.
And then finally, it's going to return that product.
So if we want to have the same exact behavior
as before, but now we're using this function, what we could do
is-- we could say, we'll still have the input.
So this is our function definition.
But then, along our main program,
we've defined our function.
And now, we can just say, look, from the user,
get a non-negative integer to take the factorial of.
And let's put that into-- let's call
that a variable called user input.
And then what I'm going to do is I'm
going to define another variable called factorial of user input.
And now, this is going to be interesting.
I'm going to call this function up here.
So this is going to be equal to factorial
of whatever the user had input, so the factorial of user input,
the factorial of this variable right over here,
the factorial of user input.
So now, factorial of user input will be storing--
or I guess we should say, it will
be pointing to the actual answer.
And now, we just have to print it out.
So now we can print.
And now we can print factorial of user input.
So they look like sentences.
But these are just variable names.
And I'm naming this way so you really
know what's inside of those variables,
or what those variables are really pointing to.
So factorial of user input.
Now the moment of truth is always saving the program
and actually trying to run it.
So let's try to run it right now and see what happens.
Let's see what happens here.
So at least nothing broke so far.
So once again, so I want to be clear.
The program started here.
But up here, all we did is define this.
So it's not actually telling-- it's not
creating any interaction with the user.
It's actually not processing anything just yet.
So it just defines this function.
Then it goes down to here and it says,
OK, look, get some input from the user.
And that's what we're doing right here.
And after we input some number here,
then it's going to go-- it's going to actually call
this function with that number that we put.
The number that we put's going to go put in the user input.
And then it's going to call factorial with user input
as an argument.
So let's try it with the number 3.
And it did not work.
Oh, and I see why it did not work,
because I had this left over from the previous program,
which actually makes no sense now.
So let me get rid of that.
It's a good lesson that seldom on the first try
does something work perfectly.
Let me try again.
That was just nonsense that I didn't even
realize was down there.
So let me try it again.
So let me try.
So a 3 again.
And it gave me a good answer.
It gave me the factorial of 3.
And what's cool now is because now my Python interpreter is
assuming that this definition has been made,
I can now call the factorial straight from the interpreter.
If I had another program, I could call it in multiple ways.
And now, you should hopefully appreciate
why it's cool that we made this function
definition, because now we can calculate,
because I've defined this function factorial,
I can calculate the factorial of 4.
It's 24.
I could say the factorial of 5 minus the factorial of 3, 114.
I can take the factorial of 12.
And you can tell the computer, even though this is just
interpreting all of this-- we'll talk more about
interpreted versus compiled code-- it's incredibly fast,
faster than we can really even fathom.
So this is a real power of a function
is that I don't have to rewrite the code every time now.
I can just call it with different arguments.
So factorial of 2.
I can do it every time.
I don't have to rerun the program.
And if I write other programs that use factorial,
maybe I do some things in [INAUDIBLE],
I can just use this as a function.
In fact, I don't even have to know
what's going on inside the function.
And one of the other powerful aspects of functions
is that-- let's say I write a bunch of programs that
call-- so I have this program right here that
calls factorial.
But let's say that you come up with a better
way of writing this right over here.
As long as your program, that does the same thing
with different guts, the end user won't notice.
So maybe you write a faster way, or a simpler way,
of doing this, a way that uses less memory or less CPU power,
then you could just replace this later on.
And then the [INAUDIBLE] program that
calls it, as long as it still works,
it'll work for that program.
And it'll just work that much better.