Function understanding of Python

Keywords: Python Back-end

Function: in short, it is a code block that encapsulates functions.

Functions:
1. Structured programming is the most basic encapsulation of code, which generally organizes a piece of code according to function
2. The purpose of encapsulation is to reuse and reduce redundant code
3. The code is more concise, beautiful and readable

Function definition:

#def function name (parameter list):
#Function body (code block)
#[return value]
# def keyword, define, when defining
def add(x, y): #add identifier, function name, pointing to a function object. 
#(parameter list). The parameter list can have n parameters (can be 0). These parameters are called formal parameters, abbreviated as formal parameters. Formal parameters are also identifiers
	result = x + y  # Functional logic
	return result  # Return return value

add(10, 100) # Adding () after the function identifier means calling and executing called. When called
#(10100) real parameters are filled in to become actual parameters, which are referred to as arguments for short. When called
remove(100, 200) # Calling remove will report NameError because you have not defined the identifier of this function

print(callable(add)) # callable is a built-in function used to test whether a function can be called and returns True or false

Python is a dynamic language. All types can be universal. Any type can be brought into the add function for return. Other languages have requirements for types, and sometimes they have to overload several times.
Function parameters
When defining a function, formal parameters should be defined, and sufficient actual parameters should be provided when calling. Generally speaking, the number of formal parameters and actual parameters should be consistent
(except for variable parameters).
Parameter transfer mode
1. Position transfer parameter
When defining def f(x, y, z), call f(1, 3, 5) and pass in arguments in the order of parameter definition

2. Keyword parameters
When defining def f(x, y, z), call f(x=1, y=3, z=5) and use the name of the formal parameter to pass in the method of the argument. If the formal parameter name is used
Word, then the order of parameter passing can be different from the order of definition
The location parameter must be passed in before the keyword parameter. The location parameter corresponds to the location

def add(x, y):
	print(x)
	print(y)
	print('-' * 30)
add(4, 5)
add(5, 4) # In order, the x and y values are different
add(x=[4], y=(5,))
add(y=5.1, x=4.2) # Keyword parameters are transferred by name, regardless of order
add(4, y=5) # correct
add(y=5, 4) # Error transmission parameter

Remember: parameter passing refers to the actual parameters passed in during the call. There are two ways.

3. Default value of formal parameter
The default value is also called the default value. You can add a default value to the formal parameter during function definition. Its function:
*The default value of a parameter can be assigned as the default value to a parameter that is not given when not enough arguments are passed in.
*When there are many parameters, you don't need to input all the parameters every time to simplify the function call

def login(host='localhost', port=3306, username='root', password='root'):
	print('mysql://{2}:{3}@{0}:{1}/'.format(host, port, username, password))

login()
login('127.0.0.1')
login('127.0.0.1', 3361, 'zhangsan', 'zhangsan')
login('127.0.0.1', username='zhangsan')
login(username='zhangsan', password='zhangsan', host='192.168.1.10')

The definition of default value is put back

def add(x, y=4):  ###correct
	print(x, y)

def add(x=4, y):  ###error
	print(x, y)

4. Variable parameters
*args is a variable location parameter. It can only receive arguments passed in by location. It can accept 0 or any of them. There is no default value

def add(*args):
	sum = 0
	for x in args:
		sum += x
		return sum

print(add(1, 3, 5, 7, 9))
print(add(2, 4, 6, 8, 10))

**kwargs is a variable keyword parameter. It can only receive arguments passed in by keywords. It can accept 0 or any of them. The last parameter. There is no default value

def config(**kwargs)
	for k, v in kwarg.items():
		print('{}={}'.format(k, v))


config(host='127.0.0.1', port=3306, user='root', password='root123')

Keyword only parameter, after * args, can only receive keyword passed in arguments. Default value, regardless of before and after

def foo(*args, x, y)
	print(x, y, args)

foo(1, x=2, y=3)

Positional only parameters, which appear after 3.8 and before / can only receive arguments passed in by positional parameters

def fn(a, b, /)
	print(a, b)

fn(1, 2)
fn(a=1, b=2) ###report errors

5. Function return value

Python functions use a return statement to return a return value
All functions have return values. If there is no return statement, return None is implicitly called
The return statement is not necessarily the last statement in the statement block of the function
A function can have multiple return statements, but only one can be executed. If no return statement is executed
return None is implicitly called when the
If necessary, you can display and call return None, which can be abbreviated as return
If the function executes a return statement, the function will return, and other statements after the currently executed return statement will not be executed
All right
Function of return value: end function call and return "return value"

Posted by ereimber on Tue, 26 Oct 2021 23:51:07 -0700