"""" Demo code from lecture on Sep 5th. Demonstrate how a function can be passed as a function to another function. """ """ The function diff(...) estimates the derivative of a function f in a point x. The first parameter to the function is a Python function f. From how f is used inside the function, we see that it must be defined with a single parameter. """ def diff(f, x, h=1e-6): r = (f(x+h) - f(x)) / h return r #define python function for a mathematical formula def g(x): return x**2 - 2 * x #call diff with g as the first argument print(diff(g, x = 1.5)) """ Notice that there are no parantheses after g. We pass the actual function, not the result of a function call. """ #alternative version using a lambda function: print(diff(lambda x: x**2 - 2 * x, 1.5)) """ Terminal> python func_argument.py 1.0000009997845893 1.0000009997845893 """