2021-09-17--python function with less product

A little python knowledge a day

Does it not smell when you add up more???
Section 15

Function Base-01

Definition of function

Function's role: A function is to assemble a block of code with independent functions into a whole and name it. Calling this name where it is needed will accomplish the corresponding requirements, avoid code reuse, and make the program execute more efficiently

Steps to use the function:
a. Define functions:
Def function name (parameter)# def keyword, parameter, optional or nonexistent
Code 1
Code 2

def sel_func():  # Function calls, which need to be defined and then called, define the function and then the role required within the function
    print("balance")
    print("deposit")
    print("Withdraw money")
    print("Help")
# Once a function is defined, it can be called


sel_func()  # 2 rows down without yellow waves
# Result:
balance
 deposit
 Withdraw money
 Help

Be careful:
1. Functions need to be defined before they can be called, which will cause errors
2. If no function is called, only the function is defined, and the internal code within the function body will not execute
3. Function execution process:
A. If a function call is encountered, the interpreter will return to the location of the function definition to execute the internal code within the function (indented code below)
b. When the code is executed, it returns to the location where the function was called and proceeds down

Function parameters, add operations with function calls, pass in real data as parameters

def num1(x, y):  # x, y and the following call function location should be one-to-one, the number of consistent
    # When defining a function, the parameters x and y that receive user data are defined. x and y are parameters
    print(x)
    print(y)
    z = x + y
    print(z)


num1(9, 10)  # Calling the function passed in real data 9 and 10, with real data as parameters
# Result:
9
10
19

Classification of parameters
1. Positional parameters: When calling a function, parameters are passed based on the position of the parameters defined by the function


def a(name, age, gender):
    print(f"Your name is{name}, Age is{age}, Gender is{gender}")


a("xli", 28, "female")  # Note the order (the parameters correspond from left to right) and the number (the numbers must be consistent)
# Result:
Your name is xli, Age is 28, gender is female

2. Keyword parameters: Function calls are determined by the form of "key" = "value", which makes the function clearer and easier to use.


def b(name, age, gender):
    print(f"Your name is{name}, Age is{age}, Gender is{gender}")


# b(name="xiao", age=23, gender="female")
# b(age=23, gender="female", name="xiao")  # Keyword parameters are passed in an order-independent manner and do not affect
# Result:
Your name is xiao, Age is 23, gender is female


3. The default parameter, also known as the default parameter, provides the default value for the parameter and does not pass the value of the default parameter when the function is called


def c(name, age, gender="female"):  # The default sex is female when called without passing on. If you need to change it, you can continue to pass on
    print(f"Your name is{name}, Age is{age}, Gender is{gender}")


c(name="Zhang San", age=23)  # No default parameter, default to female using default value
# Result:
Your name is Zhang San, your age is 23, your gender is female
c(name="Zhang San", age=23, gender="male")  # Modify default values with default parameters
# Result:
Your name is Zhang San, your age is 23, your gender is male
c("Zhang San", age=23, gender="male")  # If it is a combination parameter, the position parameter must be written before the keyword
# Result:
Your name is Zhang San, your age is 23, your gender is male
c(age=23, "Zhang San", gender="male")  # Failure to run results will result in an error

4. Indefinite-len gt h parameters: also known as variable parameters, used to wrap keyword parameters or location parameters for parameter delivery when you are not sure how many parameters will be passed - > group packages

Parcel location parameters for parameter delivery


def d(*args):  # Plus* followed by args(args can be changed, as long as the naming convention is fine)
    print(args)


d("Zhang San")
d("Zhang San", 45)
d("Li Si", 45, "Mr. Zhang San")
d("King Five", 23, "Note: Want to eat black")
# Result:
('Zhang San',)
('Zhang San', 45)
('Li Si', 45, 'Mr. Zhang San')
('King Five', 23, 'Note: Want to eat black')

Package keyword parameters for parameter delivery --. Returns a dictionary type


def e(**kwargs):  # Add ** (two*) followed by kwargs(kwargs can be changed, just name the specification)
    print(kwargs)


e(name="Zhang San")
e(name="Zhang San", age=45)
e(name="Li Si", age=45, relation="Mr. Zhang San")  # Relationship means: relation ship
# Result:
{'name': 'Zhang San'}
{'name': 'Zhang San', 'age': 45}
{'name': 'Li Si', 'age': 45, 'relation': 'Mr. Zhang San'}

Return value of function
return effect
a. Responsible for the return value of the function, returning the result to the user (where the result is returned to the function call)
b. If a return is encountered, it will be returned directly. The content behind the body of the function under return will not be executed


def bug():
    return "Single dog"


print(bug())
# Result:
Single dog

Keywords: Python Go pygame

Added by misty on Fri, 17 Sep 2021 02:46:11 +0300