What happens when we call a function?
def my_function(a, b):
c = a * b
if c % 2 == 0:
return c - a
d = 1 / 0
return 2 / 0
result = my_function(10 / 5, 3)Here is a summary of the steps taken when a function call evaluates:
Evaluate the function name and arguments.
Create a new frame to assign argument values to parameters.
Evaluate the function body.
At a return statement, evaluate the expression. If no return is encountered, return
None.
Practice: Tacos¶
What would Python display?
def food():
print("Tacos :)")
print(food())Practice: Mystery¶
What would Python display?
def mystery(a, b):
return a * b
mystery(mystery(2, 6), 3)Variable scope¶
The scope of a variable is the area of code in which the variable is defined. In Python, there are two scopes:
Local scope: Variables (including parameters) defined inside a function body. The value of the variable is accessible only within the function.
Global scope: Everything else. The value of the variable is accessible anywhere in the program (including inside functions).
Practice: Variable-ception¶
What would Python display?
x = False
def my_function(x):
print(x)
my_function(True)
xPractice: Slippery scope¶
What would Python display?
x = 1
y = 2
def func1(x):
y = 1
return x + y
def func2(x):
x = 5
x = func1(x)
return x + y
x = func2(x)
x