Computer science
-
Introduction to Programs Data Types and Variables
-
Binary Numbers
-
Python Lists
-
For Loops in Python
-
While Loops in Python
-
Fun with Strings
-
Writing a Simple Factorial Program. (Python 2)
-
Stepping Through the Factorial Program
-
Flowchart for the Factorial Program
-
Python 3 Not Backwards Compatible with Python 2
-
Defining a Factorial Function
-
Diagramming What Happens with a Function Call
-
Recursive Factorial Function
-
Comparing Iterative and Recursive Factorial Functions
-
Exercise - Write a Fibonacci Function
-
Iterative Fibonacci Function Example
-
Stepping Through Iterative Fibonacci Function
-
Recursive Fibonacci Example
-
Stepping Through Recursive Fibonacci Function
-
Exercise - Write a Sorting Function
-
Insertion Sort Algorithm
-
Insertion Sort in Python
-
Stepping Through Insertion Sort Function
-
Simpler Insertion Sort Function
Defining a Factorial Function Defining a function in Python
⇐ Use this menu to view and help create subtitles for this video in many different languages.
You'll probably want to hide YouTube's captions if using these subtitles.
- In the last few videos we wrote a nice little program here
- that asks the user for some type of input
- and then computes the factorial of that number
- and then prints it out.
- And thats nice, but you could imagine a world where
- you would want to calculate the factorial in 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 write this code
- over and over again every time
- you want to calculate the factorial.
- So what I'm gonna to in this video,
- is define a function that calculates the factorial
- and then we could use that function
- ever time we want to calculate the factorial.
- So essentially what this function is going to do,
- is going to put all this code,
- 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 keyword in Python,
- so it is kind of a special word
- that tells the interpret that something
- special is about to happen.
- The keyword in Python for defining a function is "def".
- 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 in indicative of what they actually do.
- Sometimes beginning programmers have a habit of
- naming things like "x245"
- and someone who comes in later
- will have no clue what variable
- or that function is all about.
- So you definitely somehow name it
- so it gives an idea what is supposed to do.
- My function is called factorial
- and the user when they call factorial,
- they need to tell factorial
- what number I'm going to take the factorial of.
- So they gonna pass it in argument called "number".
- And if these words look a little confusing
- i will explain them in detail in a second
- but you can just hopefully the more you see this
- the more familiar you will get with these ideas.
- And so they gonna pass it number
- and I'm just going to have to return back
- to the calling program
- and I actually shouldn't have said
- that the user is going to pass the number,
- the calling program is going to pass the 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 "number".
- And sometimes you will hear the word parameter
- and sometimes you'll hear the word argument.
- When you are 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 returns the factorial of the argument "number".
- And this is the argument "number" right over here.
- I don't mean this just is like arbitrarily the word number
- I'm literally talking about this number over here.
- Maybe I will say the argument "number".
- Maybe I will put it in quotes to make it clear
- that this is the argument,
- I'm not talking about any general number.
- Let's define it.
- So we are going to use the same code essentially,
- actually let me just copy and paste,
- let me just copy and past a lot of what i took over here.
- So I'm going to cut that out
- and I'm going to re-paste it over here
- but i have to be careful about my indentations
- cause indentations are how Python knows
- whats part of what, it knows how to interpret things.
- So everything that is 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.
- One, two, three, four.
- And this is part of the for-loop.
- One, two, three, four.
- And let's think about what we have so far.
- It is going to be passed some number,
- we define this variable "product" equals 1.
- And we will talk more about scoping of variables
- but this variable is only going to be useable
- within this factorial definition
- because the first time I define it right here
- is inside of this right here
- so we will go more into scoping of variables in the future.
- And then with the same logic we did before,
- "for i in range(number)",
- now we are not getting the number using the input function
- it is just being passed to the function,
- then "product" is equal to "product" x ("i"+1).
- Same logic as we had before.
- So after you go through this for-loop,
- after you go through it,
- you are essentially going to go through it "number" times.
- "Product" will have in it the factorial of "number".
- And instead of outputting it directly,
- what we wanna do is return,
- we are 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 is essentially the exact same code as we had before
- but we have packaged it inside of a function,
- we have defined a function.
- It takes in, it has a parameter called a "number",
- if you want a factorial of 3
- you try the factorial of 3
- and 3 would be the argument the thing
- that is 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(number)",
- we have explained the logic of that in the previous video.
- Everytime you start with 1,
- then you do 1 x 1 is going to be 1,
- then "product" is going to be 1
- but then "i" is going to be 1.
- "i" starts off at zero,
- so it is going to be 1 x (0 + 1) gives you 1,
- then "i" is 2, so it's going to be 1 x 1,
- sorry, then "i" is 1
- it is going to be 1 x (1 + 1), which is 1 x 2,
- so it is going to be 2 then
- and it will incrementing that way
- and we explained it in detail in the last video.
- And then finally it is going to return that "product".
- So if we want to have the same exact behavior as before
- but now we are using this function.
- What we could do is, we could say,
- we still have the input, so this is our function definition
- but then along our main program,
- we have defined our function
- and now we could just say:
- "Look, from the user get a non-negative integer
- to take the factorial of"
- And let's put that into,
- lets call that a variable called "user_input".
- And then what I'm going to do
- is I'm going to define another variable
- to 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,
- I guess we should say,
- it would be pointing to the actual answer
- and now we just have to print it out.
- So now we can print "factorial_of_user_input".
- They look like sentences
- but these are just variable names
- that I'm naming this way
- so you really now what's inside of those variables
- or what those variables are really pointing to.
- So "factorial_of_user_input".
- Now, you know the moment of truth
- is always saving the program and actually trying to run it.
- So lets 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 defined this
- so it is not creating any interaction with the user,
- it is actually not processing anything just yet,
- so it just defines this function then it goes down here
- and says: "Okay, get some input form the user"
- and thats what we are doing right here.
- After we input some number here,
- then it's gonna actually call this function with that number
- that we put, the number that we put,
- is gonna go put in the "user_input"
- and then it is gonna call factorial
- with "user_input" as an argument.
- So let's try with the number 3.
- And it did not work!
- Oh, and I see why it did not work
- because I had this leftover from the previous program
- which actually makes no sense now.
- So let me get rid of that.
- It is a good lesson that seldom on the first
- try does something work perfectly.
- Let me try it again!
- That was just nonsense
- that i didn't even realize what was down there.
- So let me try it again.
- So 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 multiple ways
- and now you should hopefully appreciate
- why it is cool that we made this function definition
- cause we can calculate,
- cause I've defined this function factorial,
- I can calculate the factorial of 4.
- It is 24.
- I can say that the factorial of 5 - the factorial of 3.
- 114.
- I can take the factorial of 12
- and you can tell the computer,
- even though it is just interpreting all this,
- we will talk more interpreted vs compile code,
- it's incredibly fast.
- Faster then we can really even fathom.
- So this is the real power of a function,
- is that I don't have to rewrite 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 something in combinatorics,
- I can just use this as a function,
- infact I don't even have to know
- what is going on inside the function.
- That's 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 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.
- The way that uses less memory or less CPU power.
- Then you could just replace this later on
- and then any program that calls it,
- as long as it still works,
- it will work for that program
- and it will just work that much better.
Be specific, and indicate a time in the video:
At 5:31, how is the moon large enough to block the sun? Isn't the sun way larger?
|
Have something that's not a question about this content? |
This discussion area is not meant for answering homework questions.
Discuss the site
For general discussions about Khan Academy, visit our Reddit discussion page.
Flag inappropriate posts
Here are posts to avoid making. If you do encounter them, flag them for attention from our Guardians.
abuse
- disrespectful or offensive
- an advertisement
not helpful
- low quality
- not about the video topic
- soliciting votes or seeking badges
- a homework question
- a duplicate answer
- repeatedly making the same post
wrong category
- a tip or feedback in Questions
- a question in Tips & Feedback
- an answer that should be its own question
about the site
Share a tip
Suggest a fix
Have something that's not a tip or feedback about this content?
This discussion area is not meant for answering homework questions.