After much head scratching and experimenting, I’ve solved a particular problem - but I can’t see why my first attempts did not work. So there’s clearly something I’m not understanding here. Simplified code below:
I have an association with 2 simple equations in x. I can assign one of these ‘a’ to a function and then pass a value to it - func[10]. The output is as expected - 11.
However, I wanted this to be used inside another function, hence myFuncA. This contains essentially the same code,however the output is now 1+x
To get the correct result,I had to use an intermediate variable as in myFuncB which gives the correct - 11 - output.
But being a mindful programmer, I like to keep the scope of temporary variables as limited as possible. So I wrote myFuncC. The output from this 1+x
myFuncB works but myFuncA does not.
And myFuncC breaks it again.
What am I missing here? There must be something about variable scope I am not getting. Can anyone enlighten me perhaps.
What I would recommend, is changing your association to pass pure functions. Relying on the same dummy argument names across modules is not a great idea. So change it to something like…
Many thanks for those replies, given me much more reading to do. Just want to add a few notes,in case it’s of interest to anyone else.
The Trace command is very useful (thanks Henrik), but can be a bit confusing for complicated functions. Where it’s suspected a definition is in question,then the Information function - or ? - can be useful.
?tmpFuncA
Global`tmpFuncA
tmpFuncA[x$_]=1+x
The x$_ indicating the issue. Quite why, I’m still trying to figure.
I now agree with Eric that passing ‘pure’ functions is better. However, I did solve the myFuncC by using TranditionalForm as below