Python-based function, recursive.

Source: Internet
Author: User
Tags define local variable scope

I. Math-defined functions and functions in Python

  -Junior Math function definition: Generally, in a change process, 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 refer to x as the independent variable, y is called the dependent variable, Y is the function of X. The range of values of the argument x is called the definition field of the function.

-function definition in Python: function is a programming method of logical structure and process.

two. Why use a function

1. Resolving Code Reuse

2. Maintain consistency and ease of maintenance

3. Extensibility

three. Function Parameters

1. Formal parameter: Shape parametric the memory unit is allocated only when it is called, and the allocated memory unit is freed immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. The parameter variable cannot be used again after the function call ends and the parameter does not occupy memory space.

2. Arguments: Can be constants, variables, expressions, functions, and so on, regardless of the amount of the actual argument, they must have a definite value when making a function call, in order to pass these values to the parameter. Therefore, the parameter should be determined by the method of assignment, input and so on beforehand. The actual parameter occupies memory space.

3. Position parameters and keywords:

Positional parameter parameters and the position of the argument must be one by one corresponding, the keyword: position without fixing

4. Default parameters:

is already defined in the parameter and does not need to be re-assigned in the argument

5. Parameter groups:

*args can be assigned to a list **kwargs can be assigned to a dictionary

four. Local variables and global variables

1. A variable defined in a subroutine is called a local variable, and a variable defined at the beginning of the program is called a global variable .

2. The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable.

3. When a global variable has the same name as a local variable: Local variables work within subroutines that define local variables, and global variables work elsewhere.

Five. Forward reference ' function is variable '

function is like the definition of a variable, when defining a variable, you must first open a space in the hard disk to store the variable, and the function is the same, and then define the function must also open a space in the hard disk to store the function, when it is time to call out to use.

While running the function, although the function you are calling may have been written below, but the program has not yet run to that point, the contents of the function have not yet been created on the hard disk, so the program will have an error when you call the function earlier.

   

Six. Nesting functions

Yes, functions can also be nested one layer at a level.

" Ah, cat . " def N1 ():     " Ah, dog . "    def n2 ():        nonlocal name        "Alex"    n2 ()     Print (name) Print (name) N1 () Print (name)

What is the result of the final output?

Eight recursion

Inside a function, you can call other functions. If a function calls itself internally, that function is a recursive function.

def Calc (n):     Print (n)     if int (N/2) = =0        :return  n    return calc (int (n/2)) Calc (10521) output:
Importtimeperson_list=['Alex','Wupeiqi','Linhaifeng']defAsk_way (person_list):Print("-"*60)    ifLen (person_list) = =0:return 'no one knows.' Person=person_list.pop (0)ifperson = ='Linhaifeng':        return '%s said: I know, the old boy is in Shahe Hui Tak Commercial Building, the subway is'% PersonPrint('hi Beauty [%s], dare to ask where the road is'%Person )Print('%s replied: "I do not know, but read your eyes know pig, you wait, I help you ask the%s ... "'%(person, person_list)) Time.sleep (1) Res=Ask_way (person_list)Print('The result of the%s question is:%res'%(person, res))returnResres=Ask_way (person_list)Print(RES)
recursive ask the way

Recursive properties:

1. There must be a clear end condition

2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion

3. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow.

Python-based function, recursive.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.