0

Question about return statement in Python

What exactly does return do and how is it any different than print? I’m somewhat new to Python and SoloLearn doesn’t give a great explanation.

13th Jul 2021, 12:27 AM
Alex
Alex - avatar
6 Answers
+ 8
Alex , use of print() and return: ▪︎print uses arguments, that are sent to standard output (< screen > by default), if no arguments are given, nothing will be displayed ▪︎print() adds a newline character "\n" at the end of each call, except the < end > argument is used ▪︎print can be used in main program or in functions ▪︎return can only be used in functions. return can be followed by argument/s, that are given back to the caller. if no argument or no return is used < None > will be returned ▪︎the argument/s returned from the function can be stored in a variable and can be used for further processing. ▪︎return will stop the execution of the function immediately and gives the control flow back to the caller example function: a program uses part numbers which must have a defined formatting. the main program calls a function < chk_pn(inp) > and passing the part number in variable < inp >. the function checks it an returns < True > or <False> main program gets the returned value and can handle it.
13th Jul 2021, 11:09 AM
Lothar
Lothar - avatar
13th Jul 2021, 2:54 AM
David Ashton
David Ashton - avatar
+ 1
Print can be used anywhere in the program but return statement should only be used in the function.
13th Jul 2021, 12:32 AM
Nanda Balakrishnan
13th Jul 2021, 3:25 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
0
Thanks!
13th Jul 2021, 2:09 AM
Alex
Alex - avatar