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
Diagramming What Happens with a Function Call Variable scope and function calls
⇐ 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.
- What I want to do in this video is really think about
- what happens when we define a function
- and actually what happens as we
- step through the program and we use the functions.
- So let's say we write this program
- just like we did in the last video and we run it.
- So the interpreter will start at the first line over here,
- it says: " Oh look! There is a function definition,
- we are defining the function - factorial"
- What I'm going to do here is,
- I'm going to make a big box
- and this big box is an environment
- an environment for a program.
- Is all of the different definitions and variables
- definitions and everything that are specific to
- the contex the program is running in.
- So immidietly, the program will associate the name
- "factorial" with this whole function right over here.
- So right when it says define factorial,
- it will actually associate, it wil actually,
- let me write it over here.
- It will actually associate factorial,
- the label factorial i guess you could say it with a function
- that looks like all of this business right over here.
- So you will pass it a number,
- you'll pass it a number
- it's a function and I'm not using the,
- and I'm not using or maybe I'll write it like this,
- you'll see different notation,
- lambda notation and all the this fancy stuff
- if you'd take a formal computer science class
- but all I'm just saying is factorial refers to function
- that has the parameters,
- and it only has a one parameter,
- has the parameter "number".
- And then given with the parameter "number"
- it will then process this code right here,
- so let me just copy and paste it right here and then,
- whoops I'm doing it in the wrong layer
- let me copy and paste it again.
- So let me try to copy it,
- let me copy it and then let me paste it.
- So that what it is doing right over there
- and actually I should make this box,
- the environment includes all of this,
- I think you will bare with me,
- I didn't draw this orange box big enough
- first time around.
- So let me do that so that no one gets confused,
- just so that no one gets confused,
- so let me expand the orange box.
- And i've erased a little bit,
- one of the perimeters is the number
- and then the orange box goes around the whole thing.
- Thats a green color not a green.
- Alright here we go!
- So right when we hit the first few lines
- it's saying "Look in our entire global environment
- we are defining a function,
- the factorial will now refer to a function
- that takes the perimeter "Number"
- and then it will run all of this code
- that we've defined here and then
- it will return whatever inside this function rolled,
- whatever the value of the variable "product" is.
- And the it keeps going line-by-line-by-line
- and then we get to this line right over here.
- And it says "The variable "user_input" is equal to,
- and then it calls the function input
- and it passes the function input,
- and I'm starting to use a little bit of terminology
- that we've been exposed to.
- It passes the function input the string,
- so this right here is a string.
- And once again a string sounds like a fancy word
- but it literally means "string of characters"
- or this text right here
- the text
- "Enter a non-negative integer to take the factorial of: "
- So even the spaces are part of the strings.
- And the tell-tell sign of strings is
- it is going to be inside some type of quotation marks,
- either double or single quotation marks.
- So by passing this to that input function,
- that tells input function what to prompt the users.
- So it prompts the user with this string,
- the user types in a number or some type of expression,
- that gets evaluated and then gets stored in "user_input"
- or I really should say
- in the Python context "user_input" refers that value.
- So lets say that the user inputs "3"
- then the variable user input
- then the variable user input
- will now refer in this global environment.
- Now factorial refers to a function,
- "user_input" will refer to
- what would ever the user typed in.
- I'm gonna go with the specific example of "3".
- Then we go to the next line.
- And it is saying
- "Look, make factorial of user input,
- make this entire variable refer to
- whatever we get when we call factorial of "user_input",
- factorial of this variable over here".
- And so this is the interesting part.
- At this point in the program,
- the factorial of user input this part right over here,
- it will make a call to this function
- and it gives it the argument of what ever "user_input" is
- Now "user_input" is referring to "3",
- so it is going to pass "3" to the function "factorial"
- So "3" gets passed here
- and essentially within this function world
- so now lets say that
- this is the context of running this function.
- So within this function world,
- let me make it clear,
- so now within this function world right over here
- the functions environment,
- the variable "number"
- within this function world now refers to "3".
- I now want to be a little bit careful
- because we are starting to touch on
- the idea of scope of a variable.
- The variable "number"
- in this function world,
- it is only reference able within the function,
- and we do refer it within the function.
- It is not referable outside of the function,
- so if down here someplace I would say "print(number)"
- you would get an error
- because it would be out of the scope of that variable.
- This variable, it is a perimeter to this function,
- it is only usable within this function.
- Its scope, the place
- where you can refer to it is only in this function.
- So you get that number and then it does the code
- we've looked at before
- and it sets the variable "product",
- so the variable "product" now refers to the number "1"
- then it calls all of this code
- and since we talked about scope
- i'll talk about another situation of scope.
- This variable "i" is only valid
- within the scope of the for-loop.
- If down here i says "return i",
- that would have given an error.
- You can only refer to it whiten the for-loop.
- We have gone trough this code multiple times.
- It goes from i=0 to i=1 to i=2 all the way up to "i"
- is equal to one less than the "number"
- but since we are adding one to "i" every time,
- this all expression right over here.
- Starts at 1 then 2 then 3 all the way up to the "number"
- and each time we are multiplying it times
- the original "product" to get a new product.
- So at the end of the day,
- "product" after we go through this whole for-loop,
- "product" will contain the factorial of the "number".
- So after you do this "product"
- will keep referring to a bunch of things.
- If will refer to 1, then it will refer to 2,
- then it will refer to 6 because we're gonna do 3x2,
- so eventually "product" is going to refer to 6
- and then that is returned and when I say that is returned.
- That means evaluate this entire thing over here,
- that entire thing is going to be 6.
- Once we get out of that program,
- it lets the interpreter know that
- factorial of "user_input" should now refer to that 6.
- I know this might be a little bit
- confusing with all this diagramming
- but I really want you to
- get the sense of what's happening.
- "user_input" is referring to something,
- we pass that something to the factorial program
- so then we go up here
- and within the factorial program now
- since we passed "user_input" and "user_input" was 3.
- Within the factorial program
- this "number" is going to refer to 3
- and then we run this as if "number" was 3
- and then we return "product",
- which is going to be the factorial of "number"
- and so this whole thing evaluates to
- the factorial of whatever was in here.
- So the factorial of 3.
- And so factorial of "user_input",
- this variable, will now refer to that
- because being assigned to that
- and then we print it and when we print it,
- that's what actually shows up down in our interpreter.
- So hopefully this doesn't confuse you too much.
- In the next video
- we will discuss how we might be able to do
- interesting things to this function itself.
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.