Nested function:
1, the nested function calls: a function call in the process of a call other functions
# Effect: I Can be a large function, dismantling a plurality of small features, and finally implemented in the major functional, the nested function calls instance 1 as follows:
# DEF foo ():
# bar ()
# foo ()
# nested functions example 2: comparison of four maximum values, ideas: pairwise comparison
DEF MAX2 (X, Y):
IF X> Y:
return X
the else :
return Y
DEF MAX4 (a, B, C, D):
# step: comparison a, b to give RES1
RES1 = MAX2 (a, b)
# step: Comparative res1, c give RES2
RES2 = MAX2 (res1, c)
# step Three: Comparative res2, d to give RES3
RES3 = MAX2 (RES2, D)
return RES3
RESMax4 = (1,2,3,4 )
Print (RES)
# advantages: clear, less than max4 code.
2, the nested function is defined: defined functions within a function
# Examples 1:
# DEF f1 (): # f1 in the global
# DEF f2 (): # f2 locally, how to use the global f2? Currently only the f2 into global
# Pass
# for round: find the circumference, find the area
# If I only for circular operation, then they would with the nested definitions
DEF Circle (RADIUS, Action = 0):
# Import pi function
from Math Import pi
# seeking a circular circumference: 2 * pi * RADIUS
DEF perimiter (RADIUS):
return 2 * pi * RADIUS
# seeking a circular area: pi * (2 ** RADIUS)
DEF area (RADIUS ):
return PI * (2 ** RADIUS )
IF Action == 0:
return perimiter (RADIUS)
elif Action ==. 1 :
return Area (RADIUS)
the else :
Print ( " without this feature !!! " )
Circle ( 2, Action = 2)