Python --函数学习2

一.函数参数和返回值

--参数:负责给函数传递一些必要的数据或者信息

    -形参(形式参数):在函数定义的时候用到的参数,没有具体值,只是一个占位符号

    -实参(实际参数):在调用函数的时候输入的值

    example:

>>> def hello(person):
...     print("{0},how are you ?".format(person))
...     print("{},i am fine".format(person))
...
>>> p="claire"
>>> hello(p)     #用P 代替了person,claire 又赋值给P
claire,how are you ?
claire,i am fine
>>>

1 def student (name,age):
2     print("his name is {0},and he is {1} years old".format(name,age))
3 a = "guni"
4 b = 33
5 student(a,b)
his name is guni,and he is 33 years old

 

--返回值:调用函数时候的一个执行结果

  - 使用return返回结果

  - 如果没有值需要返回,我们推荐使用return none 表示函数结束

  -函数一旦执行return,则函数立即结束

  -如果函数没有return关键字,则函数默认返回none。

example:

>>> hh=hello("gigi")
gigi,how are you ?
gigi,i am fine
>>> print(hh)
None
>>>

  

posted @ 2019-05-11 13:55  正霜霜儿  阅读(151)  评论(0编辑  收藏  举报