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 trialRachel Harif
323 PointsWhen you're defining a function, can you put another previously defined function inside the new one?
I'm on the code challenge for functions. I made 1) a function to add up the integers in a list, and now I'm trying to make 2) a different function that prints a string with the results of function number 1.
1 Answer
Winnie E Tibingana
21,066 PointsHi Rachel, Yes you can. This can also be referred to as calling a function in another function.
for example I will define a function called sayHello
def sayHello():
print("Hello Rachel!")
then I will create another function called sayHelloAgain that will call the already defined sayHello
def sayHelloAgain():
sayHello()
print("Hello, again!")
and probably a third that called them both called allHellos in whatever order I would want them to execute:
def allHellos():
sayHelloAgain()
sayHello()
Finally, I could then call whichever function I would like. If I want a function that will execute everything, I can say:
allHellos()
what do u think I would execute if I wanted to call a function that will give me a final output of:
Hello Rachel!
Hope this helps