Python3 basics — functions

(Complimentary) : programming to achieve a certain function is ultimately dependent on the function, the function is to achieve a certain function of the code collection, the realization of a logical function can be defined as a function, it provides the program with concise, readable excellent characteristics. In the programming process, we should try to define the code block of a certain logical function as a function, which can increase the code readability and make the program look more concise.

  1. Function declaration definitions: Python function definitions are not like C ++, Java, or C….. Define that c++ function definition.

    • C, C ++, Java function definition: (return type) Function name (argument list)

The return types are void (no return), int (return an integer value), and double (return a double float value)….. Void getName(string name), int getNumber(int number). Now let’s look at how Python defines functions

  • Python function definition: def function name (argument list) :

**def get_name(name): def get_number(number) : ** ——- Why is the function named this way? I elaborate on this in Python Basics -Python Naming conventions ——- for parameters, see below

  1. Parameters: Parameters are the medium or form through which the user or programmer passes information to a function. Parameters are divided into parameters and arguments

    • Parameter: the name in def get_name(name): is the parameter inside the parentheses given by the function declaration.
    • Argument: a value that appears inside the parentheses of a function call, as when we call a function

Get_name (“Daming “) The string entity that appears in parentheses when the function is called is an argument with an actual value and meaning.

  • The following are a series of questions about variable parameters, default parameters, and so on

    1. Position the arguments* : And the position of the arguments in the function call should not be arbitrarily changed, otherwise logic errors may occur or you will not get the result you want

      Such as the program:
def get_people_hobby(name,hobby): "" used to describe someone's hobby "" "print(name," likes ",hobby) get_people_hobby("Daming ".title(),"football ") get_people_hobby("football ".title(),"Daming ")Copy the code

Result: Transposing the argument position does not produce the desired result —— conclusion Do not arbitrarily change the argument position

1. The ** keyword arguments ** : and bind the parameters together during the function call so that we can get the result we want regardless of positionCopy the code

Ex. :

def get_people_hobby(name,hobby): "" used to describe someone's hobby "" "print(name," likes ",hobby) #get_people_hobby("Daming ".title(),"football ") #get_people_hobby("football ".title(),"Daming ") get_people_hobby(name="Xiaohong ".title(),hobby="piano ") get_people_hobby(hobby="piano ",name="Xiaohong ".title()Copy the code

Run result: after the argument is bound together, a change in position does not change the result

** default values ** : sometimes arguments use default values (** note: ** has default values must ** after all ** that does not give ** default values).Copy the code
def discrible_default(type="human ",feature):Copy the code

IDE points you outerror: no default parameter is given after a default parameter is given

** Correct way to define ** :Copy the code
def discrible_default(feature,type="human "):
        print(type.title()," is ",feature)Copy the code
When we ** call a function with a default argument ** :Copy the code

If there are fewer arguments than parameters, Python will take precedence over the previous arguments in the given argument binding (and in the order in which the arguments are bound).

def discrible_default(feature,type="human "):
                 print(type.title()," is ",feature)
discrible_default("intelligent ")Copy the code

The results of



Of course, parameters with default values can also change their values:As follows:

def discrible_default(feature,type="human "):
                 print(type.title()," is ",feature)
discrible_default("intelligent ")
discrible_default("fool ","pig ".title())Copy the code

Results:

conclusionThe binding of parameters and arguments is the key to making the program work the way we want it to. As long as parameters and arguments are bound correctly, the result will work.

1. Function definitions that pass any number of arguments: ** is used when the function does not know in advance how many arguments it needs to acceptCopy the code
def get_type(*types): Print (types) get_type("human ","monkey ") # two arguments get_type("dog ","pig ","cat ") # three argumentsCopy the code
! [_] (https://yqfile.alicdn.com/2185e96ff78e226fb80f309dd8aef0d594a597b7.png) 1. * * pass as keyword arguments: Is ** used to accept any number of arguments, but do not know what information is passed to the functionCopy the code

The following procedures: Requirements: Create a user information, the basic has a name but each user can add their own number of different types of details

def build_user_information(name,age,**info): "" "Create a dictionary for storing basic and user-defined information "" "users_information={} users_information["name "]=name users_information["age "]=age for key,value in info.items(): users_information[key]=value return users_information print(build_user_information("Daming ",32,address="beijing ",work="programmer ") # print(build_user_Information ("Lihua ","18 ",hobby="swim" ",height=170,midle_school_address="linshui ") #Copy the code
Results:Copy the code

  1. The return value: Sometimes you need a function to process some data and then return it back to the user, and that’s when you need a return value. Python uses a key return to do that. There are many different types of return values that can be int, double, string, It can also be tuples, lists, and dictionaries

Examples are as follows:

Def get_information(*info): "" "information=[] for item in info: # cycle. Append (item) Return Information # Print (get_information("Daming"  ",21,"student "))Copy the code
Results:Copy the code