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

I'm stuck trying to define a function to return a value. How would you create a function to return a value?

squaring.py
def square(number):
    number(5 * 5)
    return

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Hey Conor Kelly, the return, statement will return the result of an expression. If no expression is provided, a default None is returned.

So if number was set to a result of, say, 5 * 5 then the return statement could be return number, or more directly, return 5 * 5

Post back if you need more help. Good luck!!!

Okay, but say the return value is asking for it to be passed through. What do they mean by that?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

In the phrase “* return the square of the value that was passed in.*”, the value passed in is the value passed through the parameter number.

A refresher on the terminology:

Given a function definition

def func(param1):
    return param1

And a function call:

arg1 = 5
result = func(arg1)

The following terminology is correct:

  • the function func is called with the argument arg1
  • the argument arg1 is passed into the function func
  • the function parameter param1 receives the argument arg1

In reality param1 and arg1 are simply labels that point to objects in Python memory. When the function call is made, param1 is simply pointed to the same object that arg1 points to.

In the case of Treehouse code challenges, the challenge checker is exercising your code and is calling (and passing in) arguments to your function. You only need to know that the parameter you’ve defined (for example number) will receive arguments passed in by the checker.

Post back if you need more help. Good luck!!!