Python Learning path Basic Article--09python Foundation, Primary knowledge function

Source: Internet
Author: User

Functions can be broken down into built-in functions and custom functions. The main focus of this time is the custom function. Once the function is defined, it can be called wherever it is needed.

1 Importance of the return value

3 cases of Return value

No return value----return None

Indefinite return

Write only return: End a function

Return None----not used

Only one return value

Can return any data type

You can receive it as soon as you return.

If there is more than one return in a function, only the first one is executed

Return multiple values

Receive with multiple variables: How many variables are received with the number of return values

Receive with a variable: get a tuple

2 parameters

Formal parameters:

Actual parameters:

No parameters

No content is written in parentheses when defining functions and calling functions

has one parameter

Pass what is what

has multiple parameters

Position parameters

At the angle of the argument:

Pass the parameter by location

Follow the key word parameters

Mixing is possible, but you must first pass the parameter according to the location, and then follow the keyword; you cannot pass multiple values to the same variable

Standing on the angle of the formal parameter:

Positional parameters: Must pass, and have several parameters to pass several values

Default parameters: Can not pass, if not pass the default parameters, pass the Pass

Only when the function is called:

Pass by location: The value of the direct write parameter

by keyword: keyword = value

When defining a function:

Positional parameters: Defining parameters directly

Default parameter, keyword argument: parameter name = ' Default value '

Dynamic parameters: Can accept any number of parameters

Before the parameter name plus *, used to use *args (received by the value of the location parameter, organized into a tuple)

Before the parameter name plus * *, used to use **kwargs (received by the key to the value of the parameter, organized into a dictionary)

Order: Positional parameters, *args, default parameters, **kwargs

At the angle of the parameter, add a * to the variable, which is the combination of all the passed values.

At the angle of the argument, adding * to a sequence is to break the sequence in order

3 Comment of the function

def func ()

‘‘‘‘

what function does this function implement

Parameter 1: ...

Parameter 2: ...

Return: What is the returned thing

‘‘‘

Python Learning path Basic Article--09python Foundation, Primary knowledge function

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.