Functions in Python and recursion

Source: Internet
Author: User

a function

The composition of the function:

def funname (parameters):

Instructions ....

Before exploring the definition of a function, let's think about it, if we write thousands of lines of code, in fact various variables define, loop ... How do we know where the code is going? Or when it's over?

Because we should talk about the first feature of the function--decomposition

Decomposition: Refers to modularity--the ability to split code into modules that work independently, making the code clear and reusable
So how do we break it down? Then let's talk about the second feature: Abstraction

Abstract: Finds the same pattern of operations.

Let's take a look at the code below to familiarize yourself with the following

def One (x):     +1print  xz  = one  (ten)print  x        

What are the two outputs of x here?

It's obvious that the problem here is global variables and local variables.

We think of this code file as a house, but the function is the refrigerator inside the house, the space inside the refrigerator is a local space, then the apple in the fridge and the apple in the room above the table is not an apple (I know this analogy is inappropriate)

So the results are 4 4.

Two recursion:

It's hard to give a complete definition of recursion, but I'd like to say my understanding (the last time I saw someone say that recursion is the function call function itself, I think it's a very tangled argument.) But think about it and don't know where it went wrong lol

recursive----> Resolves a problem by breaking it down into smaller, more homogeneous problems (which may be the capacity of the problem or the depth of the problem)

For example: How to tell if a number is a palindrome number?
Palindrome Number: 1 121 1221 12321 2332 that's about it.

Here, for example, x = ' 12323432121 ' judgment: Let's compare the size of the first and last number, and we'll take the two numbers and do it again.

  

DEF II (x):    If Len (x) > 1:        i = x[:1]        L = x[-1:]        if i = = L:            x = x[1:]            x = x[:-1]            If len (x) = = 0:                return true            else: both                (x)        else:            return False    return True

See is not very simple

Functions in Python and recursion

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.