Writing rules for functions:
def function name (parameter):
...
function body
...
return value
The definition of a function has the following main points:
- def: A keyword that represents a function
- Function name: the names of functions, which are called in the future according to the function name (similar to a variable)
- function Body: The function of a series of logical calculations, such as: The received data must be a number, to find the maximum value, etc...
- Parameters: Providing data for the function body (providing one or more variables to the function)
- Return value: Once the function has finished executing, it can return data to the caller.
1. Return value
A function is a function block, and the result of the execution of the function is to inform the caller by the return value
2. Parameters
# no parameters
def test1 ():
Print (123)
Test1 ()
# Default Parameters
def test1 (w= ' day1 '):
Print (W)
Test1 ()
# General Parameters
def test2 (W):
Print (W)
Test2 (2)
# Specify parameters
def test3 (w,e):
Print (w,e)
Test3 (e=2,w=1)
# any number of parameters
def test4 (*args):
Print (args)
Test4 (1,2,3,4,5,6)
# character Typical parameters
def test5 (**kwargs):
Print (Kwargs)
Test5 (name= ' Jack ', sex= ' nan ')
Simple notation for functions