I. Introduction to Functions
Functions are well-organized, reusable pieces of code that are used to implement a single, or associated function.
Functions can improve the modularity of the application, and the reuse of the code. You already know that Python provides a number of built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions
Benefits of the function:
- Code reuse
- Consistency and ease of maintenance
- Scalability
Second, the definition of function
Definition rules for functions:
- function code block starts with def keyword, followed by function identifier name and parentheses ()
- Any incoming parameters and arguments must be placed in the middle of the parentheses, and the parentheses can be used to define the parameters
- The first line of the function statement can optionally use the document string-for storing function descriptions
- Function contents start with a colon, and indent
- return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None
Syntax for the function:
def function Name: function Body
Examples of functions:
Def Print_hello (): "" " print Hello : return:" "" " print (" Hello ")
Third, the return value of the function
Look at the return value of Python based on the example:
Def fun2 (): msg = "Hello World" return Msgdef fun3 (): return 1, 2, 3 AA = fun1 () BB = fun2 () cc = FUN3 () print (AA ) print (BB) print (cc) # output: # none# Hello world# (1, 2, 3)
Summarize:
1. If no return statement is returned in the function, the Python function returns none by default
2. The function returns a value of 0, the function returns none by default, the function returns a value of 1 is, the object is returned, and the number of returned value is greater than 1 o'clock, a tuple is returned;
Iv. parameters of the function
Python learns the "fourth" Python function