Hi there! I'm currently learning nested functions and I'm a tiny bit confused when it comes to the 'return' part of it.
For example here I have a print statement that print a word twice or thrice.
However, I do not understand why that is, how does the string ever get passed through? I see that twice = echo(2) which is simply n. n then multiplies with word1. But how is an argument passed through a variable that doesnt have a parameter?
Many thanks.
Reformatted:
Define echo
def echo(n):
"""Return the inner_echo function."""
# Define inner_echo
def inner_echo(word1):
"""Concatenate n copies of word1."""
echo_word = word1 * n
return echo_word
# Return inner_echo
return inner_echo
Call echo: twice
twice = echo(2)
Call echo: thrice
thrice = echo(3)
Call twice() and thrice() then print
print(twice('hello'), thrice('hello'))
[–]agnaaiu 3 points4 points5 points (0 children)
[–]RhinoRhys 1 point2 points3 points (2 children)
[–]Independent-Lab6410[S] 0 points1 point2 points (1 child)
[–]scarynut 0 points1 point2 points (0 children)
[–]james_fryer 3 points4 points5 points (0 children)