Home >Backend Development >Python Tutorial >What are the keywords used to define functions in python

What are the keywords used to define functions in python

silencement
silencementOriginal
2019-06-20 13:20:5646384browse

What are the keywords used to define functions in python

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, the parameter value and parameter names are matched in the order defined in the function declaration.

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 What are the keywords used to define functions in python. 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