[Date Index]
[Thread Index]
[Author Index]
Re: Scoping question
*To*: mathgroup at smc.vnet.net
*Subject*: [mg85744] Re: [mg85677] Scoping question
*From*: Sseziwa Mukasa <mukasa at jeol.com>
*Date*: Wed, 20 Feb 2008 07:06:41 -0500 (EST)
*References*: <200802190654.BAA27575@smc.vnet.net>
On Feb 19, 2008, at 1:54 AM, Yaroslav Bulatov wrote:
> The code below defines a function with parameter x. The body contains
> another function definition which uses this parameter. Why do "inner x
> is" and "outer x is" lines start reporting different values at one
> point?
>
> f1[x_] := (
> f2[y_] := (Print["inner x is ", x]; If[x + 1 < 3, f1[x + 1], 1]);
> (Print["outer x is ", x]; f2[#]) & /@ Range[2];
> )
> f1[1
Because f2 is a global symbol and when f1[2] is executed it redefines
f2, including the value x refers to. You shouldn't use () as a
scoping construct, strictly speaking it isn't a scoping construct, it
overrides evaluation order but not scope. Define f2 locally with
Module and then your function will run as I believe you want it to.
f1[x_] :=
Module[{f2},
f2[y_] := Block[{}, Print["inner x is ", x]; If[x + 1 < 3, f1[x +
1], 1]];
Block[{}, Print["outer x is ", x]; f2[#]] & /@ Range[2]; ]
Regards,
Ssezi
Prev by Date:
**Re: Localizing Large Numbers of Variables in a DynamicModule:**
Next by Date:
**Re: Plotting points of the function at increments.**
Previous by thread:
**Scoping question**
Next by thread:
**Re: Scoping question**
| |