函数和入参

函数有几个组成部分
1.函数名
2.函数体
3.函数调用
4.函数入参
5.返回值,函数么有写返回值,函数执行完返回的none,需要return一下,想返回什么就return一下,返回的内容就是函数执行之后的结果
return 1.返回数据 2.函数里遇到return立马结束,不再执行下面代码 像循环中的break
定义函数之前,要想好定义的函数是否有入参是否有返回
#函数不调用不会被执行
def write_file(file_name,content):
with open(file_name,'w',encoding='utf-8') as f:
f.write(content)
write_file('函数.txt','学习函数')

def read_file(file_name):
with open(file_name,encoding='utf-8') as f:
content = f.read()
return content
result = read_file('修改文件.py')
参数
#必填参数、默认值、可变参数、关键字参数 必须得按照这个顺序来
#必填参数必须写到默认值参数前面
#默认参数必须在可变参数前面
#可变参数必须在关键字参数前面

一、必填参数
def b(name,age): #必填参数,位置参数
print(name,age)
b('MLing',18)
二、默认参数,非必填,入参不传时,使用默认值
def op_file(file,content=None): #默认值参数
with open(file,'a+',encoding='utf-8') as f:
if content:
f.write(str(content))
else:
f.seek(0)
result = f.read()
return result
open('tihuan.txt')
三、可变参数
#1、这个是不是必须传的 非必传
#2、限制不限制参数的个数 不限制
#3、返回的是什么 元组
def send_sms(*args):#可变参数
print(args)
四、关键字参数
#1、这个是不是必须传的 非必传
#2、限制不限制参数的个数 不限制
#3、返回的是什么 字典
def info(**kwarge):
print(kwarge)
info()
info(name='MLing',age=12)



 


posted @ 2020-05-08 16:00  MLing  阅读(1497)  评论(0编辑  收藏  举报