Why do Def programs use return instead of print? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Why do Def programs use return instead of print?

edit: Thank you for the answers! I understand after reading them and playing with it a while.

28th Nov 2016, 2:40 AM
The Nerd Rants
The Nerd Rants - avatar
4 Réponses
+ 4
it use to send data to a new variable where you need that, and suppose you want to use some function that use to get some unique string from a file, and than want to store that string in MySQL. If you don't use return value, than you have to write it again and again. Writing function for a particular job increase your programing performance.
28th Nov 2016, 2:53 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
Sometimes you need to use the answer of a def for something instead of printing. It is much more flexible. A very common way is to set it to a variable in instead of printing.
28th Nov 2016, 5:04 AM
Grady
Grady - avatar
0
Err. Imagine having to type every single result for every single calculation, and compare it with the flexibility of functions. Not even close to being comfortable.
28th Nov 2016, 9:58 PM
Amaras A
Amaras A - avatar
0
use return when you need to asign the result of the function to a variable def max(a,b): if a>b: return a return b nMax=max(2,3) print(nMax) #prints 3
29th Nov 2016, 1:26 AM
Allan Navarro
Allan Navarro - avatar