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 trialm3shack
Python Development Techdegree Student 279 PointsI understand the 1st part of the quiz, but not the 2nd part of the quiz.
I passed the quiz at this link. https://teamtreehouse.com/library/python-basics-3/functions-and-looping/create-a-function
The first half I can understand.
def square(number):
return number ** 2
square(5)
The second half I passed but do not understand what I did to pass
def square(number):
return number ** 2
result = square(3)
print(result)
I read through several books on returns and functions nothing helped. I came across this guy https://www.youtube.com/watch?v=nuNXiEDnM44 and I kinda saw what he was saying so tried what I thought he meant it worked but I dont fully understand.
can someone please explain what I did right here?
2 Answers
Jassim Alhatem
20,883 PointsIt's basically the same as the first one. But you just passed in a different number and you stored it in a variable.
Here's another example:
def hello(word):
return word + " Nate"
result1 = hello("Greetings")
result2 = hello("How are you")
result3 = hello("How are you doing")
print(result3)
You can reuse any of the results anytime you want, without having to pass in the same argument over and over again.
Adding a function to a variable makes your code cleaner, and follows the DRY principle.
m3shack
Python Development Techdegree Student 279 PointsJassim Al-Hatem, Awesome! That helped me greatly.