# This file demonstrates the potential bad consequences it can have # to forget to declare all variables used in your functions as "local". i := 0; # silences the warnings... ouch f := function(n) local sum; sum := 0; for i in [1..n] do sum := sum + i; od; return sum; end; g := function(n) local prod; prod := 1; i := 2; while i <= n do prod := prod * f(n); i := i + 2; od; return prod; end; f(5); # 15 = 1 + 2 + 3 + 4 + 5 g(5); # we expect 15^2, but actually get 15 f(100); # 5050 g(100); # we expect 5050^50 but just get 50 # The problem is caused by the two functions not declaring i as local, # so they both are modifying the same variable i.