Modular programming - functions

Keywords: Python

Modular programming - functions

What is modular programming?

Modular programming refers to the encapsulation of programs (function encapsulation, object encapsulation, file encapsulation, etc.)

What is a function?

Function - > function
A function is a block of code with a specific function

Function?

Function is to encapsulate the code to improve the reusability of the code, improve the development efficiency, and reduce the later maintenance cost

Definition and use of functions

Function definition format:
    1.The function will not be called or executed in the future
    2.No function can be called before function definition.
    3.The number of calls to a function is not affected
    4.The naming of functions should follow the naming convention
        Alphanumeric underscores cannot begin with numbers
        It is strictly case sensitive and cannot use keywords
        The naming should be meaningful and not in Chinese
        Built in functions do not conflict and are overwritten after conflict

# Define function [basic structure]
def Function name([parameter list]): # [] means not required
    The specific code of the current function
    . . . . 
    def love():
        print('i')
        print('love')
        print('python')
# The function will not be executed after encapsulation, but the function is fixed
# If you want to use a defined function, you need to call the function with syntax
#Function call
 Function name():
    love()
    love()
    love()
    . . . . #The number of calls to a function is not limited
    
    # How is the function defined with the statement

Arguments to function:

  • When defining a function, you can define formal parameters at the position of the list
  • If the function has formal parameters, the parameters (arguments) must be passed when calling
  • The process of transferring parameters to formal parameters is essentially the assignment of variables
def love(w):
    print(f'ilove{w}')

# Arguments need to be passed when calling a function with formal parameters
love('python')

Parameter types in functions

Common parameter, default parameter, collection parameter, named keyword parameter, keyword parameter collection

Common parameters are position parameters, also known as sequence parameters, which must be passed

def func(x,y):
    print(x,y)

func(1,2)

Default parameters

#Three parameters are required, and at least two are passed. The last parameter is the default parameter. The default parameter needs to specify the default value when defining the formal parameter, and the default parameter must be ranked behind the ordinary parameter

def func1(x,y,i=100):
    print(x,y,i)
#No parameters are passed, and the default value of 100 is used when calling
func1(1,2)
#Override default parameters
func1(1,2,3)
def func2(x=100,y,i=100): #As the default parameter, x must be assigned after y, and the function will report an error
    print(x,y,i)

Collect parameters * args

#Define a formal parameter. The redundant arguments of the special mobile phone can be understood as undetermined arguments. They can be directly received with a formal parameter (* args). The collected parameters can be named with any string

def func2(x=0,*args):
    if x==0:
        print('Add',args)
    else:
        print('Subtraction operation',args)

func2(1,2,3,4) #Ages is a tuple. Print the result: subtraction (2, 3, 4)
def func2(x=0,y=2*args):
    pass

Named keyword parameters

# The keyword parameter is defined after the collection parameter. The keyword must be passed through the name of the formal parameter
def love(a,b,c=3,*args,name):
    print(a,b,c,*args)
    print(args)
    print(name)
    
#call
love(1,2,4,2,2,2,name=5)

#When calling ordinary functions, parameters need to be passed in order
#You can also pass ordinary parameters of ordinary functions according to keyword parameters
def love(name,age):
    print(name,age)
#call
love(name=1010,age='aaa')

Keyword parameter collection * * args

def love(a,b,c=3,*args,name,age,**kwargs):
    print(a,b,c)
# Ordinary mobile phone parameters will collect redundant parameters into tuples
    print(args) 
 #In keyword parameter collection, redundant keywords will be collected as dictionaries (key: value)    
    print(kwargs)
    
#call
love(1,2,3,a,b,name='python',age=12,sex='man',id=1)

Placement of all parameters

  • Argument: ordinary argument comes first and keyword parameter comes last
  • Formal parameters:
    1. Keyword collection parameters must appear at the end
    2. Collecting parameters is recommended after common parameters
  • Recommended order: common formal parameters, collection parameters, keyword collection parameters

Function return value

Function can not only meet the function, but also return some required content
The function uses the return keyword to specify the return data, which can return any type of data
The function return value can pass the return data to the caller, who can use variables to receive or judge

Function with no return value

def love():
    print('aaaa')

Specify the return content in the function and use the return keyword

def love(a,b):
    res=f'{a} love {b}'
    return res
print(love('i','python')
#The code after return in the function body will not be executed. Return also represents the end of the function body

def love(x,y):
    res=(x)
    retrun res
    print(y) #This line of code will not be executed after return

Classification of functions:

1. Execute process function: the function body can complete certain functions without return value
2. Return value function: the function body completes certain functions and returns a result to the function call

Posted by david212 on Thu, 04 Nov 2021 11:25:03 -0700