当前位置:网站首页>【第七节 函数的作用】
【第七节 函数的作用】
2022-06-10 15:32:00 【qq_24409999】
什么是函数
函数是我们在编程过程当中遇到一个非常重要的概念。 封装。
1,函数的概念。 数学函数
y = x + 1
f(x) = x + 1
初中的函数学完以后,
函数的作用
你传入一个自变量,会得到一个另外的变量,这个变量会根据自变量的变化而变化。
内置函数的作用
#函数的调用,print的作用是将数据显示在(屏幕)上,打印数据的意思
print("hello world")
#input 的作用:获取用户输入的内容,输入的参数是提示语
a = input("用户名:")
print(a)
#id 获取数据内存地址
print(id(a))
#每个函数都是有特定作用的:存储了一段程序
#如果函数名称不能体现函数的作用,不能说明函数是做什么用的?
#注释解释发生了什么事情。 函数的注释:函数的注解, 文档字符串, docstring
def func(a,b):
""""本函数是为了计算 2 个 数字相乘。"""
print(a*b)
函数的定义
- 怎么命名
- 具体语法
""" 什么是函数 f(x) = x+2 在python中,遇到冒号要缩进 def 函数的名称(参数): (缩进)函数体 """
#函数的定义
#TODO:不要在一个文件当中定义两个重名函数
def study_00(wait_time):
print("study_00")
print(f"提前多久到教室?{
wait_time}分钟")
print("到课堂签到")
print("听歌")
print("听课")
def study_00():
print("another")
#函数如果没有变量怎么办?
#如果函数每次运行都是一样的,不需要参数,括号当中可以没有参数
def study_01():
print("study_01")
print(f"提前5分钟到教室")
print("到课堂签到")
print("听歌")
print("听课")
#如果有多个部分发生变化
def study_02(wait_time,sign_code):
print("study_02")
print(f"提前多久到教室?{
wait_time}分钟")
print(f"到课堂签到,签到码是{
sign_code}")
print("听歌")
print("听课")
#使用函数的过程叫做调用函数
#没调用函数的时候,会把wait_time 赋值 12 ===》 wait_time =12
#定义和调用:定义是表示事先把需要运行的逻辑存好,存到函数当中,提取变化的部分
#调用就是使用函数
#TODO:只有当调用函数以后,函数里面的代码或逻辑才会被执行;函数的定义的时候,函数体当中的代码是不会执行
# study_00(12)
# study_01()
# study_02(7,4013)
#在study_02 当中,wait_time 和 sign_code 这两个东西是函数的参数,是变量名称,形式参数
# 7 和 4013 是函数的参数,是值,实际参数
#形式参数是在函数的定义出现的,变量名,是自己定义的
#实际参数是在函数的调用过程出现的。 实际参数是将要赋给变量名(形式参数)值
#TODO:形式参数和实际参数是要配对的,一对CP
#TODO:在函数调用的时候,请你一定要检查好函数的参数个数或者(对数)
# study_02(9,4013)
# study_00()#TypeError
# study_01(8)
study_00()
######### 函数命名 ###################
#函数名称是一个标识符,字母数字下划线
#函数名遵循蛇形命名,又叫下划线命名
#函数名称要见名知意
def abc():
pass
#函数命名的重要性
#函数名称会告诉读代码的人,我这个函数的作用是什么?
#我们之前学的内置函数分别代表什么作用
#函数如果通过函数名称还是理解不了他的作用,怎么办?
def add(a,b):
print(a+b)
add(3,4)
函数的返回值 return
"""函数的返回值"""
def add(a,b):
"""两数相加"""
c = a+b
print(c)
return c
#第一步,计算 2 个数得到相加的值
#第二步: 通过得到的相加的值 *6
#先把相加的数据要存起来
#函数体执行以后得到的结果必须通过 return 返回
#return 的作用:是为了得到函数计算以后的结果
#函数以后的结果,return 的值, 是给函数的调用方使用的
# result = add(4,5) # TODO:===》 c = a+b = 4+5 ===>result =c
# print(result*6)
# resutl = add(a,b)
#print(c*6)
def add(a,b):
"""两数相加"""
c = a+b
print("函数没有执行完")
return a-b
print("函数执行完啦")
#print return
#print() 的作用:是显示在屏幕上,和函数有关系没?? print 对返回值有影响吗? --都没有
#TODO:函数的返回值有且只能由 return 决定 ,yield
res = add(4,5)
print(res*6)
#TODO: return,函数的执行: 函数遇到return 就终止执行
非常多参数
- 形式参数
- 实际参数
- 位置参数
- 关键字参数
"""函数的参数 位置参数 """
def add(a,b):
c = a+b
return c
# res = add(6,7) # add(6,7) ==>18
# print(res)
#报错,没有成 CP
# res = add(6,7,1)
#CP 是按位置, 按顺序来配对的
#形式和实际参数的这种位置关系就叫位置参数
res = add("xiaoli" ,"wang")
print(res)
#关键字参数,函数调用的时候通过给实际参数贴标记,标记是形式参数的变量名
#通过关键字参数可以不按照顺序来配对
#关键字参数的作用:当参数很多的时候
res = add(b = "xiaoli",a = "wang")
print(res)
#TODO:关键字参数要放到位置参数的后面
# res = add(b = "xiaoli","wang")
res = add("wang",b = "xiaoli")
- 默认参数
""" 默认参数 是在函数定义的时候,给形式参数一个默认的值 默认参数有什么用? 可以让我们少写实际参数 """
def add(a,b=9):
return a+b
# print(add(3,4)) #7
# print(add(a=3,b =4))
# print(add(8))
# print(add(b =7))
结果
Traceback (most recent call last):
File "E:/ltt/02pythonProjet/pythonProject3/d5.py", line 13, in <module>
print(add(b =7))
TypeError: add() missing 1 required positional argument: 'a'
7
7
17
- 不定长参数
""" 不定长参数。* ** 当在一个形式参数的前面加*号,他会收集所有剩下没有配对的实际参数位置参数 默认参数也要放到位置参数后面 """
def add(a,*b,c = 6):
print(f"a为:{
a}")
print(f"b为:{
b}")
# add(3,4)
add(3,4,5)
add(2,4,5,6,7,8,9,c=3)
结果
a为:3
b为:(4, 5)
""" ** 只能收集关键字参数 不定长参数的作用:是在函数定义的时候,我不知道有多少个实际参数会传入 """
def add(a,**b):
print(a)
print(b)
add(3,d = 4,e =5)
print("hello","world","excel")
3
{
'd': 4, 'e': 5}
hello world excel
""" ** 只能收集关键字参数 不定长参数的作用:是在函数定义的时候,我不知道有多少个实际参数会传入 """
def add(a,*c,**b):
print(a)
print(c)
print(b)
res = add(3,4,5,d = 4,e =5)
print(res)#None 什么都没有
# print("hello","world","excel")
结果
3
(4, 5)
{
'd': 4, 'e': 5}
None
解包
边栏推荐
- ADA logics:cri-o overall safety audit project
- Solution to some problems of shadow knife RPA learning and meeting Excel
- 无线通信模组如何助力智能无人机打造“空中物联网”?
- How to improve document management
- ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 0 - overall framework and theoretical basic knowledge
- MapReduce案例之排序
- TensorFlow实战Google深度学习框架第二版学习总结-TensorFlow安装
- How to write a global notice component?
- Explore the secrets behind the open source data visualization development platform flyfish!
- 凸函数的Hessian矩阵与高斯牛顿下降法增量矩阵半正定性的理解
猜你喜欢

