当前位置:网站首页>【第七节 函数的作用】
【第七节 函数的作用】
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
解包
边栏推荐
- Li Kou daily question - day 18 -350 Intersection of two data Ⅱ
- 姿态估计之2D人体姿态估计 - Simple Baseline(SBL)
- idea新建项目报错org.codehaus.plexus.component.repository.exception.ComponentLookupException:
- 点投影到平面上的方法总结
- MapReduce之Word Count案例代码实现
- 姿态估计之2D人体姿态估计 - Associative Embedding: End-to-End Learning for Joint Detection and Grouping
- Kubernetes 1.24: avoid conflicts when assigning IP addresses to services
- TensorFlow实战Google深度学习框架第二版学习总结-TensorFlow入门
- C# 游戏雏形 人物地图双重移动
- VINS理論與代碼詳解4——初始化
猜你喜欢

硬件仪器的使用

The ultimate buff of smart grid - guanghetong module runs through the whole process of "generation, transmission, transformation, distribution and utilization"

TensorFlow实战Google深度学习框架第二版学习总结-TensorFlow入门

音视频处理三剑客之 AEC:回声产生原因及回声消除原理

Common QR decomposition, SVD decomposition and other matrix decomposition methods of visual slam to solve full rank and deficient rank least squares problems (analysis and summary of the most complete

Guanghetong cooperates with China Mobile, HP, MediaTek and Intel to build 5g fully connected PC pan terminal products

Software intelligence: formal rules of AAAS system metrics and grammars

反“内卷”,消息称 360 企业安全云将上线“一键强制下班”功能,电脑自动关闭办公软件

Several reasons and solutions of virtual machine Ping failure

Yuntu says that every successful business system cannot be separated from apig
随机推荐
Detailed installation steps of mysql8
Guanghetong high computing power intelligent module injects intelligence into 5g c-v2x in the trillion market
音视频处理三剑客之 AEC:回声产生原因及回声消除原理
Méthodes couramment utilisées dans uniapp - TIMESTAMP et Rich Text Analysis picture
CAP 6.1 版本发布通告
QT 基于QScrollArea的界面嵌套移动
Overview of cann interface calling process
姿态估计之2D人体姿态估计 - Associative Embedding: End-to-End Learning for Joint Detection and Grouping
terminator如何设置字体显示不同颜色
uniapp中常用到的方法(部分) - 時間戳問題及富文本解析圖片問題
Using GDB to quickly read the kernel code of PostgreSQL
姿态估计之2D人体姿态估计 - Simple Baseline(SBL)
C # game prototype character map dual movement
Opentelemetry metrics release candidate
Development of stm8s103f single chip microcomputer (1) lighting of LED lamp
Docket command
SVM and ANN of OpenCV neural network library_ Use of MLP
顺应医改,积极布局——集采背景下的高值医用耗材发展洞察2022
How the autorunner automated test tool creates a project -alltesting | Zezhong cloud test
How to open an account for agricultural futures? Are there any financial conditions?