Python正课30 —— 函数的基本使用

本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12511949.html

一:什么是函数

函数就相当于具备某一功能的工具

函数的使用必须遵循一个原则: 先定义,后调用。

二:为什么要用函数

1.组织结构不清晰,可读性差

2.代码冗余

3.可维护性、扩展性差

三:如何用函数

1.先定义

定义的语法

def 函数名(参数1,参数2,...):
    """文档描述"""
    函数体
    return 值

形式1:无参函数

def func():		# 不需要传递参数,
    print('哈哈哈')
    print('哈哈哈')
    print('哈哈哈')

定义函数发生的事情

# 1、申请内存空间保存函数体代码
# 2、将上述内存地址绑定函数名
# 3、定义函数不会执行函数体代码,但是会检测函数体语法

调用函数发生的事情

# 1、通过函数名找到函数的内存地址
# 2、然后加口号就是在触发函数体代码的执行
# print(func)
# func()

示范1:

def bar(): # bar=函数的内存地址
    print('from bar')

def foo():
    bar()	# 调用bar()函数
    print('from foo')
    
foo()

输出:
from bar
from foo

示范2:

def foo():
    bar()
    print('from foo')
    
def bar():  # bar=函数的内存地址
    print('from bar')
    
foo()

输出:
from bar
from foo

示范3:

def foo():
    bar()
    print('from foo')

foo()

def bar():  # bar=函数的内存地址
    print('from bar')
   
输出:
NameError: name 'bar' is not defined

形式2:有参函数

def func(x,y): # x=1  y=2
    print(x,y)
    
func(1,2)	# 用户指定参数1,2

输出:
1 2

形式3:空函数,函数体代码为pass

def func(x, y):
    pass
    
func(1,2)

输出:

三种定义方式各用在何处?

① 无参函数的应用场景

def interactive():
    name=input('username>>: ')
    age=input('age>>: ')
    gender=input('gender>>: ')
    msg='名字:{} 年龄:{} 性别'.format(name,age,gender)
    print(msg)

interactive()

② 有参函数的应用场景

def add(x,y): # 参数-》原材料
    res=x + y
    return res # 返回值-》产品

res=add(20,30)
print(res)

③ 空函数的应用场景

def auth_user():
    """user authentication function"""
    pass

def download_file():
    """download file function"""
    pass

def upload_file():
    """upload file function"""
    pass

def ls():
    """list contents function"""
    pass

def cd():
    """change directory"""
    pass

2.后调用

① 语句的形式:只加括号调用函数

interactive()
add(1,2)

② 表达式形式:

def add(x,y): # 参数-》原材料
    res=x + y
    return res # 返回值-》产品
赋值表达式
res=add(1,2)
print(res)
数学表达式
res=add(1,2)*10
print(res)

③ 函数调用可以当做参数

def add(x,y): # 参数-》原材料
    res=x + y
    return res # 返回值-》产品

result=add(add(1,2),10)
print(result)

3.返回值

return是函数结束的标志,即函数体代码一旦运行到return会立刻

终止函数的运行,并且会将return后的值当做本次运行的结果返回:

① 返回None:函数体内没有return

def func():
    return
res = func()
print(res,type(res))

输出:
None <class 'NoneType'>

② 返回一个值:return 值

def func():
    return 10
res = func()
print(res,type(res))

输出:
10 <class 'int'>
def func():
    return 'abc'
res = func()
print(res,type(res))

输出:
abc <class 'str'>

③ 返回多个值:用逗号分隔开多个值,会被return返回成元组

def func():
    return 1,2,3
res = func()
print(res,type(res))

输出:
(1, 2, 3) <class 'tuple'>
posted @ 2020-03-17 17:13  轻描丨淡写  阅读(312)  评论(0编辑  收藏  举报