How a side effect breaks substitution

Consider the following Python program. Python evaluates the left operand of + before the right one.


Python
total = 20

def add_impure(x):
    global total
    total = total + x
    return total

def add_pure(t, x):
    return t + x

a = add_impure(4) + add_impure(4)
b = add_pure(20, 4) + add_pure(20, 4)


Find the value of a, the value of b, and the final value of total after both assignment lines have run. Give each as an integer.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Functional Languages and Programs Questions: