Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

def square(number):
    number(5 * 5)

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 67,893 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 67,893 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!!!