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.
I. Definition Function
In Python, to be used to define a function def
statement 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:
def functionname( parameters ): "函数说明" function_suite return [expression]
For example: a function of the output write 'hello world'
def cusom_print(): print("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:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解忧 @Blog(个人博客地址): shuopython.com @WeChat Official Account(微信公众号):猿说python @Github:www.github.com @File:python_function.py @Time:2019/10/3 10:48 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ def custom_print(): print("hello world") print("hello world") print("hello world") custom_print()
Output:
hello world
hello world
hello world
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:
def cusom_print1(x): print("cusom_print1 : x={}".format(x)) def cusom_print2(x,y): print("cusom_print2 : x={}".format(x)) print("cusom_print2 : y={}".format(y)) def cusom_print3(x,y,z): print("cusom_print3 : x={}".format(x)) print("cusom_print3 : y={}".format(y)) print("cusom_print3 : z={}".format(z)) cusom_print1(1) cusom_print2(1,2) cusom_print3(1,2,3)
Output:
cusom_print1 : x=1 cusom_print2 : x=1 cusom_print2 : y=2 cusom_print3 : x=1 cusom_print3 : y=2 cusom_print3 : z=3
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:
def cusom_print4(x,y=2,z=3): # x=2,z=3 缺省参数 print("cusom_print4 : x={}".format(x)) print("cusom_print4 : y={}".format(y)) print("cusom_print4 : z={}".format(z)) print("***"*20) cusom_print4(1) cusom_print4(1,4) cusom_print4(1,4,3)
Output:
cusom_print4 : x=1 cusom_print4 : y=2 cusom_print4 : z=3 ************************************************************ cusom_print4 : x=1 cusom_print4 : y=4 cusom_print4 : z=3 ************************************************************ cusom_print4 : x=1 cusom_print4 : y=4 cusom_print4 : z=3 ************************************************************
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
# 错误写法 def cusom_print4(x,y=2,z): print("cusom_print4 : x={}".format(x))
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
3.python dictionary derivations
4.python function variable length parameters * argc, ** kargcs
Reproduced please specify: ape say Python » Python function declaration and call