Function
One. The initial knowledge of the function
For example: We measure the length of a string ' asdfghj ', but do not let Len ()
' ASDFGHJ ' = 0 for in s: + = 1print(count)
If we were to measure the length of the other strings, we would write these similar code again, with strong repeatability and poor readability , we could introduce the concept of function.
Two. Definition and invocation of a function
def My_len (): # def keyword, defining function space function name (variable) 'asdfghj ' = 0 for in s: + = 1 Print (count) My_len () # execute this function
Three. return value of function
Return
1. When the return is encountered, this function ends and no longer runs (similar to break in the loop)
There are four cases of 2.return
1) do not write return returns None
def My_len (): ' ASDFGHJ ' = 0 for in s: + = 1 = My_len () Print (ret) # None of the output results
2) return None returns none
def My_len (): ' ASDFGHJ ' = 0for in s: + = 1 return = My_len ()print(ret) # Output None
3) Return a value that returns this single value
def My_len (): ' ASDFGHJ ' = 0for in s: + = 1 return = My_len ()print(ret) # output: 7
4) return multiple values returned to the caller by returning multiple values in the tuple
def My_len (): "returns multiple values ' return count,1,3, " alex ", 8ret = my _len () print (ret) # output result: (7, 1, 3, ' Alex ', 8)
# Receive with multiple values def My_len (): return 1,['a','b'],3,4ret1,ret2,ret3,ret4 = My_len () Print(ret1,ret2,ret3,ret4)# output: 1 [' A ', ' B '] 3 4
Four. Parameters
Real participation Formal parameters
The parameters are also different:
The "Hello World" passed when we called the function is called the actual argument, because this is the actual content to be given to the function, referred to as the argument .
The S1 that defines a function is simply the name of a variable, called the formal parameter , because it is only a form when defining a function, which means that there is a parameter, referred to as a formal parameter.
Passing Multiple parameters
Parameters can be passed multiple, and multiple parameters are separated by commas.
1. Actual parameters
Angle of argument:
1) pass the parameter by location
def my_line (x, y ): ' ' = ifelse y return Max # call ma = my_line (5,10)print(MA) # output: 10
2) Press the key word to pass the parameter
def My_len (x, y ): # x = 5, y = if Else y return Max # call ma = My_len (y=20,x=5)print(MA) # output:
3) mixed parameter keyword parameters are always behind the position parameter
def My_len (x, y ): # x = 5,y = if Else y return Max # call ma = My_len (5,y=15)print(MA) # output:
2. Formal parameters
1) Position parameters
def Mymax (x, y ): # at this time x=10,y=20 if Else y return = Mymax (10,20)print(MA) # output:
2) Default parameters
defFA (name,sex='male'): With open ('name_list','a', encoding='Utf-8') as F:f.write ('{},{}\n'. Format (name,sex)) while1: Name= Input ('Please enter your name:') if 'elder sister' inchName:sex= Input ('Please enter gender:') FA (name,sex)Else: FA (name)
Python function One