Python's Way "fifth" the first knowledge function of Python Foundation

Source: Internet
Author: User
Tags define function

function

I. BACKGROUND

Before learning the function, always follow: process-oriented programming, that is, according to the business logic from the top to the bottom of the implementation of functions, often with a long code to achieve the specified function, the most common development process is to paste the copy, that is, the previous implementation of the code block copied to the current function, as follows:

1  whileTrue:2     ifCPU Utilization > 90%:3         #Send an email reminder4 connecting to a mailbox server5 Send mail6 Close Connection7     8     ifHDD Use space > 90%:9         #Send an email reminderTen connecting to a mailbox server One Send mail A Close Connection -      -     ifMemory Footprint > 80%: the         #Send an email reminder - connecting to a mailbox server - Send mail -Close connection

A look at the above code, the IF condition statement under the content can be extracted out of the public, as follows:

1 defSend mail (content)2     #Send an email reminder3 connecting to a mailbox server4 Send mail5 Close Connection6     7  whileTrue:8     9     ifCPU Utilization > 90%:TenSend Mail ('CPU Alarms') One      A     ifHDD Use space > 90%: -Send Mail ('HDD Alarm') -      the     ifMemory Footprint > 80%:

For the two implementations above, the second must be better than the first reusability and readability, in fact, this is the difference between functional programming and process-oriented programming:

    • Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called
    • Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."

Functional programming The most important thing is to enhance the reusability and readability of code

Ii. Definition and use

1 def function name (parameter): 2        3     ... 4     function Body 5    ... 6     return value

The definition of a function has the following main points:

    • def: A keyword that represents a function
    • Function name: the names of functions, which are called later by function name
    • Parameters
    • Function Body: A series of logical calculations in a function, such as sending a message, calculating the maximum number in [11,22,38,888,2], etc...
    • Parameters: Providing data for the function body
    • Return value: Once the function has finished executing, it can return data to the caller.

1. Return value

A function is a function block that executes successfully or is required to inform the caller by the return value.

In the above points, it is more important to have parameters and return values: 1 def Send SMS ():

2 Send SMS Code ...3     ifSend success:4         returnTrue5     Else:6         returnFalse7    8  whileTrue:9     #each time a send SMS function is executed, the return value is automatically assigned to resultTen     #after that, the log can be written according to result, or the operation will be re-sent . Oneresult =Send SMS () A     ifresult = =False: -Logging, SMS Sending failed ...

Number of return values = 0: Return None

Number of return values = 1: Return object

Number of return values >1: return tuple

2. Parameters

    • 1. parameter variables allocate memory cells only when they are called, freeing allocated memory units at the end of the call. Therefore, the formal parameter is only valid inside the function. Function call ends when you return to the keynote function, you can no longer use the shape parametric
    • 2. arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when making a function call, they must have a definite value in order to pass these values to the parameter. It is therefore necessary to use the assignment, input and other methods to get the parameters to determine the value

There are three different parameters to the function:

    • General parameters
    • Default parameters
    • Dynamic parameters
        • Defining functions (formal parameters and arguments) General arguments
  1  #   # # # ###### define function #########   2   3  #   name is called the formal parameter of function func, abbreviation: formal parameter   4  def   func ( Name):   5  print ( name) 
6 7 #  8#  ' xiaoming ' is called the actual parameter of function func, abbreviation: argument 9 func ('xiaoming  'ten -one common parameter
        • Specifying parameters and Default parameters

1 #name is called the formal parameter of function func, abbreviation: parameter2 #def func (name, age=18): #age is the default parameter if no parameter is specified below, the default parameter is used directly3 #print ("%s:%s"% (name, age))4 #5 #6 ## Specify Parameters7 #func (' Wupeiqi ', +) #19 overrides default parameters8 ## using default parameters9 #func (' Alex ')
        • Dynamic parameters

def func (*args):    print(args)#  execution mode one func (11,33,4,4454,5 #  execution mode two li = [11,2,2,3,3,4,54]func (*li)#  Dynamic Parameters 
defFunc (* *Kwargs):Print(Kwargs)#execution Mode oneFunc (name ='Wupeiqi', age=18)#mode of execution twoLi = {'name':'Wupeiqi'," Age": 18,'Gender':'male'}func (**Li)#Dynamic Parameters

Python's Way "fifth" the first knowledge function of Python Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.