Hello, why return instead of print
Why is it we type return instead of print on this video, while the previous one, after if statement, we type print.
Thanks for reaching out!
return when we want to return some result. But nothing is shown on console. To have the result in the console, we use
Let me show you an example. If you have:
We call the function with an argument and it works! Fantastic!
But if you want to use
What do we have here? The same function but instead of having
return. So, if we invoke our function with an argument of 5 (i.e. by executing ), nothing will be shown in the console. So, we can use
When this code is executed it shows
To sum it up, when using
return, we will only store the result of the function when invoked. We can later display that result by using the
print() function outside the function we had created (in this example -
Hope this helps.