basic constructions when runthis program first prints the sum of and ( )and then it prints the product ( we see that treat_xy takes function name as its first parameter inside treat_xythat function is used to actually call the function that was given as input parameter thereforeas shownwe may call treat_xy with either sum_xy or prod_xydepending on whether we want the sum or product of and to be calculated functions may also be defined within other functions it that casethey become local functionsor nested functionsknown only to the function inside which they are defined functions defined in main are referred to as global functions nested function has full access to all variables in the parent functioni the function within which it is defined short functions can be defined in compact wayusing what is known as lambda functionf lambda xyx * equivalent def (xy)return * the syntax consists of lambda followed by series of argumentscolonand some python expression resulting in an object to be returned from the function lambda functions are particularly convenient as function argumentsprint treat_xy(lambda xyx*yxyoverhead of function calls function calls have the downside of slowing down program execution usuallyit is good thing to split program into functionsbut in very computing intensive partse inside long loopsone must balance the convenience of calling function and the computational efficiency of avoiding function calls it is good rule to develop program using plenty of functions and then in later optimization stagewhen everything computes correctlyremove function calls that are quantified to slow down the code here is little example in ipython where we calculate the cpu time for doing array computations with and without helper functionin [ ]import numpy as np in [ ] np zeros( in [ ]def add(ab)return in [ ]%timeit for in range(len( )) [iadd(ii+ the slowest run took times longer than the fastest this could mean that an intermediate result is being cached loopsbest of ms per loop in [ ]%timeit for in range(len( )) [ii ( + loopsbest of ms per loop