Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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!

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