What is a function?
A function of a computer is a fixed program segment, or a subroutine. It can realize fixed operation functions and also has an entry and an exit. The so-called entry is the function. , we can use this entry to substitute the parameter values of the function into the subprogram for computer processing; the so-called exit refers to the function value of the function, which is brought back to the calling program after the computer obtains it.
Use the features of functions: reduce code duplication, program extensibility, and easy maintenance.
Syntax Definition:
1 # @Software: PyCharm 2 def say(): #Define function 3 print ( ' hello world ' ) 4 5 say() #Call function say 6 7 8 9 hello world 10 11 Process finished with exit code 0
# @Software: PyCharm a,b = 5,2 def calc(x,y): res = x**y return res c = calc(a,b) print(c) 25 Process finished with exit code 0
Parameters of the function:
Formal parameter: A variable is allocated a memory unit only when it is called, and the memory unit is released when the call ends, that is, it is only valid inside the function.
Argument: must have a definite value, pass the value to the formal parameter.
# @Software: PyCharm a,b = 5,2 # a,b actual parameters def calc (x,y): # x,y are formal parameters res = x** y return res c = calc(a,b) #The actual parameter is passed to the formal parameter print (c) 25 Process finished with exit code 0