Skip to main content
Contents
Prev Up Next Scratch ActiveCode Profile
\(\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.2 Evaluate Functions-WE1-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.
Q1: What is the output of the following code?
def foo(a, b):
if a > b:
return "a is larger!"
elif b > a:
return "b is larger!"
else:
return "a and b are equal!"
a = 10
b = 15
result = foo(b, a)
print(result)
2.
Q2: What is the output of the following code?
num = 10
a = double(5)
def double(num):
return num * 2
print(a)
3.
Q3: What is the output of the following code?
a = 10
def my_func(a, b, c):
c = c + 1
d = a * b + c
return d
c = 5
result = my_func(5, 2, 7)
print(result)
4.
Q4: What is the output of the following code?
def multiply(a, b):
return a * b
b = 3
def divide(a):
return a // b
first = 10
second = 5
multiply_result = multiply(first, second)
divide_result = divide(multiply_result)
print(divide_result)
5.
Q5: What is the output of the following code?
def bar(alpha):
return alpha * gamma
alpha = 5
beta = 2
gamma = 10
result = bar(beta)
gamma = 1
print(result)
You have attempted
of
activities on this page.