Home  >  Article  >  Backend Development  >  python define function

python define function

silencement
silencementOriginal
2019-06-05 17:53:412990browse

python define function

Python functions

Function is an organized, reusable code segment used to implement a single or related function.

Function can improve the modularity of the application and the reuse rate of the code. You already know that Python provides many built-in functions, such as print(). But you can also create your own functions, which are called user-defined functions.

Define a function

You can define a function with the functions you want. The following are simple rules:

1. The function code block begins with It starts with the def keyword, followed by the function identifier name and parentheses ().

2. Any incoming parameters and independent variables must be placed between parentheses. Parameters can be defined between parentheses.

3. The first line of the function statement can optionally use a documentation string - used to store function descriptions.

4. The function content starts with a colon and is indented.

5.return [expression] End the function and optionally return a value to the caller. Return without an expression is equivalent to returning None.

Syntax

Python defines functions using the def keyword. The general format is as follows:

def 函数名(参数列表):
    函数体

By default, parameter values ​​and parameter names are by function The order defined in the declaration matches.

Example

Let us use a function to output "Hello World!":

>>>def hello() :
   print("Hello World!")

Output result

> ;>> hello()

Hello World!

The above is the detailed content of python define function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn