Function foundation
Function: a function is an organized, reusable code segment used to implement a single or associated function.
Function can improve the modularity of application and the reuse rate of code. Python provides many built-in functions, such as print(). You can create your own functions, which are called user-defined functions.
Defining functions
The function code block begins with the def keyword, followed by the function identifier name and parentheses ().
Any incoming parameters and arguments must be placed between parentheses. Parentheses can be used to define parameters.
The first line of the function optionally uses the document string - used to hold the function description.
The function contents start with a colon and are indented.
return [expression] ends the function and optionally returns a value to the caller. return without an expression is equivalent to returning None
Syntax:
def functionname( parameters ): Function document string function_suite return [expression]
Example:
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 def add_function(x,y): #Defined function 5 6 """ 7 This is a function that adds x and y. #Function document 8 """ 9 10 result =x + y 11 12 return result # Return value 13 14 if __name__ =="__main__": #__Name is the current module name. When the module is run directly, the module name is main. This means that when the module is run directly, the following code block will be run, and when the module is imported, the code block will not be run. 15 16 a = 4 17 b = 9 18 c = add_function(a,b) #Call function or C = add UU function (x = a, y = b) 19 20 print "a + b = {0} + {1} ={2}".format(a,b,c)
Function naming:
Use lowercase letters and underscores to form two words. Use underscores to connect "add" function
Hump nomenclature the first word is lowercase and the next is capitalized
Call function
Reduce the difficulty of programming, and decompose a complex big problem into a series of simpler small problems.
Code reuse
Example:
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 def fibs(n): 5 6 """ 7 This is the Fibonacci sequence. 8 """ 9 10 result =[0,1] 11 for i in range(n-2): 12 result.append(result[-2]+result[-1]) 13 14 return result 15 16 if __name__ =="__main__": 17 18 lst=fibs(10) 19 20 print lst
Other examples:
1 >>> def my_fun(): #Define a function 2 return 1,2,3 3 4 >>> a =my_fun() #Returns a tuple 5 >>> a 6 (1, 2, 3) 7 >>> type(a) 8 <type 'tuple'> 9 >>> b,c,d =my_fun() #Define a function and return multiple values 10 >>> b 11 1 12 >>> c 13 2 14 >>> d 15 3 16 >>> x,y=1,2 17 >>> x 18 1 19 >>> y 20 2 21 >>> def my_fun(): #Define a function without a return value 22 print "this is my function" 23 24 25 >>> a =my_fun() 26 this is my function 27 >>> a 28 >>> print a 29 None 30 >>> my_fun 31 <function my_fun at 0x0000000003BCF518> 32 >>> my_fun() 33 this is my function 34 >>> def my_fun(): 35 print "before return" 36 return #return encountered. Subsequent operations are not being performed 37 print "after return" 38 39 >>> my_fun() 40 before return 41 >>>
Function documentation
Function. Doc view document
help(function) view document