Say I'm defining a function with some local variables, for which I'm using Module. Now, I want to assign some value (or perhaps a list of values or a list of list, etc.) to some of those variables. Can I use a previously user defined function (which itself might have been built using Module) to do so? Here is an example (probably not the best) of what I am asking for, hope it's clear enough:

f[y_]:= y^2

 g = f[x];    



I mean it does work, in this case, but is it a good practice? What could possibly have gone wrong?

  • 4
    $\begingroup$ I see no problem with what you're doing here, regardless of whether or not f uses Module. The idea behind scoping constructs is that you can nest them, so this is perfectly allowable. $\endgroup$ – Sjoerd Smit Feb 27 '19 at 14:20
  • 1
    $\begingroup$ You just need to watch out when you nest them explicitly: enforcing-correct-variable-bindings $\endgroup$ – Kuba Feb 27 '19 at 14:24
  • $\begingroup$ Thank you @SjoerdSmit ! I thought that the idea was correct. The reason I asked is that I'm trying to find out where the error comes from in my code and I thought I might have been due to the way I constructed my functions. Thanks again! $\endgroup$ – amator2357 Feb 27 '19 at 14:25
  • $\begingroup$ Thanks @Kuba. I'll look into it. $\endgroup$ – amator2357 Feb 27 '19 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.