- Mastering Python Networking
- Eric Chou
- 154字
- 2021-07-02 21:42:33
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
>>>
推薦閱讀
- 深入核心的敏捷開發:ThoughtWorks五大關鍵實踐
- Modular Programming with Python
- Java EE 6 企業級應用開發教程
- Developing Mobile Web ArcGIS Applications
- WSO2 Developer’s Guide
- Learning ASP.NET Core 2.0
- 基于差分進化的優化方法及應用
- Learning Neo4j 3.x(Second Edition)
- Banana Pi Cookbook
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- Functional Kotlin
- Learning Python by Building Games
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- Active Directory with PowerShell
- 深入分布式緩存:從原理到實踐