从“初代播种”到“落地生花”,广和通在5G商用三年间做了什么?

MySQL8安装详细步骤

Unified certification center oauth2 certification pit

RK3308 按键Key与LED灯

Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation

Hessian matrix of convex function and Gauss Newton descent method

视觉SLAM常见的QR分解SVD分解等矩阵分解方式求解满秩和亏秩最小二乘问题(最全的方法分析总结)

姿态估计之2D人体姿态估计 - SimDR: Is 2D Heatmap Representation Even Necessary for Human Pose Estimation?

MapReduce之Word Count案例代码实现

VINS理论与代码详解4——初始化
随机推荐
[high code file format API] Shanghai daoning provides you with the file format API set Aspose, which can create, convert and operate more than 100 file formats in just a few lines of code
MySQL8安装详细步骤
Anti "internal roll", it is said that 360 enterprise security cloud will launch the "one click forced off duty" function, and the computer will automatically close the office software
json.load(s)与json.dump(s)
Vins Theory and Code detail 4 - Initialization
影刀RPA学习和遇见excel部分问题解决方式
Jaeger introduces native support for opentelemetry
SVM and ANN of OpenCV neural network library_ Use of MLP
Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
[rust daily] first release of mnemos on April 20, 2022
姿态估计之2D人体姿态估计 - (OpenPose) Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields
【無標題】
Google X开源抓取机械臂,无需人工标注就能一眼找到目标零件[转]
姿态估计之2D人体姿态估计 - Simple Baseline(SBL)
QT 基于QScrollArea的界面嵌套移动
如何写一个全局的 Notice 组件?
VINS理論與代碼詳解4——初始化
Apple style Chinese: it seems to express the meaning clearly, and the Apple style propaganda copy that you don't fully understand
2290. Minimum Obstacle Removal to Reach Corner
[sans titre]