In this article we'll look at the functions in Python, and first we need to know
function definition and implementation method,
functions in Pythonis a well-organized, reusable code snippet used to implement a single, or associated function. Functions can improve the modularity of the application, and the reuse of the code. You already know that Python provides a number of built-in functions, such as print (). But you can also create your own functions, and try to
function programming in PythonThis is called a user-defined function.
You can define a function that you want to function, and here are the simple rules:
1. The function code block begins with the DEF keyword, followed by the function identifier name and parentheses ().
2. Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
3. The first line of the function statement can optionally use the document string-for storing function descriptions.
4. Function contents start with a colon and indent.
5.return [expression] End function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
The syntax can be seen as follows:
def functionname (parameters): "Function _ Document String" function_suite return [expression]
(by default, parameter values and parameter names are matched in the order defined in the function declaration.) )
Next I'll give you a small example:
def printme (str): "Print incoming string to standard display device" print str return
(This is a simple Python function that takes a string as an incoming parameter and then prints it to the standard display device.) )
It says the function, so here's how to call the function :
Defines a function that gives the function a name, specifies the parameters contained in the function, and the code block structure. Once the basic structure of this function is complete, you can execute it from another function call or directly from the Python prompt. The following instance calls the Printme () function:
#!/usr/bin/python#-*-coding:utf-8-*-# definition function def printme (str): "Prints any incoming string" Print str; Return # Call Function Printme ("I want to invoke user-defined function!"); Printme ("Call the same function again");
The example output from the above example is as follows:
I want to invoke the user custom Function! Call the same function again