More , Please visit mine Personal blog .
A function is a reusable 、 A block of code that implements a specific function .
The feature of the function is that it can improve the application Modularity , And code reusability .
Python
Define function use def
keyword , The general format is as follows :
def Function name ( parameter list ):
The body of the function
We need to pay attention to the following :
def
Key words start with , Followed by function identifier name and parentheses ().return
It's equivalent to returning to None
. Let's write a function , For output Hello Python!
.
def hello():
print("Hello Python!")
Add a comment to the function .
def hello():
""" Output Hello """
print("Hello Python!")
Add a parameter , And set default values for parameters .
def hello(name='Python'):
""" Output Hello """
print("Hello " + name + "!")
Set return value , If we do not set up , The default return value is None
.
def hello(name='Python'):
""" Output Hello """
print("Hello " + name + "!")
return 'ok'
Call function .
def hello(name='Python'):
""" Output Hello """
print("Hello " + name + "!")
return 'ok'
h = hello()
print(h)
Find out the errors and irregularities in the following code .
def getMax(a=0, b = 0, c)
''' return a,b,c The largest of the three numbers '''
if a >= b and a > c:
return a;
elseif b > a && b > c:
return b
else:
return c
return None
Official account : Pangao will accompany you to learn programming , reply 018, Get the answer to the exercise .