function is one of the core contents of Python programming. In this article, we mainly introduce the following
Python custom FunctionsThe concept and
python function return valueRelevant knowledge points.
Python custom FunctionsWhat is it? What it does, how to define a function, and how to call
python function return value。
What is a python custom function
Functions are well-organized, reusable pieces of code that are 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, which are called user-defined functions.
So how to customize the python function
1. You can define a function that you want to function, the following is a simple rule:
2. The function code block begins with the DEF keyword, followed by the function identifier name and parentheses ().
3. Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
4. The first line of the function statement can optionally use the document string-for storing function descriptions.
5. Function contents start with a colon and indent.
6.return [expression] End function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
About the python custom function syntax :
def functionname (parameters): "Function _ Document String" Function_suitereturn [expression]
By default, parameter values and parameter names are matched in the order defined in the function declaration.
Instance
def printme (str): "Print incoming string to standard display device" Print Strreturn
So how do I write a python function return value?
Return statement:
Return statement [expression] exits the function, optionally returning an expression to the caller. A return statement without a parameter value returns none. The previous example does not demonstrate how to return a value, and the following example tells you how to do it:
#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def sum (arg1, arg2): # Returns the sum of 2 parameters. " Total = arg1 + arg2 print "in function:" ; # Call the Sum function total = SUM (10, 20);
The result of the above example output:
Inside function: 30