Search code examples
pythonfunctionprintingreturn

What's the difference from printing a function and calling a function in Python?


I am a little confused why the return statement won't work if I'm calling a function but will when I am printing it. Below is the example of code I worked with.

def get_favorite_food(): 
     food = input("What's your favorite food?")
     return 'Your favorite food' + ' ' + food + ' ' + 'is ready!'

When I try to run:

get_favorite_food()
>>>
Whats your favorite food?Macaroni

Compared to:

print(get_favorite_food())
>>>
Whats your favorite food?Macaroni
Your favorite food Macaroni is ready! 

I apologize if I am using incorrect phrasing in my question. Please correct me so I could rephrase the question for myself and others!


Solution

  • When you are calling a function that returns something, you are supposed to assign a variable to the function call which would store the return value.

    def get_favorite_food(): 
         food = input("What's your favorite food?")
         return 'Your favorite food' + ' ' + food + ' ' + 'is ready!'
    
    result = get_favorite_food()
    print(result)
    

    In the case of printing the function call, the returned value need not be stored it is directly printed.