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 trialKendall Kelly
927 Pointsim stuck on the function quiz with squares, it doesn't make sense -python
im stuck on the function quiz with squares, it doesn't make sense -python
def square(number):
1 Answer
Eric M
11,546 PointsHi Kendall,
This code challenge is asking you to:
Create a function named square. It should define a single parameter named number. In the body of the function return the square of the value passed in.
In your code you've defined the function correctly, great work!
Now all that's left is to calculate the square and return. We square a number by multiplying it by itself. For example, 5
squared is 25
def square(number):
result = number * number
return result
Or more simply
def square(number):
return number * number
Best of luck with the Python course!
Cheers,
Eric
Kendall Kelly
927 PointsKendall Kelly
927 Pointsthank you alot, but what exactly does return mean or do? im confused on that part
Eric M
11,546 PointsEric M
11,546 PointsAh okay,
When a function runs it executes whatever code is in its code block, and when its finished executing the program continues from where that function was called. A
return
statement lets you send something back to the place that called the function so that the code can use it as it continues.For instance, here's a function that takes no arguments and asks a user their name
This function returns whatever the user typed in, so where we call this function with
get_user_name()
, you can think ofget_user_name()
as being replaced by the returned value.We might call it like this:
Or even like this:
print("Hello, {}".format(get_user_name())
In both of these cases the data the user typed in (hopefully their name!) is returned by our call to
get_user_name()
.If that's still confusing feel free to ask more questions! Functions and returned values are worth understanding.
Cheers,
Eric
Kendall Kelly
927 PointsKendall Kelly
927 Pointsahhh, thank you, will have to look more into it, its only my 3rd day coding so its alot to take in, trying to complete a lesson a day, but i might have to spend two days on functions, thanks!