Python 3 study notes: function (a)

Functions can be understood as a collection of code to achieve a certain function, this one of the most obvious advantage is that, if we need to repeatedly use a feature, simply use the function to write again these statements, then you can call in the program this function, eliminating the need to re-write it again these statements.

Create & call

Creating function

Create a function uses def keyword implementation,

def function_name(parameters):
do some things

function_name is the name (identifier) ​​function, using the function call; Parameters is a parameter of the function (if there are multiple, separated with a comma), if specified, the calling function is also required to pass actual data corresponding; If this parameter is not required parameter, then here it is empty, there is no need to call upon the incoming data.

call function

Call the function is very simple, just write the name of the function call in position, passing parameters to their needs,

function_name(parameters)

parameter

The main function is to receive the data, and then use the code in the function of the data processed into what we need, how will that data into internal function for their use of it? This is the function of the parameters, the parameters passed to the function is to receive the data required, then the processing of these processing parameters.

& Parameter argument

Parameter, specified when the function is defined; argument specified when the function is used.

Function is to prepare ourselves, so we know exactly what the function does, what data needs when implementing function, so when you define a function written in parentheses parameter is the parameter that specifies the function can receive, process What data. When we use this function, you need to pass the data it needs in accordance with its requirements, this data is the argument.

def summation(x, y):
z = x + y
print(str(x) + "+" + str(y) + "=" + str(z))

summation(2, 5)

In the above function, x and y is a parameter, and 2 and 5 is the argument.

Guess you like

Origin blog.51cto.com/14499640/2429759