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