Python: initial function

Source: Internet
Author: User

Python: initial function

Functions are well-organized, reusable pieces of code that are used to implement a single, or associated function.

Functions can improve the modularity of the application, and the reuse of the code. You already know that Python provides a number of built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions.

Definition of the function:
    # in the definition phase, the function detects only the syntax and does not execute code.    def function name (parameter 1, parameter 2,...)        : " " Comment Information " "         function Body         return   value        defines three forms of function        :# function does not need to pass in parameters externally                 # function body code, need to pass the external value of the time only use the parameter # structure placeholder           

Call to function:
    Function name ()  #* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * function         call              Print(1)            return 123                 is called directly from the function name:        foo ()                is assigned to other variables, An indirect call to        res=foo        ()print(res)            takes advantage of the function return value to call        res=foo () *10        Print(res)

The return value of the function returns:
    When the function is called, the function is terminated as soon as it executes to return. and to return the content as the return value of the call returned to the    function body can have multiple return, but only one return value can be executed    , may be any type, can be separated by commas, in the form of Ganso return multiple values    can not write return, Default returns None

Parameters of the function:
parameters: Parameter arguments specified when the function is defined: parameter invariant parameters used during function call: Default parameter: During the definition phase, the parameters are already assigned, and there are issues that can be noted without passing values during the call phase: #1. Value of default parameter, assign value only once when defined                #2. The value of the default parameter, which should be an immutable type                #3. The default parameter must be written to the right of the non-default parameter, to the left of the non-fixed parameter        defFoo (y=10)            Print(y) foo () positional parameters:#positional parameters are explained during the definition phase.        deffoo (x, y, z)Print(x, y, z) foo ()#the default invocation method for positional parameters, also known as the necessary argumentfoo (y=2,z=3,x=1)#positional parameters can be called key arguments at the call stagenon-fixed parameters:*args:#receive any number of positional parameters that are not defined in the formal parameter, and save them in tuples**kwargs:#receive any number of keyword arguments that are not defined in the formal parameter and save them in the dictionary

   Order of formal parameters
Positional parameters *args, default parameters, **kwargs

defFUNC3 (*args, * *Kwargs):Print(args)Print(Kwargs) func3 (1, 2, 3,'Alex', C=6, Name='Wusir', age=' +')(1, 2, 3,'Alex'){'C': 6,'name':'Wusir',' Age':' +'}

def func3 (*args, **kwargs):  # The definition of the function when * represents the aggregation.     print(args)    print(Kwargs) func3 (*[1, 2, 3],* (+))   # The execution of the function * stands for beating.  (1, 2, 3, +) {}

defFunc3 (*args, **kwargs):#when the function is defined, * represents the aggregation.     Print(args)Print(Kwargs) func3 (**{'name':"Alex"},**{' Age': 23})#The execution of the function * stands for beating. (){'name':'Alex',' Age': 23}

Python: initial function

Related Article

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.