# 函数 > 原文: [https://pythonspot.com/functions/](https://pythonspot.com/functions/) 函数是可重用的代码,可以在程序中的任何位置调用。 函数提高了代码的可读性:使用函数而不是冗长的指令列表,使人们更容易理解代码。 最重要的是,可以重复使用或修改函数,这也提高了可测试性和可扩展性。 ## 函数定义 We use this syntax to define as function: ```py def function(parameters): instructions return value ``` **def 关键字**告诉 Python 我们有一段可重用的代码(一个函数)。 一个程序可以具有许多函数。 ## 实际例子 We can call the function using function(parameters). ```py #!/usr/bin/python def f(x): return(x*x) print(f(3)) ``` 输出: ```py 9 ``` 该函数具有一个参数 x。 返回值是函数返回的值。 并非所有函数都必须返回某些内容。 ## 参数  We can pass multiple variables: ```py #!/usr/bin/python def f(x,y): print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y)) print('x * y = ' + str(x*y)) f(3,2) ``` 输出: ```py You called f(x,y) with the value x = 3 and y = 2 x * y = 6 ``` [下载 Python 练习](https://pythonspot.com/download-python-exercises/)