函数:带名字的一个代码块,用于完成具体的工作 ,要执行函数定义的特定的任务,可调用函数
格式:def 函数名():
def namefunc(username): ## username 是形参
print("Your friend is "+username.title()+"")
namefunc("Mike")
Your friend is Mike ## Mike 是实参
传递实参:
①位置实参(顺序位置不能乱)
def mypet(animal,name):
print("\nI have a "+animal+"")
print("My "+animal+"'s name is "+name+" .")
mypet("dog","duoduo") ## 顺序很重要
I have a dog
My dog's name is duoduo .
②关键字实参(不关心位置顺序)
def mypet(animal,name):
print("\nI have a "+animal+"")
print("My "+animal+"'s name is "+name+" .")
#mypet("dog","duoduo")
mypet(animal="dog",name="duoduo") ## 实参顺序/位置可以改变
mypet(name="duoduo",animal="dog") ## 只要关键字能对应
I have a dog
My dog's name is duoduo .
I have a dog
My dog's name is duoduo .
默认值:(形参可以先指定默认值)
def mypet(name,animal='dog'): ## 只能对后面的形参设默认值
print("\nI have a "+animal+"")
print("My "+animal+"'s name is "+name+" .")
mypet("duoduooo") ##两种格式
mypet(name="duoduo")
I have a dog
My dog's name is duoduooo .
I have a dog
My dog's name is duoduo .
返回值;return将值返回到调用函数的代码行
def mypet(animal,anima2):
out=("\nI have two dogs,its name are "+animal+" and "+anima2+",respectly.")
return print(out)
mypet("duoduooo","didi")
I have two dogs,its name are duoduooo and didi,respectly.
def dogname(A,B,C):
name=A+' '+B+' '+C
return name.title()
dogs=dogname('duoduo','didi','haha')
print(dogs)
Duoduo Didi Haha
返回字典:
今天就在这里吧 学不进去了。。。