1. Function concept:
A function is an organized, reusable piece of code that implements a single, or related function .
Functions can improve application modularity and code reuse. You already know that Python provides many built-in functions, such as print(), len(), etc. But you can also create your own functions, which are called user-defined functions.
Second, the definition method and call of the function:
def + functionname():
'''Comment'''
function body
def mylen(): """计算s1的长度""" s1 = "hello world" length = 0 for i in s1: length = length+1 print(length) #function call mylen ()
Definition: Begin with the def keyword, followed by a space followed by the function name and parentheses (), and a " : " at the end . def is fixed and cannot be changed, it is the keyword for defining functions. Space In order to separate the def keyword from the function name, it must be empty (four sounds). Of course, you can leave 2 spaces, 3 spaces, or as many spaces as you want, but normal people still leave 1 space. Function name: The function name can only contain strings, underscores and numbers and cannot start with a number. Although the function name can be arbitrarily named, we should try to name the function as short as possible and express the function of the function. Parentheses: must be added, don't ask why there are parentheses, in short, add parentheses! Note: Each function should have a corresponding description of the function and parameters, which should be written on the first line below the function. to enhance the readability of the code. Call: it is the function name (), remember to add parentheses
Third, the return value of the function:
Add a return at the end of the function, and write the return value you need after return
3.1 The role of the return keyword
return is a keyword, and in pycharm, you'll see it turn blue. You must memorize the word word for word.
This word is translated as "return", so we call the value written after return "return value"
To study the return value, we also need to know that there are several situations for the return value: no return value, one value returned, and multiple values returned.
3.2 No return value
If no return is written, a None will be returned by default: the first function we write does not write return, which is a situation where there is no return value.
Note: Only write return, if you don't write anything else, it will also return None. Some students will be surprised. Since there is no value to be returned, you can not write return at all. Why write a return? Here we want to talk about other uses of return, that is,once return is encountered, the entire function ends.
3.3 Return a value
We have just written a case of returning a value, just write the content to be returned after return.
3.4 Returning multiple values
Any number of values of any data type can be returned. (Multiple values returned will be organized into tuples and returned, or they can be received with multiple values, that is, assigned separately)
Fourth, function parameters
4.1 Actual participation parameters:
#Function definition def mylen(s1): """ Calculate the length of s1 """ length = 0 for i in s1: length = length+1 return length #function call str_len = mylen( " hello world " ) print ( ' str_len : %s ' %str_len)
actual participation parameter
The parameters are also different:
The "hello world" we pass when we call the function is called the actual parameter, because this is the actual content to be given to the function, called the actual parameter for short.
When defining a function, s1 is just the name of a variable and is called a formal parameter, because it is just a form when defining a function, indicating that there is a parameter here, referred to as a formal parameter.
pass multiple parameters
Multiple parameters can be passed, and multiple parameters are separated by commas.
def mymax(x,y): the_max = x if x > y else y return the_max ma = mymax (10,20 ) print (ma)
Note: the_max = x if x > y else y is a ternary operation
4.2 Positional parameters
From the point of view of actual parameters
1. Pass parameters by position
def mymax(x,y): #此时x=10,y=20 the_max = x if x > y else y return the_max ma = mymax (10,20 ) print (ma)
2. Pass parameters by keyword
def mymax(x,y): #At this point x = 20,y = 10 print (x,y) the_max = x if x > y else y return the_max ma = mymax (y = 10, x = 20 ) print (ma)
3. Mixed use of location and keywords
def mymax(x,y): #At this point x = 10,y = 20 print (x,y) the_max = x if x > y else y return the_max ma = mymax (10, y = 20 ) print (ma)
Rules for passing parameters: 1. Positional parameters must be in front of keyword parameters
2. A parameter can only be assigned once
From the point of view of formal parameters
Positional parameters must be passed by value
def mymax(x,y): #At this point x = 10,y = 20 print (x,y) the_max = x if x > y else y return the_max #call mymax without passing parameters ma = mymax() print (ma) #resultTypeError : mymax () missing 2 required positional arguments: ' x ' and ' y '
4.3 Default parameters
1. Normal use
Instructions
Why have default parameters: set the value with less variation as the default parameter
2. Definition of default parameters
def stu_info(name,sex = " male " ): """ Print student information function, since most of the students in the class are male, So set the default parameter sex to the default value of 'male' """ print(name,sex) stu_info('alex') stu_info('eva','female')