The definition of a function in mathematics: Generally in a process of change, if there are two variables x and Y and for each definite value of x, Y has a unique value corresponding to it, then we will make X an argument to make Y the dependent variable y is the function of X. The value range of the argument x is called the definition field of the function.
y = 2*x
The function in Python is a kind of logical structure and process of becoming a method
Methods of function definition in Python:
1 def Test (x): 2 " The function Deinitions " 3 x + = 14 return x
def defines a keyword for a function
Test: Name of function
() can be defined in the parameter
The comment comment is an integral part of the function * * * *
x + = 1: Generic code block or program processing logic
Return defines the returned value
Debug run: Can be with parameters or without
Function name ()
Advantages of the function:
1. Code Reuse
2. Maintain consistency and ease of maintenance
3. Extensibility
Functions and procedures in Python:
process Definition: The process is simple and special no return value
That is, a function that has no return value
function return value:
return value =0 return none
Return value =1 returns an object
return value >= 1 returns a tuple
function parameters
1 parameters are allocated memory units only when they are called to end the release unit
2 arguments in a function call they must be deterministic values that are passed to the parameter and therefore pre-assigned
1 def Test (x, Y, z):2 print(x, Y, z)
Position parameters need one by one corresponding missing one not more than one can not
Test (+/-)
Keyword parameter does not need one by one correspondence missing one no more no
Test (x=1,z=2,y=3)
Positional parameters must be on the left side of the keyword argument
Test (1,3,y=2) error
Test (1,3,z=2) True
Parameter Group (* * Dictionary * list
If there is no add *, the whole list is treated as a whole
Plus * is passed as multiple parameters after traversal
Test (1,y=2,z=3,z=3) A parameter cannot pass two values
1 def Test (*args, *Kwargs):2 print(args)3 Print (Kwargs)
Global variables and local variables
Global variables generally define global available global variables at the top of the file all uppercase letters
Local variables defined in subroutines use local variable name lowercase only inside subroutine
Global variables defined in sub-program keywords
If the contents of the function do not have the Global keyword first read local variables can only read global variables cannot be re-assigned to global variables
If the function has the Global keyword variable in essence, the function can read the assignable value
function = = variable????
1 def foo (): 2 Print ('this isfoo') 3 Bar ()4def Bar ():5 Print ('This isbar') 6 7 foo ()
Functions loaded first will not be error-
1 def foo (): 2 Print ('this isfoo') 3 Bar ()4foo ()5def Bar ():6 Print('This isbar')
However, the bar function is not loaded into memory when the function is executed, so the function = = variable is deduced.
Recursion for the function:
Characteristics:
1 must have a definite end condition
2 The problem size should be reduced compared to the last recursion each time you enter a deeper level of recursion
3 recursive efficiency is not high, too many recursive layers will cause stack overflow (in the computer, the function call is through the stack (stack) This data structure, each entered a function call, the stack will add a stack of frames, each time the function returns, the stack is less than a stack of frames. Because the size of the stack is not infinite, the more recursive calls, the more often the stack overflows
Python function basics and function recursion