Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>print()</code> function writes, i.e., "prints", a string in the console. The <code>return</code> statement causes your function to exit and hand back a value to its caller. The point of functions in general is to take in inputs and return something. The <code>return</code> statement is used when a function is ready to return a value to its caller. </p> <p>For example, here's a function utilizing both <code>print()</code> and <code>return</code>:</p> <pre><code>def foo(): print("hello from inside of foo") return 1 </code></pre> <p>Now you can run code that calls foo, like so:</p> <pre><code>if __name__ == '__main__': print("going to call foo") x = foo() print("called foo") print("foo returned " + str(x)) </code></pre> <p>If you run this as a script (e.g. a <code>.py</code> file) as opposed to in the Python interpreter, you will get the following output:</p> <pre><code>going to call foo hello from inside foo called foo foo returned 1 </code></pre> <p>I hope this makes it clearer. The interpreter writes return values to the console so I can see why somebody could be confused.</p> <p>Here's another example from the interpreter that demonstrates that:</p> <pre><code>&gt;&gt;&gt; def foo(): ... print("hello from within foo") ... return 1 ... &gt;&gt;&gt; foo() hello from within foo 1 &gt;&gt;&gt; def bar(): ... return 10 * foo() ... &gt;&gt;&gt; bar() hello from within foo 10 </code></pre> <p>You can see that when <code>foo()</code> is called from <code>bar()</code>, 1 isn't written to the console. Instead it is used to calculate the value returned from <code>bar()</code>. </p> <p><code>print()</code> is a function that causes a side effect (it writes a string in the console), but execution resumes with the next statement. <code>return</code> causes the function to stop executing and hand a value back to whatever called it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload