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: return and print inside function

This might be a silly question but Why can't you use print() and return inside the same function?

def add_int(): answer= 4*5 return answer print(answer) add_int()

This only passes the return but doesn't print answer. However, if you pull the print() statement out of the function

def add_int(): answer= 4*5 return answer print(add_int())

The function add_int() is printed. I can remember that it doesn't work but I would like to understand why, please.

1 Answer

When you use the return keyword, it immediately exits the function. Any code placed after the return keyword will not run. Try placing your print statement just before the return keyword.

Thanks John! That actually makes a lot of sense.