Return vs. Print: BEHIND THE SCENES!
Return vs Print
Hi! Welcome to this section. We will talk about the difference between Returning and Printing a value inside a function.
We will discuss what happens when we don’t explicitly return anything from a function and how to assign the value returned to a variable.
Let get started!
In the diagram you can see some essential characteristics of return and print (be sure to use lowercase letters for these keywords, otherwise Python will throw an error)
Let's talk about return
:
- This is basically how the return statement works:
- Here you can see a diagram of the being picture of how return works and how you can assign the returned value to a variable.
Let's talk about print
- Now… let’s talk about “print”. Surely you’ve used this statement before for debugging. But what happens if we only use a print statement and not a return statement?
Let’s see what happens!
- In this case, when a print statement is inside a function, we can draw this diagram to get a sense of what happens if no explicit return statement is found.
- You can see that if no return statement is found, the value returned will be
None
, and this will be assigned to the variable
(if you decide to assign the value returned to a variable, you can always execute a function without assigning the returned value to a variable)
- In this case, the value of b will be
None
.
Hope this helps!
If you have any questions, Community TAs are always there to help you on the forums! :-)
Estefania.
Comments
Post a Comment