Function is a well organized, reusable code segment used to implement single or associated functions.
Unlike the definition of functions in mathematics, functions can be called subroutines in computers.
Function can improve the modularity of application and reuse of code. You already know that Python provides many built-in functions, such as print (). But you can create functions yourself, which is called user-defined functions.
Define a function
You can define a function that you want to function. Here are the simple rules:
- Function code blockdef The keyword begins with the function identifier name and parenthesis.()。
- Any incoming parameters and arguments must be placed in the middle of parentheses. The parentheses can be used to define the parameters.
- The first line statement of a function can selectively use the document string – for storing function descriptions.
- The content of the function begins with a colon and is indented.
- return [Expression] End the function, selectively returning a value to the caller. The return without an expression is equivalent to returning None.
The grammar of a function
def functionname( parameters ): "Function document string" function_suite return [expression]
By default, the parameter values and parameter names are matched in the order defined in the function declaration.