Python study notes (five) function and code reuse

  Function module of the application can be improved, and code reuse. In many high-level languages, you can use the function to achieve a variety of functions. In the previous study, I believe you already know Python provides many built-in functions, such as print (). Similarly, you can also create your own functions, which are called user-defined functions to implement custom features.

First, the basic function is used

1. Definition Function

  Function is a period with specific functions, reusable group of statements , with the function name and function call is represented by the function name.

  Function to complete a particular function, and the black box similar to the function of the utility function need to know the internal implementation principle, as long as the understanding of the function to input and output. So, the function is a function abstraction.

Use function has two purposes: to reduce the difficulty of programming and code reuse. So complex functions can be programmed using the function implementation.

Wherein, Python using reserved word def defines a function, the following syntax:

  DEF <function name> (<parameter list>):

    <Function> The

    return <return a list of values>
Note: When the list of parameters in a plurality of parameters, default parameter values, and parameter names are defined in order to match the function declaration together.

2. Functions of the calling process

Program calls a function to be executed at four steps:

  (1) calls the program be suspended at the call.

  (2) copy the arguments to the function parameter when invoked.

  (3) perform the function body statements.

  (4) function call given by the end of the return value, the program returns to pause before the call to continue.

 

  From the calling procedure function, people come out of the functional programming concept. Functional Programming is a programming paradigm, further comprising a common programming paradigm imperative programming and object-oriented programming. The main idea of functional programming is the procedure by using a series of function can make coding more concise, easier to understand, small and medium-scale software projects is the most common form of programming.

Second, the parameters of the transfer function

1. Optional and a variable number of parameters,

Optional parameters  

  When you define a function, if there is a default value of some parameters, that part does not necessarily need to call the program input function, you can specify a default value when defining the function directly for these parameters. When the function is called, if no incoming parameter value corresponding to the default value when the function is defined instead. E.g:

  Use the default parameters:

def printf(str, times = 3):
    print(str * times)
>>>printf("hello")
hello
hello
hello

  Input parameters

def printf(str, times = 3):
    print(str * times)
>>>printf("hello",2)
hello
hello

 

Variable parameters  

  When the function definition, you can also design variable parameters , achieved by adding an asterisk before the parameter (*). Variable parameters with an asterisk can appear only at the end of the argument list. When invoked, these parameters are treated as a tuple passed to a function.

E.g:

def printf(times,*b):
    print(b*times)
printf(2,1,2,3)
>>>(1, 2, 3, 1, 2, 3)

2. The location and name of the parameter passed

   Python is provided in a manner in accordance with input arguments parameter name, e.g.

  return = fuc (x1 = 2, x2 = 5, x3 = 8) # where the parameters determined by the function fuc

As specified by the parameter name the function is called, the order between the parameters can be adjusted.

3. The return value of the function

  return statement is used to exit the function and return the program position is called continue.

  Meanwhile, there may be no return function, the function does not return values ​​at this time. Function may return multiple values ​​return, the plurality of values ​​stored in tuples.

4. The role of function variables

  A variable in the program includes two types: global and local variables . It refers to global variables defined outside the function variable, generally not indented, the whole process performed in the program is valid. Refers to the local variable variables inside a function used only within the valid function, the function exits when the variable does not exist.

Python functions which comply with the following principles of a variable to:

(1) simple data types, variables and global variables, whether or not the same name, only to create and use within the function, the function exits the variable is released, if the same name as a global variable, its value remains unchanged.

(2) simple data type variable declared with the reserved word global, used as a global variable, the variable retains the function exits and the function value is changed.

(3) a combination of data types for global variables, if the variable is not the same name to be created inside the real function, and the function can be used to modify the internal values ​​of global variables.

(4) If the internal function to create a true combination of variable data type, regardless of whether there is a global variable of the same name, function operates only on local variables, local variables are released after the function exits, the global variable value unchanged.

Third, code reuse and modular design

  Function is an abstract program, which is achieved by encapsulating code reuse . The modular design of the program can use the function.

  Program consists of a series of code components, if the code is a sequence but unorganized, is not conducive to reading and understanding, it is difficult to maintain and upgrade. Thus, the code resource as the abstract, forming a structure easy to understand.

Function is a program of basic abstract way, it will organize a series of codes by name for other programs. Direct benefit of encapsulation is the function code reuse, as long as any other code input parameters to the calling function, so as to avoid repeating the same code written at the called. Code reuse produce another benefit, when the update function, the function is invoked at all can be updated.

The modular design refers to a function or object encapsulating function program into expression between main programs, subprograms and subprogram relationship. The modular design and using ways of thinking function object design program, function block as a basic unit, generally have the following two basic requirements:

  (1) tight coupling: as rational division function block, tightly coupled inside the block.

  (2) loose coupling: the relationship between the modules as simple, low coupling between the functional blocks.

Generally speaking, it is loose coupling between the internal modules tightly coupled, module

Fourth, the recursive function

1. recursive definition

  As a function of the code package may be called by other procedures, of course, the function may be called internal code. This function is defined in a way that calls itself a function is called recursively .

Two key features of recursion:

(1) the presence of (the solution of the problem of minimum size) one or more groups embodiment, the base need not recursive embodiment again, it is determined expression.

(2) recursively to all end groups in one or more embodiments.

2. The use of recursive

Example: factorial calculation.

def fact(n):
    if n == 0:
        return 1
    else:
        return n * fact(n-1)

n=eval(print())
print(fact(abs(int(n))))

Input:

>>>10
26288800

 

Guess you like

Origin www.cnblogs.com/Y-xp/p/11589366.html