This is the 12th day of my participation in Gwen Challenge
We’ve finished with data types, flow control statements, and we’re going to have to encapsulate these separate pieces of code and use them whenever we need them.
At this time, there is the concept of “function”, not much to say, we will learn function today ~
1. Function introduction
1.1 What is a function
Functions are reusable blocks of program code.
Function function, not only can realize the reuse of code, but also can realize the consistency of code.
Consistency refers to the fact that all calls to the general function are reflected as long as the code of the function is modified.
When writing the function, the code writing method in the function body is basically the same as what we talked about before, which only encapsulates the code, and increases the function call, parameter passing, return calculation results and other content.
Therefore, we know that functions have the following characteristics
- A function is an important building block of a Python program, representing a function or task
- A Python program can consist of multiple functions
- Functions are a general mechanism for code reuse
1.2 Function Classification
Python can be divided into functions with no arguments and functions with arguments
Functions in Python are divided into four broad categories by source
The serial number | The name | case |
---|---|---|
1 | Built-in function | STR (), list (), len (), etc |
2 | Library function | Import the library through the import statement |
3 | Third-party libraries | After pipx is downloaded and installed, import is imported |
4 | User-defined library | Users customize functions as needed |
2. Function definition and invocation
2.1 Function Definition
In Python, the syntax for defining functions is as follows:
Def function name (argument list): "docstring" function body (several statements) [return(return value)]Copy the code
- Composition of functions
1.def:
(1) When Python executes def, it creates a function object and binds it to the function name variable. Therefore, it must start with its keyword
2. Function name:
(1) It is composed of one or more meaningful words.
(2) Each word should be lowercase and separated by an underscore
3. Parameter List:
(1) Parameters that the function is used to receive. Placed in parentheses, can be null. (2) Use commas (,) to separate multiple parameters.
4. Docstring:
(1) Use three quotation marks to describe the function for subsequent viewing
5. Function body:
(1) Composed of multiple statements, which are executed in strict sequence. Statements before the function body are always executed first, and statements after the function body are always executed last.
- Return the return value
Def print_word(word): for I in word: print(I) def print_word(word): print(I)Copy the code
2.2 Function Call
Function call steps
1. You must define the function first, that is, call def to create the function object
(1) Automatic creation of built-in function objects
(2) Standard library and third-party library functions that execute def statements in modules when importing modules through import
2. Pass in parameters
3. Execute the function body
4. Return the result
Print_word (STR) print_word(STR) = "JueJing" print_word(STR)Copy the code
3 functions are mainly composed
3.1 form participates in the argument
We will learn more about the types of parameters later, but this time we will outline them.
The details of parameters and arguments in the function are as follows:
1. Place them in parentheses. Use commas (,) to separate multiple parameters.
2. Parameters do not need to declare a type, nor do they need to specify the return type of the function
3. If no parameter is specified, leave empty brackets
4. Once the argument list is specified in the function definition, the function must be called with the correct argument values (arguments).
5. Parameters and arguments must correspond one by one; otherwise, the program will fail to execute.
- A function parameter
(1) is a parameter belonging to a function, that is, the parameter in the function argument list, can only be used in the function.
(2) Used to define parameters
- The function arguments
(1) is the specific argument we take when we call the function.
(2) Used to call
Let’s take a look at chestnuts:
Def factorial(num): f = 1 if num < 0: return None elif num == 0: return 1 else: for i in range(1, num + 1): Print (n1) n2 = factorial(3) # 3 print(n2) n3 = factorial(3,4) # Print (n3)Copy the code
3.2 Function Comments
The readability of a program is very important, and it is generally recommended to enclose the function definition at the beginning of the function body. This is called the docstring, also known as the function comment.
-
We can do this with three single quotes or three double quotes, with multiple lines of text in between.
-
View docstrings that print functions by calling help(function name :doc
"Print ("*"*n) help(print_star)" print("*"*n) help(print_starCopy the code
3.3 the return value
Return Return value features:
- 1. If the function body contains a return statement, the function is terminated and the value is returned
Print (a) print(a) print(add(30,40)+20) print(a) print(add(30,40)+20Copy the code
Return Terminates the function body execution
Def test(): print("start") print("starting") returnCopy the code
- 2. If the function body does not contain a return statement, None is returned
Def add(n, m): def add(n, m): sum = n + mCopy the code
- 3. If multiple values are returned, Python automatically wraps them into tuples
Def test(x,y,z): return x,8*y,z print(type(test(1,3,5)))Copy the code
4. Function memory distribution
4.1 Creating a Function
Procedure for creating a function:
- In Python, def is used to create function objects that are stored in the heap
- The address is then assigned to the variable (function name) and stored in stack memory
- Waiting to be called
Def print_word(word): for I in word: print(I) def print_word(word): print(I)Copy the code
4.2 Function Objects
In Python, everything is an object, and the system actually creates the corresponding function object after def defines the function.
Print_word () print(c) print_word(STR) print(c)Copy the code
conclusion
This period introduces the basic concept of function, and function memory analysis, next time we learn the function parameters related content.
All right, that’s it for this issue. You are welcome to comment in the comments section.