当前位置:网站首页>【第七节 函数的作用】
【第七节 函数的作用】
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
解包
边栏推荐
- SQL语言
- Apple style Chinese: it seems to express the meaning clearly, and the Apple style propaganda copy that you don't fully understand
- QT 基于QScrollArea的界面嵌套移动
- 统一认证中心 Oauth2 认证坑
- 如何写一个全局的 Notice 组件?
- How to write a global notice component?
- 22. Generate Parentheses
- 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
- ORB_SLAM2视觉惯性紧耦合定位技术路线与代码详解1——IMU流型预积分
- 影刀RPA学习和遇见excel部分问题解决方式
猜你喜欢

Vins Theory and Code detail 4 - Initialization

VINS理論與代碼詳解4——初始化

The product design software figma cannot be used. What software with similar functions is available in China

Sorting and paging

Save a window with a specific size, resolution, or background color

Beginner pytorch step pit
![[MySQL basics]](/img/fc/48e9c6b739e29472a7a103e47663af.png)
[MySQL basics]

港大、英伟达 | Factuality Enhanced Language Models for Open-Ended Text Generation(用于开放式文本生成的事实性增强语言模型)

Development of stm8s103f single chip microcomputer (1) lighting of LED lamp

SQL language
随机推荐
Self recommendation - in depth understanding of the rust Standard Library Kernel
docket命令
Hutool Usage Summary (VIP collection version)
The ultimate buff of smart grid - guanghetong module runs through the whole process of "generation, transmission, transformation, distribution and utilization"
MySQL8安装详细步骤
苹果式中文:似乎表达清楚意思了,懂了没完全懂的苹果式宣传文案
视觉SLAM常见的QR分解SVD分解等矩阵分解方式求解满秩和亏秩最小二乘问题(最全的方法分析总结)
Kubernetes 1.24: preventing unauthorized volume mode switching
22. Generate Parentheses
The product design software figma cannot be used. What software with similar functions is available in China
统一认证中心 Oauth2 认证坑
VINS理論與代碼詳解4——初始化
C# 游戏雏形 人物地图双重移动
What has guanghetong done in the three years of 5g business from "seeding in the first generation" to "flower on the ground"?
姿态估计之2D人体姿态估计 - Numerical Coordinate Regression with Convolutional Neural Networks(DSNT)
What are the top ten futures companies with low handling fees? Is it safe?
Guanghetong high computing power intelligent module injects intelligence into 5g c-v2x in the trillion market
How to write a global notice component?
Google X开源抓取机械臂,无需人工标注就能一眼找到目标零件[转]
自动化运维必备的工具-Shell脚本介绍