函数(上)

1.基本的形式

def 函数名(参数):
    函数体
    return  》》》可以返回多个值(此时以元组的形式输出)
                》》》可以返回多个值
                》》》可以没有值(此时返回none)

注意:函数一碰到return就会停止

2.函数参数

(1)形参:即定义参数时的参数如:def chen (x)  >>>>>x即为形参。形参不占用内存。

(2)实参:具体值代入函数时,该具体值即为实参。如:chen(5)  >>>>>在该函数中x=5即为实参。

(3)位置参数:即在把具体参数代入函数时按参数的顺序一 一对应的情况。如图中:x=1,  y=2,  z=3

def test (x,y,z):
    print()
    return 

test(1,2,3)

(4)关键字参数:明确说明哪个参数等于哪个值。此时不要 一 一 对应。

def  test (x,y,z):
    print()
    return


test(y=2,x=1,z=3)

 (5)关键字参数与位置参数混合使用时,位置参数只能位于关键字参数的左边,即:test(1, 2, z=3)

(6)默认参数:创建函数时已经有规定参数的值了。当你没有输入该参数的值时,默认运用该规定的值。但当你输入值时,以你输入的值为准。

(7)参数组:def  test(x, *args,  **kwargs)》》》》args:将剩下的位置参数以元组的形式输出,kwargs:将关键字参数以字典的形式输出

def  test(x, *args,  **kwargs):
       print(x)
       print(args)
       print(kwargs)




test(1,2,5,6,8,7,a=1,b=2,c=3)



>>>1
>>>(2,5,6,8,7
>>>{"a":"1","b"="2","c"="3"}

注意:

(1)如输入的是一个列表时,注意在列表前加   *

  如输入的是一个字典时,注意在字典前加   **

def test(x,*args,**kwargs):
    print(x)
    print(args)
    print(kwargs)
    return

test(1,[1,2,3,4],{"a":"1","b":"2"})



》》》1
》》》([1, 2, 3, 4], {'a': '1', 'b': '2'})
》》》{}
def test(x,*args,**kwargs):
    print(x)
    print(args)
    print(kwargs)
    return

test(1,*[1,2,3,4],**{"a":"1","b":"2"})


》》》1
》》》(1, 2, 3, 4)
》》》{'a': '1', 'b': '2'}

(2)一个参数不能传两个值,否则会报错。

def test(x,*args,**kwargs):
    print(x)
    print(args)
    print(kwargs)
    return

test(1,1,2,3,4,x=5,y=6)

>>>
Traceback (most recent call last):
  File "C:/Users/chenweitao/.PyCharmCE2018.3/config/scratches/scratch_1.py", line 77, in <module>
    test(1,1,2,3,4,x=5,y=6)
TypeError: test() got multiple values for argument 'x'

3.局部变量与全局变量:

(1)全局变量:没有缩进时定义的变量。

(2)局部变量:在子程序里设置的变量。

(3)在一段子程序中,首先在自己的程序中找局部变量,如果没有局部变量,就会引用全局变量。

(4)在子程序中设置和全局变量一样的局部变量时并不会改变全局变量的值。但当在局部变量前加一个global 时,此时设置于全局变量一样的变量时,会改变全局变量。

name = "chenweitao"
def cwt():
    name = "chenweinan"
    print(name)


print(name)
cwt()

>>>>>chenweitao
>>>>>chenweinan
name = "chenweitao"
def cwt():
    global name
    name = "chenweinan"
    print(name)

print(name)
cwt()

>>>>>chenweinan
>>>>> chenweinan   

    
            

(5)子程序中镶上子程序时,若想引用上级变量,可以在该子程序中加  nonlocal   变量 ,在此情况下改变变量的话,上级变量也会跟着改变。

name = "chenweitao"
def ming1():
    global name
    print(name)
    def ming2():
        name = "chenweinan"
        print(name)
        def ming3():
            nonlocal name
            print(name),
        ming3()
    ming2()



ming1()


>>>>chenweitao
>>>>chenweinan
>>>>chenweinan

 (6)在写代码时,最好的选择是全局变量用大写 ,局部变量用小写。

4.函数即变量。函数名相当于地址,将函数体里的程序存入内存中。当想用某个函数时,需要引用函数(前向引用)才会对函数体的程序进行运行。

name = "chenweitao">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    1
def ming1():    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   2
    global name       >>>>       >>>>>>>>>    >>>>>>>>>>>>>>>>>>4
    print(name)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>5
    def ming2():>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.6
        name = "chenweinan">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.8
        print(name)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>9
        def ming3():>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>10
            nonlocal name>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>12
            print(name)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>13
        ming3()>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>11
    ming2()>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>7


调用函数
ming1()                              >>>>>>>>>>>>>>>>>>>>>>>   3

程序在读代码的顺序。

原文地址:https://www.cnblogs.com/chenweitao/p/11231519.html