Python学习之路:函数介绍

编程:面向对象:华山派----->类---->class

         面向过程:少林派----->过程--->def  一段段的函数和功能包含在过程中

         函数式编程:逍遥派--->函数---->def

         过程就是没有return返回值的函数

函数的定义:数学函数的定义,两个变量x和y,y=2x;

编程语言中函数的定义:函数是逻辑结构化和过程化的一种编程方法。

 1 #定义函数
 2 def test(x):
 3     "The function definition"
 4     x+=1
 5     return x #函数的完整定义
 6 
 7 def func1():
 8     "testing"
 9     print("in the func1")
10     return 0
11 
12 
13 #定义过程
14 def func2():
15     "testing"
16     print("in the func2")
17 
18 #调用函数:
19 x=func1()
20 #调用过程
21 y=func2()
22 
23 print('from func1 return is %s'%x) #返回0
24 print("from func2 %s"%y)#返回None

函数式编程的优点:可扩展、保持一致性、代码重用性

import time

def logger():
    time_format ='%Y-%m-%d %X'
    time_current =time.strftime(time_format)

    with open('a.txt','a+') as f:
        f.write('%s end action
'%time_current)



def test1():
    print('in the test1')

    logger()


def test2():
    print('in the test2')

    logger()

def test3():
    print('in the test3')


    logger()

x=test1()
y=test2()
z=test3()
原文地址:https://www.cnblogs.com/xiaobai005/p/7828814.html