List of articles

    • 1. The concept of function
    • 2. Parameter passing for function calls
    • 3. The return value of the function
    • 4. Function parameter definition
    • summary
    • 1) Concept
    • 2) Why use functions
    • 3) Syntax of functions
    • 1) Case study 1
    • a) Variable position parameters :
    • b) Variable number of keyword parameters :


1. The concept of function

1) Concept

A function is a piece of code that performs a specific task and has completed a specific function

2) Why use functions

Why use functions ?

  • Code reuse
  • Hide implementation details
  • Improve maintainability
  • Improve readability, easy to debug

3) Syntax of functions

Function creation :
def Function name ( Input parameters ):
The body of the function
return

def funtion_name([para1,para2...paraN]):
statement1
    statement2      ....[return value1,value2...valueN]

The function name comes from itself , Entry to input parameters , It's equivalent to the entrance of the fruit machine .

I put the fruit in the fruit machine , And finally there's juice

Case study :

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef calc(a,b):
    c=a+breturn c
result=calc(10,20)print(result)

 Insert picture description here

  • Defining functions requires the use of def key word
  • function_name Represents the function name , The colon on the back (:) Don't forget it , And indent
  • [para1,para2…paraN] Said parameters , It can be omitted , You can also have multiple parameters
  • [return value1,value2…valueN] Represents the return value , There can be no return value , You can also have multiple return values
    It should be noted that custom functions have return values , The main function needs to accept the returned result

2. Parameter passing for function calls

Function transfer refers to the transfer of function calls

Here are three concepts :

  • Shape parameter : For example, in the example above a,b
    The position of the parameter is at the definition of the function

  • Actual parameters : Here's an example of an argument
    result=calc(10,20) # there 10,20 It's a real parameter , The position of the argument is at the call to the function

  • Location reference : It's ours calc(10,20) It's passed on to  Insert picture description here
    This corresponds to a=10, b=20, This is the positional argument .

  • Pass the key words to :

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef calc(a,b):
    c=a+breturn c
result=calc(b=10,a=20) # there 10,20  It's a real parameter , The position of the argument is at the call to the function print(result)

This is based on the variable name to the left of the equal sign b=10 , This b It's a keyword parameter

When we use keyword parameters , Will search according to the keyword name , At this time, the positional argument is not used .

1) Case study 1

The actual parameter name and the formal parameter name can be different :

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef fun(agr1,agr2):
    print('agr1',agr1)print('agr2',agr2)agr1=100
    agr2.append(10)print('agr1',agr1)print('agr2',agr2)n1=10
n2=[11,22,33,44]fun(n1,n2)print('n1',n1)print('n2',n2)

Output results :
 Insert picture description here

there n1 and n2 And the original agr1,agr2 Different , But it doesn't matter , Just as well .

Here are some points to pay attention to :

  • If it's an immutable object , Modify in the body of the function , Does not affect the value of the argument ,agr1 It is amended as follows 100, Does not affect the n1 Value
  • If the variable object , Modifying the function body will affect the value of the argument ,agr2 Modification of , It will affect n2 Value

3. The return value of the function

  • When the function returns multiple values , Result is tuple
#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef fun(num):
    odd=[] #  Save odd even=[] #c An even number for i in num:if i%2:
            odd.append(i)else:
            even.append(i)return odd,even#a Function call lst=[11,22,33,44,55,66,49.80]print(fun(lst))

 Insert picture description here
You can see , The result is a tuple , But there are two lists in the tuple .

The return value of the function :

  • How does a function have no return value ( After function execution , There is no need to provide data for the call out ),return You can omit it
  • The return value of the function , If it is 1 individual , Direct return type
  • If the return value of a function is multiple , The return result is tuple

4. Function parameter definition

  1. Function defines default value parameters : Set default values for parameters , Arguments need to be passed only if they do not match the default values
#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef fun(a,b=10):  #b Is the default value print(a,b)fun(100)

This is the result a=100,b=10

If ab It's all worth it `

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef fun(a,b=10):  #b Is the default value print(a,b)fun(20,30)`

This is the result a=20 ,b=30, This is the time b=30 Does not match the default value , Will pass arguments

  1. The parameters of the function are variable number of position parameters and variable number of keyword parameters

This refers to the number of parameters at the time of definition .

a) Variable position parameters :

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef fun(*agrs): # I don't know how many parameters to pass , This is called *a It's fine too print(agrs)fun(10)fun(10,20)fun(10,20,30)

 Insert picture description here
The final result is tuple

b) Variable number of keyword parameters :

#  author : Internet veteran Xin #  Development time :2021/4/4/0004 6sdef fun(**args): # Two stars print(args)fun(a=10)fun(a=10,b=20,c=30)

The result is a dictionary

summary

Function is still used more , It's in every programming language , So we have to practice more .