1

I am writing a simple function which performs a sum, but run into an unexpected answer.

f[n_]:=Sum[n-m, {m, 1, 10}]
f[1] (* gives -45 *)
f[m] (* gives 0; didn't expect this *)

It appears that when fed an unassigned m, m coincides with the iteration variable m, which if of course is undesired. I can change the iteration variable to something like $m. But is there better practice to force the iteration variable to be completely local? Perhaps I should use Module or Block?

(Module seems to do the job)

user64494
  • 26,149
  • 4
  • 27
  • 56
Lelouch
  • 533
  • 3
  • 9

0 Answers0