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

return and print functions

Hi guys,

would need some advice regarding the print and return functions. Can you please explain me easily when to use these 2 functions and what the difference between these 2 is?

Thank you!

BR

2 Answers

Rahul Saini
Rahul Saini
4,611 Points

There is no validation on what to use specifically. You can use any according to your need.

For example
def sum(a,b):
    return a+b

def main():
    x = sum(9,7)
    print(sum)

or you can use 


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

def main():
    sum(9,7)

either way, you will get the same result. It's really up to you. what you prefer?
Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

return isn't a function, it's a keyword. It lets functions have a return value. You can use the value returned from a function somewhere else by assigning it to a variable, or by using it as an argument to another function.

def giveMeANumber():
    return 42

num = giveMeANumber()
sum = addNumbers(num, 5)
sum2 = addNumbers(giveMeANumber(), giveMeANumber())

print is a function. It doesn't return anything, but it has a side effect of sending output to the console.

print("hello world") # you'll see "hello world" in the console
hello_world = print("hello world") # this doesn't work....