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 McKibbin
MOD
Eric McKibbin
Treehouse Moderator 11,441 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 McKibbin
Eric McKibbin
Treehouse Moderator 11,441 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!