[Chapter 9] Functions of python

Introduction of Functions

Functions are organized, reusable code segments that implement a single, or related function.

Functions can improve the modularity of applications and the reuse of code. You already know that Python provides many built-in functions, such as print(). But you can also create your own functions, which are called user-defined functions.

IV. PARAMETERS OF FUNCTIONS

Principles of functional parameters:

Parametric variables allocate memory units only when they are called. At the end of the call, the allocated memory units are released immediately. Therefore, the parameter is only valid inside the function. The parameter variable cannot be used again after the function call ends and returns to the main calling function.

Arguments can be constants, variables, expressions, functions, etc. No matter what type of arguments they are, they must have definite values when calling functions in order to transmit these values to the parameters. Therefore, the parameters should be determined by assignment and input in advance.

Anonymous functions, however complex, can only write one line and return data directly after the logic has finished.

The return value, like a normal function, can be of any data type

Anonymous functions do not mean that there must be no name. The variable in front of them is a function name. The reason why it's a function name is that when we look through _name_ we don't have a name. The unification is lambda. There's nothing special when we call it, just like a normal function.

9. Higher Order Functions

9.1 sorted sort

Grammar: sorted(Iterable, key=None, reverse=False)
Iterable: Iterable Object
key: Sorting rule (sorted function), in which each object in an iterative object is passed to the parameter of the function and sorted according to the result of the operation of the function.

9.2 filter filtering

Filter: traverse each element in the sequence, determine that each element gets a Boolean value, and if True, leave it behind to form a new iterator
Syntax: filter(function, Iterable) returns an iterator
Function: A function used for filter ing, in which the elements in iterable are automatically passed to the function, and then the number of data retained is determined by the True or False returned by the function.
Iterable: Iterable Object