Example:
when we know the value of radius r , we can calculate the area according to the formula, assuming we need to calculate the area of 3 Circles of different sizes:
# !/usr/bin/env python # -*-coding:utf-8-*-r1=2.34R2=7.28R3=10.32s1=3.14*r1*r1s2= 3.14*r2*r2s3=3.14*R3*R3
The contemporary code has the regular repetition, each time writes 3.14*x*x not only is very troublesome, and if wants to Change the 3.14 to 3.14159 The time must replace completely
with the function, we no longer write s=3.14*x*x every time , but rather make a more meaningful function call S=area (x), and the function Area It takes only a few times to call
Abstraction is a very common concept in mathematics, for example
calculation of the sum of the series, such as 1+2+3+.....+100, writing is very inconvenient, so mathematicians invented the summation symbol Σ, you can put 1+2+3+ ... 100 is called:
This abstract notation is very powerful, because we see that Σ can be understood as summation, not reduced to lower-level addition operations.
Writing a computer program is also the same, the function is the most basic way of code abstraction
The most important purpose of the function is to make it easy for us to call the same program repeatedly
Python not only has the flexibility to define functions, but it also has a lot of useful functions built into it, which can be called directly
Definition of a function
in Python , you define a function to use the def statement, write out the function name, parentheses, the arguments in parentheses, and the colon, and then write the function body in the abbreviation block, with the return value of the function return statement returns
For example:
we customize an absolute value. Numbers function as an example
#!/usr/bin/env python#-*-coding:utf-8-*-defNumbers (x):ifx>=0:returnxElse return-x#function of this function is to find the sum of squares of two numbers#First Def, this keyword notifies python, I'm going to define a function, numbers is the function name, the parameter of x in parentheses, is the input to the function#parameters can be one or more, or none at all, but the parentheses are preservedifX>=0:#indicates that this sentence determines the size of x so that the statement is calculatedElse:#if the If post condition is false execution of this statementreturnX#Returns the value of C, which is the function of the output, the Python function allows no return value, that is, without returnreturnA,b,c#equivalent to return (A,B,C)
when the statement inside the function body executes, once it executes to the return statement, the function finishes and returns the result
If there is no return statement, the function will return the result after execution, only the result is none,return none can be shortened to return
in python , when the program executes to return , the program stops executing the remaining statements in the function
None is A special data type in Python that represents nothing, equivalent to the C language of null,none Multiple default values for keyword parameter passing
Call to function
to invoke a function, you need to know the name and parameters of the function, such as the function abs for absolute value . It receives a parameter
Documents can be viewed directly from the official Python website:
http://docs.python.org/2/library/function.html#ABS
You can also view the help information for the abs function on the interactive command line via the Assist (ABS)
return value of the function
Return single value
# !/usr/bin/env python # -*-coding:utf-8-*- def Numbers (x): if x>=0: Print(x) Else: Print(-x) Numbers (-5)
Returns multiple values
The Math package provides the sin () and cos () functions, which we first refer to with import
# !/usr/bin/env python # -*-coding:utf-8-*- Import Math def Move (x,y,step,angle): xx=x+step*math.cos (angle) yy=y-step*math.sin (angle) return xx,yyx,y=move (100,100,60,MATH.PI/6) print x, y printing 151.961524227 70.0
In fact, this is an illusion,the Python function returned is still a single value
# !/usr/bin/env python # -*-coding:utf-8-*- Import Math def Move (x,y,step,angle): xx=x+step*math.cos (angle) yy=y-step*math.sin (angle) return Xx,yy Print (Move (100,100,60,MATH.PI/6)) # Print (151.96152422706632, 70.0)
Prints The returned result with print, the original return value is a tuple
Note : In syntax, returning a tuple can omit parentheses, and multiple variables can accept a tuple at the same time ,assigning the corresponding value by location, so python a function returning a multivalued value is actually returning a tuple , but it's more convenient to write.
The 16th lesson of Python Learning--function and definition of functions