Read sections 3.7, 3.8, and 3.9 in the book and do the examples. I know that the last section is an extra page, but it helps explain how to visualize the issue in 3.8.
FYI, cat is short for concatenation in the reading.
Notice in the diagram that the stack frames are named by the function they're inside, except for the first one which is outside of any function and instead named __main__. Let's write a few functions and define the variables x and y inside them both (and in __main__). Create a new script with the following code and run it:
def abra(x): y = 5 #check the local variables x and y print("inside abra:") print(" x:", x) print(" y:", y) print() def clefairy(x): y = 10 #check the local variables x and y print("inside clefairy:") print(" x:", x) print(" y:", y) print() abra(y) #check the local variables x and y again print("inside clefairy (again):") print(" x:", x) print(" y:", y) print() #now do things outside of any functions and call clefairy x = 1 y = 3 #check the local variables x and y print("inside __main__:") print(" x:", x) print(" y:", y) print() clefairy(x) #check the local variables x and y again print("inside __main__ (again):") print(" x:", x) print(" y:", y) print()
Try it out! What gets printed out? Here's the first half:
inside __main__: x: 1 y: 3 inside clefairy: x: 1 y: 10 inside abra: x: 10 y: 5 ...
If you get something different from this, check with me or a tutor! What should the last two parts print out?
With parameters, the difference between a function definition and a function call is even more important! In a function (definition) header, parameters are always new variable names, never values (e.g. strings "monkey" and integers 5). In a function call, the arguments can be any values, or variables that already have a value, or even more complicated expressions.
The following are good examples of function headers and calls. There won't be any syntax errors here.
def cool_function(x, y):
>>> cool_function("monkey", 7)
>>> cool_function("don" + "key", 10 - 6)
>>> cool_function(long_string, Math.floor(3.5))
On the other hand, these are bad function headers that will cause syntax errors:
def cool_function("monkey", 7):
def cool_function("don" + "key", Math.floor(10.2))
The point of a parameter is to have a variable that can be used with lots of different values for that variable. Anything other than that variable name doesn't make sense for that purpose.