mirror of
https://github.com/Estom/notes.git
synced 2026-02-13 07:16:13 +08:00
66 lines
1.6 KiB
Markdown
66 lines
1.6 KiB
Markdown
**\>函数**
|
||
|
||
\>\>函数定义:完成特定功能的一个语句组,这组语句可以作为一个单位使用,并且给他求个名字,通过函数名执行
|
||
|
||
\>\>语法:
|
||
|
||
**[python]** [view
|
||
plain](http://blog.csdn.net/estom_yin/article/details/51873452)
|
||
[copy](http://blog.csdn.net/estom_yin/article/details/51873452)
|
||
|
||
1. \<span style="font-size:18px;"\> **def** print\_sum(start, stop):
|
||
|
||
2. """ to calculate the sum from start to stop """
|
||
|
||
3. result = 0
|
||
|
||
4. **for** i **in** range(star, stop + 1):
|
||
|
||
5. result += i
|
||
|
||
6. **print** 'sum is', result\</span\>
|
||
|
||
> 第一行定义了一个函数,关键字 def ,函数名 print_sum,函数参数start, stop
|
||
|
||
\>\>函数调用
|
||
|
||
**[python]** [view
|
||
plain](http://blog.csdn.net/estom_yin/article/details/51873452)
|
||
[copy](http://blog.csdn.net/estom_yin/article/details/51873452)
|
||
|
||
1. \<span style="font-size:18px;"\>print\_sum(a, b)\</span\>
|
||
|
||
\>\>参数说明
|
||
|
||
> 形式参数和实际参数
|
||
|
||
> 缺省参数,定义实参时,给参数赋值
|
||
|
||
\>\>返回值
|
||
|
||
> return 语句会终止当前的函数执行
|
||
|
||
> 返回一个变量
|
||
|
||
\>\>变量的作用域
|
||
|
||
> 局部变量:只能在程序特定部分起作用
|
||
|
||
> 全局变量:能在程序的全局起作用
|
||
|
||
> 名称的屏蔽作用
|
||
|
||
> global x 表明x是一个全局变量声明
|
||
|
||
> pass 语句可作为一个占位符,用于表示此处省略的大量语句
|
||
|
||
\>\>函数的作用:
|
||
|
||
> 可以将一个打的问题,分解成一个小的函数,有利于解决问题
|
||
|
||
> 可以进行信息的封装和隐藏
|
||
|
||
**\>琐碎的函数知识总结**
|
||
|
||
\>\>if month in(1, 3, 5, 7, 8, 9, 11, 12):函数in可以表示一个选择范围
|