A function is a procedure, also called a subroutine, that performs a specified operation or operation. Functions have internal functions or standard functions, external functions that are user-defined functions. Function A block of code used to implement a single function. It can improve application modularity and code reuse rate.

So, the function is a good thing!

A, definitions,

Python functions begin with the def keyword, followed by the function name and parentheses ().

def functionName( arg ):  
  "Docstring"
   pass
   return expression
Copy the code

When the first statement inside a function is a string, that string is a docstring. Docstrings are an important tool for explaining documentation programs for later maintenance.

The difference between docstrings and comments: Documentation docstrings tend to cover the general functionality of functions, while comments tend to cover the details of code implementation.

Once the program is running, there are different levels of namespaces in memory. Python’s namespace is its symbol table. Functions execute using the local symbol table of function variables, and all function variable assignments are stored in the local symbol table. When referencing a variable, first look up the variable in the local symbol table, then in the local symbol table of the outer function, then in the global symbol table, and finally in the built-in name symbol table.

When a function is called, the actual arguments (arguments) are introduced into the local symbol table of the called function. Thus, arguments are passed using value-by-value calls (where the value is always a reference to the object rather than the value of the object). When a function calls another function, a new local symbol table is created for that call.

A function definition associates a function name with a function object in the current symbol table. The interpreter treats the object to which the function name refers as a user-defined function.

Python functions all return a value. If there is no value following a return or no return statement at all, the function returns None

Second, the participation

Functions have three forms of parameter passing that can be used in combination.

1. Pass the default value

Def func1(x, y=2, z=3): return x + y + z print(func1(1)) print(func1(1,2)) print(func1(1,2))Copy the code

The above three methods of calling the function are legal and do not report errors. All three methods return 6

2. Keyword parameter transmission

def func1(x, y=2, z=3) :return x + y + z
print(func1(y=100, x=9, z=4Results:))113
Copy the code

For the same function, there is nothing wrong with the above method, although it changes the order of its arguments, but does not affect the result at all.

But there are a few caveats to this approach:

① Keyword arguments must be followed by positional arguments.

It’s the same function up here

print(func1(100,y=100,z=1)) #// This call will not return an errorResults:201
print(func1(y=100,z=1.100Positional argument cannot appear after keywordarguments
Copy the code

② The same parameter cannot be assigned multiple times

print(func1(10,y=100,z=1,x=100) # get multiple valuesfor argument 'x'
Copy the code

③ The number of parameters cannot exceed the number defined by the function

print(func1(10.100.100.100)) #takes from 1 to 3 positional arguments but 4 were given
Copy the code

④ Do not use parameters that are not defined in the pass function

print(func1(10.100,j=100)) # got an unexpected keyword argument 'j'
Copy the code

The j argument above is not defined in the function and will return an error when called

3. Special parameter transmission


def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
      -----------    ----------     ----------
        |             |                  |
        |        Positional or keyword   |
        |                                - Keyword only
         -- Positional only
Copy the code

/ and * are optional. These symbols show how parameters pass parameter values to functions: position, position, or keyword, keyword. Only positional arguments can be passed before /, and only keyword arguments can be passed after *. Both types of parameters can be passed between/and *.

The first: positional parameters only
def f3(x, y, /):
    return x + y
print(f3(1.2Results:))3
print(f3('a'.'b') result: abCopy the code

An error is reported if called as follows:

print(f3(1, x=2)) #got some positional-only arguments passed as keyword arguments: 'x'
Copy the code
Second: positional parameters only
def f4(*, x, y):
    return x+y
print(f4(x=1, y=2Results:))3
Copy the code

Here are the wrong ones:

print(f4(1.2))#takes 0 positional arguments but 2 were given
Copy the code
Third: the above two ways combined use
def f5(x, /, y, *,z):
    return x+y+z
print(f5(1.2, z=3Results:))6
Copy the code

Summary:

1. Using position-only parameters prevents the user from using the parameter name. This is useful when parameter names have no real meaning, when you force the argument order of a function to be called, or when you accept both positional parameters and keywords.

2. Use keywords only when the parameter name has real meaning and when an explicit name makes the function definition easier to understand, preventing users from relying on where the argument was passed.

3. For apis, using position-only parameters prevents damaging API changes when parameter names are changed in the future.

Note: I am only a beginner in Python. I hope you can forgive me if I make any mistakes! Your comments are also welcome.