python--函数的返回值、函数的参数

函数的返回值

特性:

  • 减少重复代码
  • 使程序变的可扩展
  • 使程序变得易维护

函数名:命名规则和变量一样

函数的返回值:

return, 函数执行完毕. 不会执行后面逻辑

  • 如果函数中不写return返回None
  • 只写return 返回None
  • return 返回值. 返回一个值
  •  return 值1, 值2,... 返回多个值. 调用方接收到的是元组

遇到return结束函数里的语句,下面的不会执行

没有return,返回None

def foo():
    print('foo')


foo  # 表示是函数
foo()  # 表示执行foo函数
def sayhi():  # 函数名
    print("Hello, I'm nobody!")


sayhi()  # 调用函数

结果:

Hello, I'm nobody!

之前写的乘法是这样的

a, b = 5, 8
c = a ** b
print(c)

改成函数之后是这样的

a, b = 5, 8

# 改成用函数写
def calc(x, y):
    res = x ** y
    return res  # 返回函数执行结果


c = calc(a, b)  # 结果赋值给c变量
print(c)

函数和过程

函数有return 过程没有return

# 函数
def func1():
    print('in the func1')
    return 0


# 过程
def func2():
    print('in the func2')


x = func1()
y = func2()
print('from func1 return is %s' % x)
print('from func2 return is %s' % y)

结果:

in the func1
in the func2
from func1 return is 0
from func2 return is None
import time  # 导入time模块


def logger():  # 定义了函数名为logger
    time_format = '%Y-%m-%d %X'  # 时间格式,Y年m月d日X时分秒

    time_current = time.strftime(time_format)  # time.strftime引用上面的时间格式
    with open('ccc.txt', 'a+') as f:
        f.write('%s end action
' % time_current)  # 将日期时间和end action写入到f里


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


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


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


aaa()
bbb()
ccc()

结果:

in the test1
in the test2
in the test3
ccc.txt里写入
2020-06-15 22:17:37 end action
2020-06-15 22:17:37 end action
2020-06-15 22:17:37 end action
def test():
    print('in the test')
    return 0
    print('end test')


test()

结果:

in the test
# 遇到return结束函数里的语句,下面的不会执行
def test():
    print('in the test')
    return 0


x = test()
print(x)

结果:

in the test
0

x=test()先执行函数里的内容,最后将函数里的返回值赋给x

def test():
    print('in the test')
    return 1


print(test())  # 相当于x=test()  print(x)

结果:

in the test
1

没有 return 返回 None

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


x = test()
print(x)

结果:

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


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


def test3():
    print('in the test3')
    return 1, 'hello', {'name': 'zouzou'}
    return test2


x = test1()
y = test2()
z = test3()
print(x)
print(y)
print(z)

结果:

in the test1
in the test2
in the test3
None
0
(1, 'hello', {'name': 'zouzou'})

先执行x,y,z,在打印他们的返回值

函数的参数

  • 关键字参数不能写在位置参数前面(定义函数时)
  • 参数组不能在位置参数和关键字参数前面
  • 默认参数放在最后面

 

形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量

实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值

def test(x, y):  # x,y叫形参
    print(x)
    print(y)


test(2, 4)  # 位置参数,实参
def test(x, y):  # x,y叫形参
    print(x)
    print(y)


test(x=2, y=3)  # 关键字参数
def test(x, y=3):  # y为默认参数
    print(x)
    print(y)


test(2)

参数组

*args接收的位置参数,转换成元组的形式,没有传参返回空元组

**kwargs接收的关键字参数,转换成字典的形式,没有传参返回空字典

def test(**kwargs):  # 参数组,接收的参数不确定时用**
    print(kwargs)


test()

结果:

{}
def test(*args):  # 参数组,接收的参数不确定时用*
    print(args)


test()

结果:

()
def test(x, *args):
    print(x)
    print(args)  # 打印的是一个元组


test(2, 4, 6, 8)
test(*[6, 8, 9, 45, 5])  # args=tuple[6,8,9,45,5]

结果:

2
(4, 6, 8)
6
(8, 9, 45, 5)
def test(**kwargs):  # 参数组,接收的参数不确定时用*  
    print(kwargs)  # 打印的是一个字典
    print(kwargs['name'])
    print(kwargs['age'])
    print(kwargs['job'])


test(name='zou', age=18, job='IT')  # 接收的是关键字参数

结果:

{'name': 'zou', 'age': 18, 'job': 'IT'}
zou
18
IT
原文地址:https://www.cnblogs.com/zouzou-busy/p/12995115.html