Function
The term function is derived from mathematics, but the concept of "function" in programming is very different from the function in mathematics, and the function in programming has many different names in English. In basic it is called subroutine (sub-process or subroutine), in Pascal is called procedure (process) and function, in C only function, in Java is called method.
Defined
A function is to encapsulate a set of statements by a name (function name), and to execute the function, simply call the name of its functions.
Characteristics
3. Make the program easier to maintain
Grammar
def Sayhi ()# use def to define a function followed by the function name (). print ("hello")# action contents in function, printing a Hellosayhi () # function name + parentheses to implement function invocation # Print Result: Hello
Parameters of the function
# define variable a # define variable B def Calc (x, y)#x , y parameter res x** yreturn res print (A, b)#A/b argument # printing result: 1024x768
Formal parameter: The memory unit is allocated only when it is called, and the allocated memory unit is freed immediately after the call ends. Therefore, the formal parameter is only valid inside the function. The parameter variable can no longer be used after the function call finishes returning the keynote function.
Note: 1, the parameter is a local variable, scope only inside the function, the external variable by default cannot be modified inside the function, can only be referenced.
2, if you need to modify the global variables inside the function need to use "global", but strongly do not recommend this use.
3, inside the function can modify the list, dictionary, collection, instance.
Arguments: Can be constants, variables, expressions, functions, and so on, regardless of the type of arguments, they must have a definite value when making a function call to pass these values to the parameter. Therefore, the parameters should be pre-assigned, input and so on to obtain a definite value.
Note: There are parameters that determine the value, and all data types can be passed as arguments to the function.
Cond..........
Python Learning notes 5-python functions