Skip to main content

Section 8.2 Evaluate Functions-WE1-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.

    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)
    
  • "a is larger!"
  • "b is larger!"
  • "a and b are equal!"
  • Compiler error

2.

    Q2: What is the output of the following code?
    num = 10
    a = double(5)
    
    def double(num):
        return num * 2
    
    print(a)
    
  • 5
  • 10
  • 20
  • Compiler error

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)
    
  • 18
  • 16
  • 28
  • 26
  • Compiler error

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)
    
  • 10
  • 16.667
  • 16
  • Compiler error

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)
    
  • 10
  • 20
  • 5
  • Compiler error
You have attempted of activities on this page.