python function declaration and call (18)

    Refers to a function code snippets can be called repeatedly, such as we come into contact with the previous article type () / len () and the like are functions that are built-in functions of the python, the python bottom package used to implement certain functions.

copy and paste

 

I. Definition Function

     In Python, to be used to define a function defstatement sequentially write the function name, parentheses, brackets and colon parameters: and then, write the function block body in the retracted, the return value returned by the function return statement; if not return statement, default return None:

    For example: a function of the output write 'hello world'

 

II. The calling function

    When py file, the code line by line execution, if they are defined functions, the compiler will automatically skip the code after execution of the function, if you want to call a function to be called directly.

    Note: The function must be declared before calling. python built-in functions such as: print / type functions, and so has been declared inside the python compiler and defined, we just call, you do not need to care about how specific internal implementation. Sample code is as follows:

    Output:

    Code Analysis: code execution to the 15th row, the compiler found that this is a function declaration, the compiler does not perform, it will automatically jump to the end of the function, line 20, line 20 compiler found is calling custom_print () function, directly into custom_print () line of code in the function of 16/17/18 execute the function until the end of the function, which is the entire run.

 

III. Functions of mass participation

    Function can be transferred through the external parameters, such as: print () function, and can be passed directly to the print string string; may not pass parameters, such as the above custom_print function, according to their needs.

    When the function is called with the parameter definitions declared parameter; parameter called external call transfer function arguments; have both types of function parameters:

    1. General Parameters

    Conventionally, there are several default function parameter, the number of arguments passed requires external call, the following sample code:

    Output:

 

    2. The default parameters

    Function parameters, in addition to conventional parameters there are default parameters, i.e., the default parameter has a default value, if no external function call parameters passed to the default parameters, the parameter values ​​directly from the default parameter; if external call to default parameter passed parameters, then the parameter value should be equal to the parameter of the external transfer function with default parameters also called default function, the following sample code:

    Output:

    note:

    1. The default parameter has a default value, if no external pass parameters to the default parameters, then the default value directly; otherwise, a value equal to the external parameters passed

    2. The default parameters must be written at the end of the function parameter

 

    3. The variable length parameter

    In addition to the two above, function parameters there is a variable length parameters, namely: the function of the parameter length / type is not fixed, you may listen to a little Mongolian, we leave this issue to the next article  python variable length parameter function * argc, ** kargcs  explain, being not too much to explain.

 

IV. Function return value return

    Function's return value is optional, according to their own needs. If the function does not return a return value, the default will return None, that is a null value. And different False, it does not represent 0, does not mean an empty string, and represents no value, it is null.

 

V. Key summary

    1. The function must be declared before the call, otherwise it will error.

    2. Note the wording parameter default parameters

    3. function without the use of return, default return None

 

you may also like:

    1.pycharm development template configuration / set the font size

    2.python list comprehensions

    3.python dictionary derivations

    4.python function variable length parameters * argc, ** kargcs

 

    Reproduced please specify: ape say Python  »  Python function declaration and call

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/11846261.html