Max H.

# Nested Functions

Hi, I know the general idea behind nested functions, and how one function can call another inside it. But this example, from Python, confuses me a bit. One function totalPct, is called using a startValue and years. But how can makeTotalPct return totalPct (in the form of pctIncrease) if no pct value was provided anywhere? I only gave startValue and years, but no pct.

def pctIncrease(begin, end):
return 100*(end/begin-1)

def increaseByPct(begin, pct):
return begin+begin*pct/100

def makeTotalPct(startValue, years):
def totalPct(pct):
value = startValue
for year in range(years):
value = increaseByPct(value, pct)
return pctIncrease(startValue, value)

totalPct = makeTotalPct(175.1, 5)
totalPct2 = makeTotalPct(154.4, 10)

Also, why do I not write "return totalPct()" instead of "return "totalPct"? Aren't I calling the function, which would give me the return value? I greatly appreciate any help.

By: Tutor
4.7 (153)

Computer Science, Math, Physics Tutoring

Max H.

Thank you Greg--I edited the question. So as it currently stands, the makeTotalPct function with values 1910 (startValue) and 20 (years), this would give me a totalPct function where in the future I could provide a pct, and it would automatically use those two values in it?

However, how would this work if I ended with totalPct()? Doesn't totalPct require a value? But how would I enter the value in this function, if makeTotalPct only takes in two values? Or would I have to edit it so the function is makeTotalPct(startValue, years, pct)? Would this create a function, and then run it right afterwards?

Thank you!
Report

03/16/15 Greg H.

Max:

The key here is to recognize that a function is an object in Python.  You can treat that object like any other Python object, which means you can do things like pass it into a function as an argument or return it as a function value.

"totalPct" is interpreted as a reference to a function object.   This reference can be assigned, returned, etc., just like any other object.
"totalPct()"  uses the object reference to call the function.  The parentheses are a signal to the interpreter that you want to call the function.  totalPct(1.0) passes a value of 1.0 to the totalPct function.

When you say "return totalPct", you're returning a reference to the function object.
When you say "return totalPct()", you're asking the interpreter to call the function and return its result.

The startValue and years arguments are effectively stored as attributes of the function object so they can be used when you make the function call later on.  This is possible because of Python closures.  If you want the gory details, google "python closure".  Note that this is a topic that raises a lot of passion in language purists.

I've modified your code slightly so it calls the function returned by makeTotalPct().  Try it yourself to see how it works (Python 2.x; you'll need to convert the print statements to functions for Python 3).

-------

def pctIncrease(begin, end):
return 100*(end/begin-1)

def increaseByPct(begin, pct):
return begin+begin*pct/100

def makeTotalPct(startValue, years):
def totalPct(pct):
value = startValue
for year in range(years):
value = increaseByPct(value, pct)
return pctIncrease(startValue, value)
print "totalPct: ", totalPct
print "totalPct(1.0): ", totalPct(1.0)

totalPct = makeTotalPct(175.1, 5)
result1 = totalPct(1.0)
result2 = totalPct(2.0)
print "result1: ", result1
print "result2: ", result2

totalPct2 = makeTotalPct(154.4, 10)
result3 = totalPct2(3.0)
result4 = totalPct2(4.0)
print "result3: ", result3
print "result4: ", result4

Report

03/17/15

## Still looking for help? Get the right answer, fast.

Get a free answer to a quick problem.
Most questions answered within 4 hours.

#### OR

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.