Skip to main content

Section 8.4 Evaluate Functions-WE2-P1

Subgoals for evaluating a function call:.

  1. Create stack frame for global variables
  2. Ensure function is defined before function call before beginning body of function
  3. Determine parameter values based on positional arguments, keyword arguments, and default values to initialize local variables
  4. Use stack frame for function to trace local variables
  5. At return to call site, pass values back to call site
  6. 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
  • 4
  • 4.3
  • Compiler error

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)
    
  • 4
  • -2
  • -4
  • Compiler error

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)
    
  • 6
  • 1
  • 4
  • Compiler error

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)
    
  • 1.5
  • 1
  • 2.5
  • Compiler error

5.

    Q10: What is the output of the following code?
    a = 1
    def hello():
        return "Hello " * a
    
    a = 3
    result = hello()
    print(result)
    
  • "Hello Hello Hello "
  • "Hello "
  • "Hello 3"
  • Compiler error
You have attempted of activities on this page.