Novices learn python (5) functions

1) Definition: A function refers to encapsulating a set of statements with a name (function name). To execute this function, you only need to call its function name.

2) The benefits of using functions: 1. Simplify the code; 2. Improve the reusability of the code; 3. The code can be extended

3) For example:

def sayhello(): #Use the keyword def to define a function, followed by the function name

    print('hello world') #function body

sayhello() #call function

4) Parameter passing

def calc(a,b): #a,b are formal parameters, formal parameters

    res=a*b

    print('%s * %s=%s'%(a,b,res))

calc(7,8) #actual parameters, actual parameters

Note: The formal parameter variable allocates the memory unit only when it is called. At the end of the call, the allocated memory unit is released immediately. Therefore, the formal parameters are only valid inside the function.

5) Four types of formal parameters

1. Positional parameters: The literal meaning is to pass parameters according to the position of the parameters. For example the above calc function, a and b are positional parameters. Positional parameters are required to be passed. Pass a few, otherwise it will report an error.

2. Default parameter, the default parameter is to assign a value to the function by default when the formal parameter is defined. E.g:

def op_file(file_name,content=None): #content is the default value parameter, not required 
f = open(file_name,'a+',encoding='utf-8')
f.seek(0)
if content: #No Empty means write
f.write(content)
f.flush()
else:
all_user=(f.read()) #all_user local variable, can only be used in the function
return all_user #After calling the function, return the result
f.close( )

3. Variable parameters, variable parameters are received with *, it is not necessary to pass parameters, and the number of parameters is not limited.
def syz(*args): #Variable parameters, that is, parameter group 
print(args)
# username=args[0] #When there are multiple parameters, take the data according to the subscript
syz()
syz('zhangsan','35435345')

4. Keyword parameters, use ** to receive, no parameters must be passed, no limit on the number, the passed parameters are placed in the dictionary
def syz2(**kwargs): #Keyword parameters 
print(kwargs)

syz2()
syz2(name='zhangsan',age=38)
syz2(name='zhangsan',age=38,addr='Huilongguan',home ='Sichuan')

6) The return value of the function

Each function has a return value. If the return value is not specified in the function, after the function is executed in python, it will return a None by default. The function can also have multiple return values. Put the return value into a tuple, and the return is a tuple.

The reason why there is a return value is that after this function is completed, its result needs to be used in subsequent programs.

The return value in the function uses return, and the function ends immediately when it encounters return.

def calc(a,b):

    c=a*b

    return c,a,b #function return value

res=calc(5,6) #Assign the return value of the function to res

print(res)

7) Local variables and global variables

A local variable means that it takes effect locally. When the scope of the variable is exceeded, the variable will become invalid. For example, the above c is a local variable. After the function is out, there is no value of c.

The meaning of global variables is to take effect in the whole program. All global variables are defined at the front of the program. If global variables are to be modified in a function, they need to be declared with the global keyword. If they are lists, dictionaries and sets, You do not need to add the global keyword, you can modify it directly.

name ='zhangsan' #Global variable 
def sayname():
global name #If you want to modify the global variable, you need to declare it, the value you modify is the global variable
name='lisi'
print(name)
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066809&siteId=291194637