The most important purpose of a function is to facilitate our repeated use of the same program.
To subordinate some operations to a function, you can use the Call Function name only when you want to implement the same operation, without having to repeatedly knock all the statements.
Definition of function
First, we're going to define a function to illustrate the function of this function.
Copy Code code as follows:
def square_sum (a,b):
c = a**2 + b**2
Return C
The function of this function is to find the sum of squares of two numbers.
First, Def, this keyword informs Python: I'm defining a function. Square_sum is the name of the function.
A, b in parentheses is the function's argument, and is the input to the function. Parameters can have multiple or none at all (but parentheses are to be retained).
We've seen colons and indented affiliations in loops and selections.
Copy Code code as follows:
c = a**2 + b**2 # This sentence is a function of the internal operation
Return C # Returns the value of C, which is the function of the output. The Python function allows no return value, which is not returned.
Returns can return multiple values, separated by commas. Equivalent to returning a tuple (fixed value table).
Return A,b,c # equals return (A,B,C)
In Python, when a program executes to return, the program stops executing the remaining statements within the function. Return is not necessary, and the function will automatically return none when there are no returns, or if there are no returned values. None is a special data type in Python that represents nothing, equivalent to NULL in C. None is used for default values passed by keyword parameters.
function calls and parameter passing
Once you have defined a function, you can use this function in a later program
Copy Code code as follows:
Python passes the position, knowing that 3 corresponds to the first argument in the function definition A, 4 corresponds to the second parameter B, and then passes the parameter to the function Square_sum.
(Python has a wealth of parameter transfer methods, as well as keyword transfer, table delivery, dictionary delivery, etc., the basic tutorial will only involve location delivery)
The function is evaluated and the value 25 is returned, and the 25 is printed by print.
Let's take a look at the following two examples
Copy Code code as follows:
A = 1
Def Change_integer (a):
A = a + 1
Return a
Print Change_integer (a)
Print a
#=== ("#" in Python is followed by comments, not executed)
b = [1,2,3]
def change_list (b):
B[0] = b[0] + 1
Return b
Print Change_list (b)
Print B
For the first example, we pass an integer variable to a function that operates on it, but the original integer variable a does not change.
In the second example, we pass a table to the function, and the function is manipulated, and the original table B is changed.
For a variable of the base data type, after the variable is passed to the function, the function copies a new variable in memory, which does not affect the original variable. (We call this a value pass)
But for a table, a table is passed to a function with a pointer to the position of the sequence in memory, and the operation of the table in the function will be in the original memory, thus affecting the original variable. (We call this a pointer pass)
Summarize
Copy Code code as follows:
def function_name (a,b,c):
Statement
Return something # Return is not a must
The purpose of the function is to increase the duplicate availability of the program.
Copy Code code as follows:
Pass the position, passing the parameter.
Parameters for basic data types: Value delivery
Table as parameter: pointer pass
Practice:
Write a function that determines the leap year, and the parameters are years, months, and days. If it's a leap year, return True