Unfortunately, the i
in For
is not scoped, it is global. The body of For
calls test1[]
which sets the global i
to 12 which terminates the For
. If you want different scopes for i
.
test1[] := Module[{i}, i = 12]
test2[]
(* Prints 1 to 10 *)
i
(* 11 *)
Or
test1[] := Module[{}, i = 12]
test2[] := Module[{i}, For[i = 1, i <= 10, i++, test1[]; Print[i]]]
test2[]
(* Prints 1 to 10 *)
i
(* 12 *)
Or
i = -1;
test1[] := Module[{i}, i = 12]
test2[] := Module[{i}, For[i = 1, i <= 10, i++, test1[]; Print[i]]]
test2[]
(* Prints 1 to 10 *)
i
(* -1 *)
Suggest you read this.