Python = = "function

Source: Internet
Author: User

A. Ask a question, why do you have a function?

1. Make the program simple

2. Improve the readability of your code

Definition of the function:
def function name (parameter 1, parameter 2):    Comment of---function---    print (' function body ')    return ' value '
Call to function:
return value = function name (parameter 1, parameter 2)
Parameters of the function: arguments and formal arguments. (Class two)

Argument: The value of the parameter passed in when the function is called.

Formal parameter: The name of the parameter defined when the function is defined.

Parameters: Divided into three categories.

1. The parameter can be any data type. 2. You can pass 0 or more parameters.

Position parameters:

Defining parameters sequentially

Default parameters:

First, do not set the default parameter for a mutable type, and you can set a default parameter for a parameter that has little variation.

If passed, overrides the default value of the default value is set before the function definition is OK.

Dynamic Parameters:
   *args **kwargs
Arguments to invoke a function: pass the parameter by location:
Def My_max (A, b):    print (A, b) My_max (10,20)
Follow the key word parameters:
def My_max (    A, A, b): Print (A, A, My_max (a), a=20)
The return value of the function:

Keyword for return value: return

The function of return:

1. Ending the execution of a function

2. Return the value you want to return

Two cases of return value:

One. The return value is None

There are three cases where the return value of the function is none.

1. Do not write the return value

2. Write only a return

3.return None (almost unused)

1. Do not write the return valuereturndeffunc1 (): a= 111b= [A]ret=func1 ()Print(ret)#2.returndefFunc2 (): a= 111b= [A]    returnret=Func2 ()Print(ret)#3. Returndeffunc3 (): a=111b= [A]    returnNoneret=func3 ()Print(ret) The return value of these three cases is none. 
View Code

Two. The return value is not none

1. Returns a value

Receive with a variable

2. Return multiple values

Received with a variable, the result is a tuple

There are multiple return values that are received in the number of variables.

    

  

Python = = "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.