Next: Noweb reference syntax, Previous: Header arguments, Up: Working With Source Code

### 14.9 Results of evaluation

The way in which results are handled depends on whether a session is invoked, as well as on whether `:results value` or `:results output` is used. The following table shows the possibilities:

| Non-session | Session
`:results value` | value of last expression | value of last expression
`:results output` | contents of STDOUT | concatenation of interpreter output

Note: With `:results value`, the result in both `:session` and non-session is returned to Org-mode as a table (a one- or two-dimensional vector of strings or numbers) when appropriate.

#### 14.9.1 Non-session

##### 14.9.1.1 `:results value`

This is the default. Internally, the value is obtained by wrapping the code in a function definition in the external language, and evaluating that function. Therefore, code should be written as if it were the body of such a function. In particular, note that Python does not automatically return a value from a function unless a `return` statement is present, and so a ‘`return`’ statement will usually be required in Python.

This is the only one of the four evaluation contexts in which the code is automatically wrapped in a function definition.

##### 14.9.1.2 `:results output`

The code is passed to the interpreter as an external process, and the contents of the standard output stream are returned as text. (In certain languages this also contains the error output stream; this is an area for future work.)

#### 14.9.2 Session

##### 14.9.2.1 `:results value`

The code is passed to the interpreter running as an interactive Emacs inferior process. The result returned is the result of the last evaluation performed by the interpreter. (This is obtained in a language-specific manner: the value of the variable `_` in Python and Ruby, and the value of `.Last.value` in R).

##### 14.9.2.2 `:results output`

The code is passed to the interpreter running as an interactive Emacs inferior process. The result returned is the concatenation of the sequence of (text) output from the interactive interpreter. Notice that this is not necessarily the same as what would be sent to `STDOUT` if the same code were passed to a non-interactive interpreter running as an external process. For example, compare the following two blocks:

         #+begin_src python :results output
          print "hello"
          2
          print "bye"
         #+end_src

         #+resname:
         : hello
         : bye

In non-session mode, the `2' is not printed and does not appear.

         #+begin_src python :results output :session
          print "hello"
          2
          print "bye"
         #+end_src

         #+resname:
         : hello
         : 2
         : bye

But in `:session` mode, the interactive interpreter receives input `2' and prints out its value, `2'. (Indeed, the other print statements are unnecessary here).