函数是一组语句,它们接受输入、进行一些特定的计算并产生输出。
函数存在的意义:
1. 提高代码的复用性
2. 将复杂的逻辑简单化,分功能化
函数定义:
小知识 : []表示可选 <>表示必备
函数名字最好能直接表示该函数的功能,多个单词之间用_链接
<def> <函数名>([参数列表])<:>
# 待执行语句
# 如果有需要显式返回<return 标识符>或<return>
Python提供了内置函数,如print()等。用户定义函数:可以创建自己的函数
# A simple Python function to check
# whether x is even or odd
def evenOdd( x ):
if (x % 2 == 0):
print "even"
else:
print "odd"
# Driver code
evenOdd(2)
evenOdd(3)
产出:
even
odd
函数的返回值
函数可以返回任何类型的数据,函数内执行到return后结束,后面代码不再执行.
def func():
res = 1+1
print('我执行了')
return res
print('我不会执行')
print('程序正常执行')
# 函数内的语句确实是执行了,打印我执行了
func()
# 想要拿到函数return的值需要重新找个变量接收一下
# 注意这里再次打印了一次我执行了 因为函数再次调用了一次
res = func()
print(res)
# 不写return默认返回None
def func():
print('我执行了')
res = func()
print(res)
多个返回值
多个变量接收
def func(): return 1,2,3
a,b,c = func()print(a,b,c)
输出:

一个变量接收是一个元组
def func(): return 1,2,3
res = func()print(res)
输出:

参考传递还是价值传递?
注意:在Python中,每个变量名都是引用。
当我们将变量传递给函数时,将创建对对象的新引用。
Python中传递的参数与Java中传递的引用相同。
# Here x is a new reference to same list lst
def myFun(x):
x[0] = 20
# Driver Code (Note that lst is modified
# after function call.
lst = [10, 11, 12, 13, 14, 15]
myFun(lst);
print(lst)
产出:
[20, 11, 12, 13, 14, 15]
当我们传递引用并将接收到的引用更改为其他引用时,传递参数和接收参数之间的连接就会中断。
例如,考虑下面的程序。
def myFun(x):
# After below line link of x with previous
# object gets broken. A new object is assigned
# to x.
x = [20, 30, 40]
# Driver Code (Note that lst is not modified
# after function call.
lst = [10, 11, 12, 13, 14, 15]
myFun(lst);
print(lst)
产出:
[10, 11, 12, 13, 14, 15]
如果我们分配一个新值(在函数中),则另一个示例演示引用链接被破坏。
def myFun(x):
# After below line link of x with previous
# object gets broken. A new object is assigned
# to x.
x = 20
# Driver Code (Note that lst is not modified
# after function call.
x = 10
myFun(x);
print(x)
产出:
10
练习:尝试猜测以下代码的输出。
def swap(x, y):
temp = x;
x = y;
y = temp;
# Driver code
x = 2
y = 3
swap(x, y)
print(x)
print(y)
产出:
2
3
关键字传参(参数名对应)
def sue_for_peace(num1,num2): print(num1,num2) res = num1+num2 return res
print(sue_for_peace(num2=3,num1 = 4))
混合使用(先位置参数再关键字参数)
def sue_for_peace(num1,num2,num3):
print(num1,num2,num3)
res = num1+num2+num3
return res
print(sue_for_peace(3,num3=1,num2 = 2))
默认参数:
默认参数是一个参数,如果该参数的函数调用中没有提供一个值,则该参数假定为默认值。
# Python program to demonstrate
# default arguments
def myFun(x, y=50):
print("x: ", x)
print("y: ", y)
# Driver code (We call myFun() with only
# argument)
myFun(10)
产出:
('x: ', 10)
('y: ', 50)
与C++默认参数一样,函数中的任意数量的参数都可以具有默认值。
但是一旦有了默认参数,它右边的所有参数也必须有默认值。
混合使用时,先按位置传递参数,后按关键字传递参数,最后是默认值
错误示范:
def sue_for_peace(num1=1,num2,num3): # 第一行这里就错了,默认值只能再最后,编辑器报错
print(num1,num2,num3)
res = num1+num2+num3
return res
print(sue_for_peace(2,3))
def sue_for_peace(num1, num2, num3=3):
print(num1, num2, num3)
res = num1+num2+num3
return res
print(sue_for_peace(num2=2,3)) # 先位置后关键字,编辑器报错
print(sue_for_peace(1,num1=2)) # 不允许给同一个形参传两个值
关键词参数:
允许调用者用值指定参数名,就不需要记住参数的顺序。
# Python program to demonstrate Keyword Arguments
def student(firstname, lastname):
print(firstname, lastname)
# Keyword arguments
student(firstname ='Geeks', lastname ='Practice')
student(lastname ='Practice', firstname ='Geeks')
产出:
('Geeks', 'Practice')
('Geeks', 'Practice')
可变长度参数:
# Python program to illustrate
# *args for variable number of arguments
def myFun(*argv):
for arg in argv:
print (arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
产出:
Hello
Welcome
to
GeeksforGeeks
# Python program to illustrate
# *kargs for variable number of keyword arguments
def myFun(**kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
# Driver code
myFun(first ='Geeks', mid ='for', last='Geeks')
产出:
last == Geeks
mid == for
first == Geeks
匿名功能:在Python中,匿名函数意味着函数没有名称。正如我们已经知道的,def关键字用于定义普通函数,lambda关键字用于创建匿名函数。请看这关于细节。
# Python code to illustrate cube of a number
# using labmda function
cube = lambda x: x*x*x
print(cube(7))
产出:
343

本文介绍了Python函数的基础知识,包括函数的意义、定义、返回值、参数传递方式、默认参数、关键字参数、可变长度参数及匿名函数(lambda)。通过实例解析了Python中参数的值传递特性,并给出了相关代码示例。

被折叠的 条评论
为什么被折叠?



