python base - Function (1)

1.1 Introduction to function

The so-called function is to block the organization having individual functions as a small module, called when needed

Function uses two steps comprising:

  1, the definition of the function - the package independent function

  2, call the function - Enjoy the outcome of the package (if it is called in another py file, you need to use the import function to import py file contains the original keyword can use the function call)

Function returns: In the development, use function can improve the efficiency and the reuse of code written in

 

1.1.1 defined function

Format function is as follows:

def function name (): 
    function code package 
    .....

1, def is the abbreviation define the

2, should be capable of expressing a function name of the function code function package, to facilitate subsequent calls

3, function names should be named in line with naming identifiers

Identifier naming rules:

  · Contain letters, underscores and numbers

  · Can not begin with a number

  · Must be unique keyword

1.1.2 Function calls

1, call the function has been written in the original py file, you only need to complete a call to a function by function name

def hello_world():
    """打招呼"""
    print("hello world")


hello_world()

 

If you are calling a function in another file, you need to use the import keyword import raw py file, then the file name. Format calling function name

import Hello

Hello.hello_world()

Note: The calling function can not be above the function definition

Because before the function call using the function name, we must ensure that python already known to exist functions

Otherwise, the console will prompt NameError: name 'function name' is not defined

 

1.1.3 Function documentation comments

  • In development, if you want to increase or decrease the function comment should be defined functions below, using three consecutive quotes
  • In three consecutive quotation marks to write captions of function between
  • In the function call position using pycharm shortcuts ctrl + q can view a description of the information function

Note : Because the body is relatively independent of the function, the function defined above, should be, and other code (including comments) two blank lines

 

2.1 Function parameters

 When calling the function, we need certain values ​​to internal functions

2.1.1 Use of function parameters

  • Fill in the parameters inside the parentheses after the function name
  • A plurality of parameters separated by commas

 

def sum_2_num(num1,num2):
    result = num1 + num2
    print("%d" % result )

       
sum_2_num(50, 100)

 

 

 Effect parameters 2.1.2

  •  Function, the organization code blocks having individual functions as a small module, called when needed
  • Function parameters, increasing or decreasing function of versatility, processing logic for the same data, it is possible to adapt more data
  1.  Inside the function, the variables as parameters, required for data processing
  2. Function call, the function defined in accordance with the order parameter, the data processing inside the function desired, through argument

 2.1.3 formal and actual parameters

  • Parameter: defining a function, the argument in parentheses, is used to receive the parameters used as variables inside a function
  • Arguments: the function is called, parentheses parameter is used to pass data to the internal function with

 2.1.4 return value of the function

  • Program development, sometimes want to perform a function after the end, tell the caller a result, for the caller to do follow-up treatment for concrete results
  • The return value is a function after the completion of the work, a final result to the caller
  • Use the return keyword to return results in the function
  • Calling party function variables can be used to accept function returns
DEF sum_2_num (num1, num2):
     "" " of the two digital sum " "" 
    result = + num1 num2
     return result 


# calling function, receiving and using the result variable calculation 

result = sum_2_num (50, 100 )
 Print ( " % D " % Result)

 

Note: return means return, also represents the end of a function, the subsequent return code will not be executed

 

2.1.5 nested function calls

  • A function which in turn calls another function, which is nested function calls
  • If the function test2, call another function test1
  1.   So when you call the test1 to execute a function, the function will first test1 in executing the task
  2.         Will return to test2 calls test1 function of position, continue to implement the follow-up code test2

 

DEF test1 ():
     Print ( " * " * 50 )
     Print ( " the Test 1 " )
     Print ( " * " * 50 ) 


DEF test2 ():
     Print ( " - " * 50 )
     Print ( " test2 " ) 
    test1 () # call the test1 function, after executing the test1 back to the position 
    Print ( " - " * 50 ) 


test2 ()

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/alone-wolf/p/11869753.html