官术网_书友最值得收藏!

Python functions

Most of the time when you find yourself reusing some pieces of code, you should break up the code into a self-contained chunk as functions. This practice allows for better modularity, is easier to maintain, and allows for code reuse. Python functions are defined using the def keyword with the function name, followed by the function parameters. The body of the function consists of Python statements that are to be executed. At the end of the function, you can choose to return a value to the function caller, or by default, it will return the None object if you do not specify a return value:

    def name(parameter1, parameter2):
statements
return value

We will see a lot more examples of function in the following chapters, so here is a quick example:

    >>> def subtract(a, b):
... c = a - b
... return c
...
>>> result = subtract(10, 5)
>>> result
5
>>>
主站蜘蛛池模板: 库尔勒市| 固始县| 衡阳市| 潮安县| 东城区| 岳西县| 潼关县| 马关县| 丹寨县| 南开区| 元朗区| 景泰县| 嘉荫县| 英德市| 龙游县| 瑞安市| 丰镇市| 凉山| 紫金县| 栾城县| 万宁市| 内江市| 哈尔滨市| 定远县| 彭水| 衡山县| 庆城县| 万安县| 徐水县| 普兰县| 扶余县| 思茅市| 云霄县| 宾川县| 抚远县| 宁海县| 潜山县| 平江县| 常山县| 上犹县| 聊城市|