···· Function
Consists of a set of code that accomplishes a specific task
Keywords: def
The creation of the function:
def function name (parameter list):
Code (Implementation of the function)
Def printhelloworld ():
For I in range (10):
print ' Hello world '
Call to function:
Function name (parameter)
····· The mechanism of function operation
When calling a function, PVM will find the function's definition position according to the function name, then jump inside the function and execute the internal code of the function once.
EG1:
Def printhelloworld ():
For I in range (10):
print ' Hello world '
Printhelloworld ()
EG2:
def myFunction (s):
For I in range (15):
Print S,
print ' \ n '
Print (' Day depends on mountain ')
MyFunction (' * ')
Print (' Yellow River into the ocean ')
MyFunction (' + ')
Print (' To be poor ')
MyFunction ('-')
Print (' Up to the Heights ')
EG3:
def myFunction (s,n):
For I in range (n):
Print S,
Print
Print (' Day depends on mountain ')
MyFunction (' * ', 10)
Print (' Yellow River into the ocean ')
MyFunction (' + ', 15)
Print (' To be poor ')
MyFunction ('-', 6)
Print (' Up to the Heights ')
··· Where the function is defined
Functions have sequence and scope of action
Functions can also be defined inside another function
Def function1 ():
print ' F1 '
Def function3 ():
print ' F3 '
Function3 ()
Def function2 ():
print ' F2 '
Function1 ()
Can not be called in a random call only inside the function
··· Structure of the function
1. Definition of function (declaration):
def function name (List of functions):
Code (Implementation of the function)
2. Note:
Parameter not defined: no parameter
Multiple parameters: separated by commas
The declaration of a function must have a colon at the end
def add (num1,num2):
result = Num1 + num2
Print result
Add (+)
····· return value of the function
return携带值 一个函数,如果有return语句,return语句后面的值就是返回值,如果return语句后面没有任何值,返回一个None 一个函数,如果没有return语句,系统会默认在函数末尾添加return, def fun1():return 1
Def fun2 ():
Pass
A=FUN1 ()
B=fun2 ()
Print A, b
python--004--function definition