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 trialSamith Vijayan
11,318 PointsWhy is this code wrong ? def square(number): return number * number result = return square (3)
def square(number):
return number * number
result = return
square (3)
2 Answers
Blake Kazmierzak
2,491 PointsSo when you return something from a function, that allows you to use whatever you returned later on. In other words, "return" isn't used like a variable that stores something, it performs an action (which is why you got an error when you tried to store return inside a variable).
People most often will store whatever action they want to perform in a variable, and then return that variable. So if you stored number * number inside a variable, you could then return that variable whenever you run a function.
Try:
def square(number):
result = number * number
return result
square(3)
KRIS NIKOLAISEN
54,971 PointsTo store the return value of a function in a variable, do it like you would any other variable assignment. Place the variable on the left hand side, use the assignment operator, then the function call on the right hand side.
def square(number):
return number * number
result = square(3)
Samith Vijayan
11,318 PointsThank you.
Samith Vijayan
11,318 PointsSamith Vijayan
11,318 PointsThank you.