0

I don't know why result = my_function() returns only hello there and what's up, and print(result) returns only 52.

(Code)

def my_function():
    print("hello there")
    print("what's up")
    return 52

print("done")
result = my_function()
print(result)
suki5523
  • 9
  • 1
  • 1
    Welcome to Stack Overflow. *What do you think it should do instead? Why*? If you don't have an alternative idea about what should happen, then there is nothing we can explain - it does what it does because that's what the code says should be done. If you have a different idea, then we can possibly explain a misconception. – Karl Knechtel Jan 14 '22 at 05:37
  • Print and return are not the same thing. – Mateen Ulhaq Jan 14 '22 at 05:38
  • 2
    That said: `my_function()` **does not** return either `'hello there'` or `"what's up"`. It returns `52`. That's the point of the code that says `return 52` - `return` means "stop here and return something", and `52` is the thing that gets returned. `print(result)` **does not** return `52`. It returns `None`. Printing things and returning them have *absolutely nothing to do with each other*. – Karl Knechtel Jan 14 '22 at 05:38
  • `print` just show the value at the running time in interpreter to user whereas `return` give the output from the function to functional call. to whatever you want after function execution stop then use return on that variable/object. and to see the value at any stage of a object use print – sahasrara62 Jan 14 '22 at 05:41

1 Answers1

0

You are saving the value being returned from my_function to the variable result.
print(result) isnt returning anything, it is simply printing the value that was already returned when you called my_function.

qTzz
  • 126
  • 6