Skip to main content\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 9.4 Evaluate Functions-WE2-P1
Subgoals for evaluating a function call:.
-
Create stack frame for global variables
-
Ensure function is defined before function call before beginning body of function
-
Determine parameter values based on positional arguments, keyword arguments, and default values to initialize local variables
-
Use stack frame for function to trace local variables
-
At return to call site, pass values back to call site
-
Delete stack frame with local variables after return to call site
Exercises Exercises
1.
Q6: What is the output of the following code?
a = 10
c = 3
def foo(a, b):
b += a
return bar(b, c)
def bar(alpha, beta):
return alpha // beta
result = foo(2, 4)
print(result)
2.
Q7: What is the output of the following code?
def alpha(var1):
return var1 - 5
def beta(var1):
return var1 * var1
my_var = 3
result = alpha(beta(my_var))
print(result)
3.
Q8: What is the output of the following code?
a = 3
b = 4
def foo(a, b=10, c=3, d=1):
return (a - d) // ( b + c )
c = 1
result = foo(13, 1, c=2)
print(result)
4.
Q9: What is the output of the following code?
def foo(alpha, beta=3, gamma=5):
beta += gamma
return beta / alpha
beta = 5
result = foo(4, gamma=1)
print(result)
5.
Q10: What is the output of the following code?
a = 1
def hello():
return "Hello " * a
a = 3
result = hello()
print(result)
You have attempted
of
activities on this page.