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 :
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.