2

Consider the following piece of code.

def calculate(x,y,z=3,w=4):
    return(x+y+z+w)

calculate(2,3,4)+5

In the above snippet, the function would return the value 13 to the calling function. But the output isn't displayed as return doesn't print any value and the program terminates. This is how it is in majority of the IDE's.

However, when I run the same program on Jupyter Notebook, it gives me the following Output. Jupyter Notebook Output

How is 13 getting printed? What is Jupyter Notebook doing that other IDE's don't?

  • 2
    Isn't that `18` in the output? – aminrd Feb 28 '22 at 23:50
  • @aminrd Yes. Sorry about that. – Suraj Suresh Mar 01 '22 at 00:11
  • 1
    important to understand, `return` is **not a function**, it is part of a statement. Your parentheses around return, `return(x+y+z+w)` are superfluous, and should be removed. – juanpa.arrivillaga Mar 01 '22 at 00:25
  • The question is very confusing: The text says something different than the result shown (13 vs 18). Why did not you fix that? The example is unnecessarily complicated there was no effort done to find a simpler example. - Probably during that finding process you would easily learn yourself much more. – pabouk - Ukraine stay strong Jun 08 '22 at 06:45

2 Answers2

5

A Jupyter notebook implements a Read-eval-print loop (REPL) which means that the output of the last line of a cell in a Jupyter notebook will always be printed.

There is a great article in the Jupyter docs which explains the basics of the concept really well.

David Scholz
  • 8,421
  • 12
  • 19
  • 34
1

The Jupyter notebook acts as a console instead of a script. If you enter an operation in a python console it will output the solution. There are no print functions needed. E.g.:

>>> 2 + 2
4
f-grimm
  • 76
  • 7