6

I noticed that if I open a .m file in Mathematica and click on the Run Package button, all the Set definitions will be printed as output if they are not ended in ;. The same for function::usage definitions.

This is probably harmless, but I am wondering if there is a guidance on weather semicolons should be appended at the end of Set or SetDelayed definitions.

Looking into the Mathematica libraries source code, I can see both approaches - some Module functions have ; at the end, others do not.

Meh
  • 1,637
  • 2
  • 13
  • 17
  • 1
    As you noticed, it doesn't really make a difference for as long as the commands written in the package don't print any output as a side effect. Packages typically contain definitions, which don't print anything when evaluated, so most of the time there's no need for semicolons. Set is a possible exception, as you noticed. – Szabolcs Oct 11 '13 at 19:17
  • 1
    I usually do this, but I know a lot of folks who don't. This probably doesn't matter much, as long as one is consistent with it. – Leonid Shifrin Oct 11 '13 at 19:32
  • 2
    @Szabolcs Set doesn't print if the package is read in via << or Needs – rm -rf Oct 12 '13 at 04:15

1 Answers1

2

(Based on Szabolcs's & rm -rf's comment)

As you noticed, it doesn't really make a difference for as long as the commands written in the package don't print any output as a side effect. Packages typically contain definitions, which don't print anything when evaluated directly, so most of the time there's no need for semicolons. Set is a possible exception, as you noticed.

Even cells in the package which contain code that otherwise prints output if Shift+Enter-ing the cell (e.g. lines with Set, or simple statements without assignments like {1, 2, 3}) won't print anything if the package is read in via << or Needs.

István Zachar
  • 47,032
  • 20
  • 143
  • 291