What is the difference between print and return in a function?
Options
- They are the same: both show text and also end the function
printshows text on screen;returnsends a value back to the caller (Correct option)returnshows text on screen;printends the function nowprintsaves a value in a file;returnclears the screen
Answer
print shows text on the screen; return sends a value back to the code that called the function so it can be used
Why
A very common beginner confusion.
print(x) only displays x for a human to read. The program cannot use it afterwards.
return x hands x back to the caller: total = add(2, 3) stores 5 in total.
If a function only prints and does not return, total would be None.
Remember
print = show. return = give back.