函数(Python)

函数是什么?

  计算机的函数,是一个固定的一个程序段,或称其为一个子程序,它在可以实现固定运算功能的同时,还带有一个入口和一个出口,所谓的入口,就是函数所带的各个参数,我们可以通过这个入口,把函数的参数值代入子程序,供计算机处理;所谓出口,就是指函数的函数值,在计算机求得之后,由此口带回给调用它的程序。

使用函数的特性:减少代码重复,程序可扩展,容易维护。

语法定义:

 1 # @Software: PyCharm
 2 def say():#定义函数
 3     print('hello world')
 4 
 5 say()#调用函数 say
 6 
 7 
 8 
 9 hello world
10 
11 Process finished with exit code 0
View Code
# @Software: PyCharm
a,b = 5,2
def calc (x,y):
    res = x**y
    return  res
c = calc(a,b)
print(c)




25

Process finished with exit code 0

函数的参数:

形参:变量只有在被调用的时候才被分配内存单元,调用结束即释放内存单元,也就是只有在函数内部有效。

实参:必须有确定的值,将值传递给形参。

# @Software: PyCharm
a,b = 5,2#a,b 实参
def calc (x,y):#x,y 为形参
    res = x**y
    return  res
c = calc(a,b)#实参传递给形参
print(c)




25

Process finished with exit code 0
View Code

位置参数:调用函数时根据函数定义的参数位置来传递参数。

def students(name,age,sex,num):#name age sex num 为位置参数
    print('下面是学生信息')
    print('名字:',name)
    print('年龄:',age)
    print('性别',sex)
    print('学号:',num)

students('jack',22,'boy',150313)
students('siri',5,'girl',180254)



下面是学生信息
名字: jack
年龄: 22
性别 boy
学号: 150313
下面是学生信息
名字: siri
年龄: 5
性别 girl
学号: 180254

Process finished with exit code 0

默认参数:默认参数指的是当函数调用中省略了实参时自动使用的一个值。例如,如果将void wow(int n)设置成n有默认值为1,则函数调用wow()相当于wow(1)。这极大地提高了使用函数的灵活性。

def students(name,age,sex,num,addr='0370'):#addr为默认参数
    print('下面是学生信息')
    print('名字:',name)
    print('年龄:',age)
    print('性别',sex)
    print('学号:',num)
    print('学会',addr)    

students('jack',22,'boy',150313)
students('siri',5,'girl',180254)





下面是学生信息
名字: jack
年龄: 22
性别 boy
学号: 150313
地址 0370
下面是学生信息
名字: siri
年龄: 5
性别 girl
学号: 180254
地址 0370

Process finished with exit code 0

 非固定参数:在定义时不确定需要传入多少参数。

# @File    : jia.py
def students(name,age,*args):#*args 将多余的值放入一个元组中
    print(name,age,args)

students('jack',22)
students('siri',1,'girl','read')




D:untitledvenvScriptspython.exe D:/untitled/bogls/jia.py
jack 22 ()
siri 1 ('girl', 'read')

Process finished with exit code 0
# @File    : jia.py
def students(name,age,*args,**kwargs):#**kwargs 将多余的值放入字典中
    print(name,age,args,kwargs)

students('jack',22)
students('siri',1,'girl','read',sex='girl',addr='0370')





jack 22 () {}
siri 1 ('girl', 'read') {'sex': 'girl', 'addr': '0370'}

Process finished with exit code 0

 全局与局部变量:

在子程序中定义的变量为局部变量,在程序的一开始定义的变量称为全局变量。全局变量的作用域是整个程序,局部变量的作用域是定义该变量的子程序。当局部变量与全局变量重名时,在定义局部变量的子程序内,局部变量起作用,在其他地方全局变量起作用。

# @File    : jia.py
a,b = 5,2
def square(a,b):
    print('函数内,定义局部变量前a**b=%s'%(a**b))
    a,b = 5,1
    print('函数内,定义局部变量后a**b=%s'%(a**b))

square(a,b)
print('函数外,定义局部变量后a**b=%s'%(a**b))




D:untitledvenvScriptspython.exe D:/untitled/bogls/jia.py
函数内,定义局部变量前a**b=25
函数内,定义局部变量后a**b=5
函数外,定义局部变量后a**b=25

Process finished with exit code 0

 函数返回值:

要想获取函数的执行结果,可以使用return语句把结果返回。函数在执行过程中遇到return语句,就会停止执行并返回结果,return语句代表着函数的结束

def square(name,a,b):
    my_name = name
return my_name
square_ab = a**b
print(square_ab)
a = square('jia',5,2)
print(a)





D:untitledvenvScriptspython.exe D:/untitled/bogls/jia.py
jia

 D:untitledvenvScriptspython.exe D:/untitled/bogls/jia.py jia Process finished with exit code 0

如果没有指定return,则函数返回值是None

def square(name,a,b):
    my_name = name
    #return my_name
    square_ab = a**b
    print(square_ab)
a = square('jia',5,2)
print(a)



D:untitledvenvScriptspython.exe D:/untitled/bogls/jia.py
25
None

Process finished with exit code 0

递归函数:

在函数内部,可以调用其它函数。如果一个函数在内部调用自身,这个函数就是递归函数。递归的典型案例,二分查找http://www.cnblogs.com/serpent/p/8970561.html(一个简单的二分查找)

递归的特性:有一个明确的结束条件;每进入更深一层递归,问题规模减小。

匿名函数:匿名函数和其他函数一起用非常强大

 1 res = map(lambda x:x**2,[1,2,3,4,5])
 2 for i in res:
 3     print(i)
 4 
 5 
 6 
 7 D:untitledvenvScriptspython.exe D:/untitled/bogls/jia.py
 8 1
 9 4
10 9
11 16
12 25
13 
14 Process finished with exit code 0
原文地址:https://www.cnblogs.com/serpent/p/8969986.html