function-type programming
Functional programming is a kind of programming method, it treats the computer operation as the function computation; In layman's terms, a mathematical logic is used to define an expression, and then a function is used to implement this function.
Example (python3.0+):
y = 2*x+1 ----first define a mathematical expression def cal (x): "' Description: function to implement the above expression" ' return 2*x+1# the above procedure is functional programming
How can I tell if a function is functional programming?
- Do not use variables to save state (do not assign values to variables), do not modify variables
Example (python3.0+):
# Non-functional Programming n = 1def cal (): global n n+=1 return n cal () # Functional Programming n =1 def CAL (N): return n+1cal (10)
function is variable
In Python programming, there is a saying: function is variable, how to embody it?
Example (python3.0+):
def foo (n):p rint (n) def Bar (name): print (' My name is Sunwk ') foo (bar) # treats the function bar (that is, the memory address of the bar function) as a variable (parameter) Passed to the Foo function result:>>> <function bar at 0x0000000000fa58c8>
Higher order functions
With know more knowledge, contact surface wide, also more and more confused, just began to contact higher-order functions, what is the advanced function Ah, first a few examples to see it
Example (python3.0+):
def foo (): print (' from foo ') def Bar (): print (' from bar ') return FOOA = Bar () print (A ()) # Run result:>>> From Bar>>>from foo>>>none "" "Why did you finally output a None, because print (a ()), first executes a () relative to execute foo (), but after print, the equivalent of output foo ( Return value is not defined in the Foo function, so return none "" "
Summary of higher-order function characteristics:
- The parameter received by the function is a functional name
- The return value contains the function
Python3-Functional programming