Editor

Functions: What Does This Print

What does this print? · Intermediate · about 8 min

Read each short program and type exactly what it prints, with each printed line on its own line. If a program stops with an error, type the error's name, like TypeError.

Example

def square(n):
    return n * n
print(square(3))

prints

9
  1. Program 1

    def add(a, b):
        print(a + b)
    result = add(2, 3)
    print(result)
  2. Program 2

    def greet(name, greeting="Hello"):
        return greeting + ", " + name
    print(greet("Sam"))
    print(greet("Sam", "Hi"))
  3. Program 3

    x = 5
    def change():
        x = 10
    change()
    print(x)
  4. Program 4

    count = 0
    def bump():
        count += 1
    bump()
    print(count)
  5. Program 5

    def total(*nums):
        return sum(nums)
    print(total(1, 2, 3), total())
  6. Program 6

    def power(base, exp=2):
        return base ** exp
    print(power(exp=3, base=2), power(3))
  7. Program 7

    def f(n):
        if n == 0:
            return 0
        return n + f(n - 1)
    print(f(4))
  8. Program 8

    def check(n):
        if n > 0:
            return "positive"
    print(check(5), check(-1))