4 replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/ edited Apr 13 '17 at 12:55 Per comments, including here the description of the problem lifted from another site: Mathematica Pure Function Scope Problem Consider this snippet: f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999]  Executing this sequence generates errors messages: Function::flpar: Parameter specification {0} in Function[{0},999] should be a symbol or a list of symbols. (x3) $RecursionLimit::reclim: Recursion depth of 256 exceeded. (x4) Hold[Function[{0},999][0]]  However, if you change the name of the function variable in the first line from a to z, then all works as expected: f[x_] := g[Function[z, x]]; f[999] => 999  Another workaround is to compile the function: f[x_] := g[Compile[a, x]]; f[999] => 999  The problem appears to be caused by a name conflict between the pure function's argument a and the helper function h's argument a. This is a really nasty problem because the definitions are completely separate -- one would have to perform a global code analysis to turn up such problems. I'm a relatively new user of Mathematica and have already ran into that problem, asked and answered herehere. I'm used to C++; in C++ and most other programming languages) local scope of function arguments and local scope of most variables prevents variable name clash between different functions. Thus the question: how does one prevent the above problem in Mathematica? Per comments, including here the description of the problem lifted from another site: Mathematica Pure Function Scope Problem Consider this snippet: f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999]  Executing this sequence generates errors messages: Function::flpar: Parameter specification {0} in Function[{0},999] should be a symbol or a list of symbols. (x3)$RecursionLimit::reclim: Recursion depth of 256 exceeded. (x4) Hold[Function[{0},999][0]]  However, if you change the name of the function variable in the first line from a to z, then all works as expected: f[x_] := g[Function[z, x]]; f[999] => 999  Another workaround is to compile the function: f[x_] := g[Compile[a, x]]; f[999] => 999  The problem appears to be caused by a name conflict between the pure function's argument a and the helper function h's argument a. This is a really nasty problem because the definitions are completely separate -- one would have to perform a global code analysis to turn up such problems. I'm a relatively new user of Mathematica and have already ran into that problem, asked and answered here. I'm used to C++; in C++ and most other programming languages) local scope of function arguments and local scope of most variables prevents variable name clash between different functions. Thus the question: how does one prevent the above problem in Mathematica? Per comments, including here the description of the problem lifted from another site: Mathematica Pure Function Scope Problem Consider this snippet: f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999]  Executing this sequence generates errors messages: Function::flpar: Parameter specification {0} in Function[{0},999] should be a symbol or a list of symbols. (x3) $RecursionLimit::reclim: Recursion depth of 256 exceeded. (x4) Hold[Function[{0},999][0]]  However, if you change the name of the function variable in the first line from a to z, then all works as expected: f[x_] := g[Function[z, x]]; f[999] => 999  Another workaround is to compile the function: f[x_] := g[Compile[a, x]]; f[999] => 999  The problem appears to be caused by a name conflict between the pure function's argument a and the helper function h's argument a. This is a really nasty problem because the definitions are completely separate -- one would have to perform a global code analysis to turn up such problems. I'm a relatively new user of Mathematica and have already ran into that problem, asked and answered here. I'm used to C++; in C++ and most other programming languages) local scope of function arguments and local scope of most variables prevents variable name clash between different functions. Thus the question: how does one prevent the above problem in Mathematica? Tweeted twitter.com/StackMma/status/780678580137328640 occurred Sep 27 '16 at 8:00 3 edited tags | link edited Sep 27 '16 at 3:39 Karsten 7. 25.5k55 gold badges5555 silver badges113113 bronze badges 2 added 1338 characters in body edited Sep 27 '16 at 0:28 Michael 37733 silver badges99 bronze badges Could you recommend a method to preventPer comments, including here the description of the problem lifted from Mathematica Pure Function Scope Problemanother site?: Mathematica Pure Function Scope Problem Consider this snippet: f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999]  Executing this sequence generates errors messages: Function::flpar: Parameter specification {0} in Function[{0},999] should be a symbol or a list of symbols. (x3)$RecursionLimit::reclim: Recursion depth of 256 exceeded. (x4) Hold[Function[{0},999][0]]  However, if you change the name of the function variable in the first line from a to z, then all works as expected: f[x_] := g[Function[z, x]]; f[999] => 999  Another workaround is to compile the function: f[x_] := g[Compile[a, x]]; f[999] => 999  The problem appears to be caused by a name conflict between the pure function's argument a and the helper function h's argument a. This is a really nasty problem because the definitions are completely separate -- one would have to perform a global code analysis to turn up such problems. I'm a relatively new user of Mathematica and have already ran into that problem:, asked and answered here. I'm used to C++; in C++ and most other programming wherelanguages) local scope of function arguments and local scope of most variables prevents variable name clash between different functions. Thus the question: how does one prevent the above problem in Mathematica? Could you recommend a method to prevent Mathematica Pure Function Scope Problem? I'm a relatively new user of Mathematica and have already ran into that problem: I'm used to C++ programming where local scope of function arguments and local variables prevents variable name clash between different functions. Per comments, including here the description of the problem lifted from another site: Mathematica Pure Function Scope Problem Consider this snippet: f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999]  Executing this sequence generates errors messages: Function::flpar: Parameter specification {0} in Function[{0},999] should be a symbol or a list of symbols. (x3) \$RecursionLimit::reclim: Recursion depth of 256 exceeded. (x4) Hold[Function[{0},999][0]]  However, if you change the name of the function variable in the first line from a to z, then all works as expected: f[x_] := g[Function[z, x]]; f[999] => 999  Another workaround is to compile the function: f[x_] := g[Compile[a, x]]; f[999] => 999  The problem appears to be caused by a name conflict between the pure function's argument a and the helper function h's argument a. This is a really nasty problem because the definitions are completely separate -- one would have to perform a global code analysis to turn up such problems. I'm a relatively new user of Mathematica and have already ran into that problem, asked and answered here. I'm used to C++; in C++ and most other programming languages) local scope of function arguments and local scope of most variables prevents variable name clash between different functions. Thus the question: how does one prevent the above problem in Mathematica? 1 asked Sep 26 '16 at 22:37 Michael 37733 silver badges99 bronze badges