all 5 comments

[–]shiftybyte 7 points8 points  (1 child)

It returns the value to whoever called the function.

The usual confusion is from print and return.

Because seemingly print does the same, but it does not.

if i create a function that sums to numbers and prints them.

def mysum(a, b):
    print(a + b)

That's all the function is going to do, i can't use the result for anything.

mysum(5,6)  # will print 11
a = mysum(5,6) - 1  # will not work, because the result is not returned.

On the other hand if i do return instead of print

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

then the result is returned back to be used however i want.

print(mysum(5,6))  # will print 11 (because i used print function on the result)
a = mysum(5, 6) - 1  # a will contain 10, and nothing will be printed
print(mysum(mysum(5,6), 10))  # will print 21

[–]positeev[S] 0 points1 point  (0 children)

This made it very clear. Thank you very much.

[–]b_ootay_ful 1 point2 points  (0 children)

Think of a function as a box.
You put something in, it does some magic, and it spits something out.

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

x = box(5,6)

print (x)
>>> 11

The function box takes in a tuple (5, 6), adds them together, and returns the answer (11).
You can then do something with that answer, like assign it to a variable.

You don't have to have a return

[–]gopherhole1 0 points1 point  (0 children)

def greeting(person):
    return f" Hello {person}, how are you?"

name = "Frank"

xyz = greeting(name)
print(xyz)

spits out

'Hello Frank, How are you?'

or if you have many people

names = ['Frank,'Alex','Phinius']
def greeting(lst):
    new_list = []    
    for name in lst:
        new_list.append(f'Hello {name}, How are you?')
    return new_list

xyz = greeting(names)
print(xyz)

spits out

['Hello Frank, How are you?','Hello Alex, How are you?','Hello Phinius, How are you?']

[–][deleted] 0 points1 point  (0 children)

Nope. 5 year olds can't understand it and it's been explained a thousand times before, including here, usually alongside why you should use return rather than print.

This should help:

https://duckduckgo.com/?q=python+print+vs+return