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 Create a Function

im 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

squaring.py
def square(number):

1 Answer

Eric M
Eric M
11,546 Points

Hi 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

thank you alot, but what exactly does return mean or do? im confused on that part

Eric M
Eric M
11,546 Points

Ah 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

def get_user_name():
        name = input("What is your name?  ")
        return name

This function returns whatever the user typed in, so where we call this function with get_user_name(), you can think of get_user_name() as being replaced by the returned value.

We might call it like this:

username = get_user_name()
print("Hello, {}".format(username))

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

ahhh, 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!