函数式编程

函数式编程

面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计。函数就是面向过程的程序设计的基本单元。

函数式编程:是使用一系列函数去解决问题,函数式编程就是根据编程的范式来,得出想要的结果,只要是输入时确定的,输出就是确定的。

高阶函数:1、函数接收的参数是一个函数名 2、返回值中包含函数把函数当作参数传给另外一个函数

def foo(n): #n=bar
    print(n)

def bar(name):
    print('my name is %s' %name)

foo(bar)
foo(bar('alex'))

输出

<function bar at 0x0000000002985620>
my name is alex
None
#返回值中包含函数
def bar():
    print('from bar')
def foo():
    print('from foo')
    return bar
n=foo()
n()

输出

from foo
from bar
def hanle():
    print('from handle')
    return hanle
h=hanle()
h()

输出

from handle
from handle
原文地址:https://www.cnblogs.com/liushuizs/p/10336753.html