Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to have multiple outputs from a single function?

For example:

In[1]:= x+1
Out[1]= 1+x
Out[2]= 2+x

has multiple outputs, how can I acheive that in a function?

I tried

In[3]:= f[x_]:=Module[{},
In[4]:= f[2]
Out[4]= 6

which doesn't work.

share|improve this question
The only way I can think of is to use NotebookWrite. –  Chip Hurst Nov 22 '13 at 3:27
add comment

3 Answers 3

I have found this solution:

In[1]:= f[x_] := CellPrint[{
                    Cell[BoxData[ToBoxes[x + 1]], "Output"],
                    Cell[BoxData[ToBoxes[x + 2]], "Output"]}]

In[2]:= f[2]


I was originally trying to achieve something like this:

f[data_] :=
    Cell["Data", "Subsection"],
    Cell[BoxData[ToBoxes[ListLinePlot[data]]], "Output"],
    Cell["Fourier transform", "Subsection"],
    Cell[BoxData[ToBoxes[ListLinePlot[Abs[Fourier[data]]^2]]], "Output"]

which will generate two subsections

f[RandomReal[1, 200]]

enter image description here

share|improve this answer
There is no reason to use Module in the definition of f; f[x_] := CellPrint[{Cell[BoxData[ToBoxes[x + 1]], "Output"], Cell[BoxData[ToBoxes[x + 2]], "Output"]}] works just as well –  m_goldberg Nov 22 '13 at 4:39
@m_goldberg yes you are right, let me correct that. Thanks for pointing it out :) –  xslittlegrass Nov 22 '13 at 16:15
add comment

May be you are looking for :

f[x_] := CellPrint /@ (ExpressionCell[#, "Output"] & /@ {x + 1 , x + 2}); ?

ExpressionCell[...] replaces the complicated structure Cell[BoxData[ToBoxes[...]]]

share|improve this answer
add comment
f[x_] := Module[{},
  Column[{x + 1, x + 2}]



(* 2 3 *)

share|improve this answer
But it is one output cell, OP wants each result in different cell. –  Kuba Nov 22 '13 at 8:34
@Kuba I would say that from his question I did not unequivocally see that. I have at least two interpretations of the question. –  Alexei Boulbitch Nov 22 '13 at 14:19
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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