4
$\begingroup$

When I execute:

Print[x]//InputForm

I get a Null at the last line of the result.

This is an example shown in the documentation of Print command in the Mathematica's official web page.

What is the Null? Why does it appear?

$\endgroup$
10
  • 1
    $\begingroup$ Use Echo[] if you want the output to be the expression in addition to the printing. $\endgroup$ Commented Jun 20, 2016 at 6:25
  • 1
    $\begingroup$ @ciao while Null ref page says it is an indication of a result absence, the ref page of Print says only things like Print sends its output to the channel $Output. - which isn't clear for newbies. There are a lot of examples of strage code with Print statements instead of Table etc, and I believe it's because its purpose is not well documented. $\endgroup$
    – Kuba
    Commented Jun 20, 2016 at 7:24
  • 1
    $\begingroup$ @Kuba - Take it up with Stephen W.. Forewarning: he's a big guy, with trained and vicious Turing machines caged and ready to attack... along with the superpower to materialize 6kg books at will to fling at you... $\endgroup$
    – ciao
    Commented Jun 20, 2016 at 7:29
  • 2
    $\begingroup$ Related: (25507), (41086). Tangentially related: (5682), (18617) $\endgroup$
    – Mr.Wizard
    Commented Jun 20, 2016 at 10:24
  • 1
    $\begingroup$ @Ciao Those are cellular automata. They snack on Turing machines. $\endgroup$ Commented Jun 20, 2016 at 13:20

1 Answer 1

8
$\begingroup$

Mathematica is a term rewriting system. It works by taking an input expression and transforming it according to some rules to produce an output expression. The implication is that every evaluation will have a result. For every input there is an output.

Null is simply the expression used to represent "no result". In StandardForm and OutputForm it is not printed. Thus when the result is Null, it is usually simply not shown. Try evaluating Null alone and it will appear that there is no output. In InputForm however it is shown as "Null". You can also try InputForm[Null].


Clarification: When you evaluate Print[x] // InputForm, Null is not "the last line of the result". It is the only result. There is always precisely one result.

The x you see is not a result of the evaluation. It simply happens to be shown as a side effect of evaluating Print. But it is not what Print evaluates to and cannot be used as an input to other functions. It is simply displayed when Print is evaluated.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.