Function:
Functions: Encapsulation of functions
Grammar:
def function name (formal parameter):
function body
Function name (argument)
Function name: Naming rules and variables
Cases
# while 1:
# Print ("First, turn on your phone")
# Print ("Open Momo")
# Print ("Find a certain one that looks good")
# Print ("Come out about a. Happy Heart")
# Print ("Each home, each to find the mother")
# The above function needs to be defined or encapsulated. When it's needed. Carry it around any time. This thing that encapsulates is a function.
# define a function called Yue
Def Yue ():
# function body, code block
Print ("First, turn on your phone")
Print ("Open Mo Mo")
Print ("Find a certain one that looks good")
Print ("Come out about a. Happy Heart")
Print ("Each home, each to find the mother")
# function call, function name ()
Yue () # execution Function # First Call
Print ("normal work")
Yue () # Second Call
The return value of the function:
Return, the function is finished executing. No subsequent logic is executed
1. If the function does not write return return none
2. Write return only returns none
3. return value. Returns a value
4. Return value 1, value 2,... Returns multiple values. The caller receives a tuple
Cases
# Def Yue ():
# Print ("About You")
# Print ("About him")
# Print ("I'm not about")
# return "Aunt" # return value when the function ends. Give the caller a result
#
# ret = Yue ()
# Print (ret)
#
# Ret2 = Yue ()
# Print (Ret2)
# as soon as the function executes to the return function, it stops executing
# 1. Each function returns none by default if the return is not written in the function.
# 2. We can also write only a return, also return none, stop the execution of the function
# 3. Returns a return value. You can accept a return value in the caller
# 4. Returns multiple return values. Multiple values need to be separated from the received location. A tuple is received.
# Def Yue ():
# Print ("About one")
# Return # The function stops.
# ret = Yue ()
# Print (ret)
# Def Yue ():
# Print ("Open Momo")
# Print ("Big Brother")
# return "Aunt", "Lori", "Model"
#
# a,b,c = Yue () # multiple return values received in the accepted location are of type tuple data
# Print (a)
# print (b)
# Print (c)
Parameters:
The information that is passed to the function when the function executes
1. Formal Parameters:
1. Position parameters.
2. Default value parameter. The default value is not used when calling a value
3. Mixed use. Order: 1. Position parameter, 2, default value parameter
2. Actual parameters:
1. Positional parameters. Assigning a parameter to a position by location
2. Keyword parameters. Assigning parameters to parameters by name
3. Mixing parameters. Use positional parameters first, then use keyword parameters
Cases
# def Yue (Fangshi, age): # parameter
# print ("Turn on Phone")
# print ("open%s"% Fangshi)
# print ("Find a beautiful sister")
# print ("Best age is%s"% ages)
# print ("Come out about about one")
#
# Yue ("")
# Yue ("probing", 38)
# yue ("Mo Mo", 26)
# DEF FN (a,b,c):
# Print (a)
# print (b)
# Print (c)
#
# fn (1, 2, 3)
# fn (b=3, a=2, c=1) # When the argument is called. You can specify how much the value of the parameter is. Keyword (formal parameter) parameter
# def GN (name, age, address, id, sex, hobby):
# print ("person's name is%s, age:%s, Address:%s, ID:%s, Gender:%s, Hobby is:%s"% (name, ages, address, id, sex, hobby))
# GN ("Taibai", 58, "Baoding", 1, "Unknown", "female") # positional parameters
# GN (id=1, name= "Taibai", address= "Baoding", hobby= "female", age=58, sex= "Unknown") # keyword (parameter) parameters
# GN ("Taibai", 58, "Baoding", id= "3", sex= "female", hobby= "Unknown")
def regist (ID, name, sex= "male"):
Print ("Input student Information: ID is%s, name is%s, gender is:%s"% (ID, name, sex))
Regist (1, "Tai Yang elder brother")
Regist (2, "Xu Weixing")
Regist (3, "Little Sister", "female")
# parameter: The information that is passed to the function when the function is executed
# formal parameter: The position of the function declaration. Declare out variables
# arguments: When a function is called. The actual value you pass to the function
# The number of arguments to the function is not required, but at run time. The formal parameters and arguments to match. Assigning arguments to parameters by location
# Categories of parameters:
# Stand at the angle of the argument:
# 1. Position parameters
# 2. Keyword parameters
# 3. Mixed parameters, note the order. Write the positional parameter first, and then in the Write keyword parameter. Otherwise it will be an error
# Stand at the angle of the formal parameter:
# 1. Position parameters
# 2. Default value parameter
# 3. The default value parameter is mixed with the position parameter. Order: Write positional parameters first. Then write the default value parameter
Practical application
Cases
# 1+2+3+4+5+6+7+8+9+....+100 =? 5050
# Encapsulation of features
# def SUM (n):
# s = 0
# Count = 1
# while count <= N:
# s = s + count
# Count = count + 1
# return S
#
# ret = SUM (456)
# Print (ret)
# calculates whether n is an odd or an even number
# DEF FN (n):
# If n%2 = = 0:
# return "even"
# return "odd"
#
# Print (FN (456))
# lst = ["Tai Yang elder brother", "Xu Weixing"]
# Print (lst.append ("hu Spicy Soup")) # no Return
#
# s = "haha"
# Print (S.replace ("Ha", "Ah") # must have return
Python 2018.7.12