When to use print vs return( in the video use conditional statements and functions together)
It's quite difficult to comprehend when to use return vs print. For instance, in the video mentioned above, we used
else:
return "save more!" instead of print.
Could you please elaborate when to use return and print.
Thanks.
1 answers ( 0 marked as helpful)
Hi Muhammad!
Thanks for reaching out.
We use return
when we want to have the ability to use the result of the function later in our code. For example, if we have a function that adds two numbers, return
gives us a result that we can use again.
We use print
when we simply want to display something on the screen (for instance if we want to check if our code is working correctly). Unlike return
, print
just displays information and doesn't give back a value for a later use.
Hope this helps.
Best,
Ivan