Basics python - function code reuse and

One. Define and use functions

1. Definition: The function is a piece of code,

- having a specific function is a period function, reusable statement group

- a feature function is abstract, usually expressed as a function specific functions

- two effects: reduce the difficulty of programming and code reuse

DEF <function name> (<parameter (0 or more)>):

  <Function> The

  return <Return value>

After calling the function definition without going through it is not performed.

2. Call: Run the function code of way

- To give the actual parameters when calling

- actual parameters defined in the substitution parameters

- After the return value of the function call to get

 

 3. The transfer function parameters

   1. The number of parameters, may also be a plurality of 0, but it must retain the brackets

 

   2. optional parameter: you can specify default values ​​for certain parameters defined, optional configuration parameters

 

 

 

   3. Variable transmission parameters: the number of variable parameters may be designed to function definition, neither the total number of parameters determined

 

 

 

   4. The parameters passed in two ways: position transfer / transmission name

 

 4. Local and global variables

 

 

 

 Rule 1: Local and global variables are different variables

- local variables inside a function placeholder with the same name but a different global variables may

- After the calculation function, local variables is released

- You can use the reserved word global use global variables inside a function

 

 Rule 2: Create a local variable and not as a combination of data types, the global variable is equivalent to

 

 

Compared:

 

 

5.lambda function: anonymous function, no function name. A simple definition, the function can be represented in a row

 

 

 

 Second, recursive function code reuse and

Code reuse: the code as abstract resources. It can be reused when necessary

Recursively defined: the definition of the function calls itself a function of ways, such as:

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ShallByeBye/p/12464011.html