I'm grading for a class using Mathematica right now, and one of the students ran into a weird issue. When he ran code like this:
examplefun[n_] :=
SetDirectory["/Users/"];
Module[{output = ""},
Do[output = output <> RandomInteger[{1, 4}], {i, n}];
Print[output]]
He get's an error: Do::iterb: "Iterator {i,n} does not have appropriate bounds. "
But if he moves Module up:
examplefun[n_] :=
Module[{output = ""},
SetDirectory["/Users/"];
Do[output = output <> RandomInteger[{1, 4}], {i, n}];
Print[output]]
The error disappears. I'm not sure what the problem is, though I suspect it has something to do with scoping. Could someone provide me with some insight here?
Thanks!
Edit: Ohhhhhhhh, that makes sense! Thanks for the prompt responses, everyone! I hadn't considered that his code didn't use the proper syntax for a multi-line function since I haven't used multi-line syntax before. I've accepted an answer, but thanks for all the responses!
Module
is not part of the function but is executed on its own. So n is not defined. $\endgroup$ – C. E. Sep 5 '13 at 20:46