Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Returning Values

What is the purpose of return instead of a print, if we are going to be using it later?

We could just include the print line inside the function and not use the return keyword. Is there any special ocassion for using return.

Like this:

import math

def split_check(total, number_of_people):
    cost_per_person = math.ceil(total / number_of_people)
    print("Each person owes {}!".format(cost_per_person))

total_due = float(input("What is the total?  "))
number_of_people = int(input("How many people?  "))
split_check(total_due, number_of_people)

2 Answers

Return can be used to store the result of a function so it can be used it later on.

def myAge():
  return 30

# store result of function in variable
age = myAge()

# my age in 10 years
print(age+10)

Note that this is a contrived example. Let me know if this helps!

David Marmon
David Marmon
196 Points

not an answer, a question: why 'return' a parameter value? you've already assigned it when you define the parameter.