Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I don’t understand why you would use return instead of print

I am currently working on defining commands and the service I’m using wants me to use return in the command instead of print to get practice with it. I was wondering what the difference between the two and some benefits of each of the two.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Printing in a function is used to display output, such as

def greet(name):
    print(f"Hi, {name}!")

greet("Person")  # Prints "Hi, Person!"

While returning can be used to assign a variable used in the function into a variable outside of the function, such as

def add(a, b):
    return a + b

result = add(4, 5)
print(result)  # Prints 9

Basically, use return when you want to compute a value within the function and send it back to the main program to be used, and use print when you want to display information without affecting the function’s return value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading