Consider that in the environment model, when you define a function, actually you obtain a closure, that is a couple (function, environment in which to evaluate the free variables of the function). A free variabile is a variable mentioned in the function which is not a parameter or a local variable, so that, when the function is actually executed, it is searched in the environment part of the closure.
So, when you define f1
, the closure returned has as environment the current global environment, in which a1
has value 1
.
In your definition of f2
, you define a1
as 2
. This is a local definition, so that in the body, the value of a1
is searched first in the local environment, and 2
is found.
In the definition of f3
, you define again a1
as 2
, and again this binding is present in the local environment, but you call f1
, which is a closure, and during its esecution the value of a1
is searched according to the definition of f1
(and the value found is that present in the environment used at the closure building time, that is 1
.)
This way of interpreting variables is called static binding, in contrast to dynamic binding, in which the result would have been 2
.
Note that both C and Scheme uses static binding, and your C example show exactly this: the result of calling f3
is 1
, since this is the value printed inside f1
. The image, on the other hand, is not correct. You should think of an environment as a set of frames, each of them containing bindings (i.e. couples variable, current value), connected to other frames. So that the closure of f1
, f2
, f3
are different.
The following picture shows the growth of the global environment:

E1
is the global environment after the definition of a1
. E2
after the definition of f1
, you can note that the value of f1
is a closure pointing to the second frame (this to allow recursive definitions, since f1
can in principle call itself). In the closure the value of a1
is the value present in E1
. E3
is the environment after the definition of f2
, with the closure pointing first to the local environment, where a1
is equal to 2
, then to the current global environment. Finally E4
is the global environment after the definition of f3
. Note that the new closure has again a local environment with a2
equal to 2
.
When f3
is called, the f1
inside its body is retrieved as the value of f1
in E2
, and when f1
is evaluated, then a1 = 1
is used.
With dynamic binding, on the other hand, there is no need to create closures. Each function is evaluated in the current environment, which is the global environment extended with the parameter bindings and the local definitions. So you could imagine that the global environment now has simply this form:

but when f3
is evaluated, the (define a1 2)
add a new frame to the environment:

and with this environment is evaluate the last form, (f1)
. During this evaluation, f1
is retrieved in the environment, and its body is again evaluated in the same (unique) environment, in which a2
has value 2
.