C ++ ape's Python note 03-Function

Source: Internet
Author: User
Basic Data Type Integer, long integer, floating point, plural, string

 

Variable

You do not need to declare or define a type. You can assign a value when needed.

 

Function Definition

Def function name (parameter list)

You do not need to declare or define the type when using a variable. Therefore, the function does not need to define the return type.

By default, none is returned.

 

Notes for Functions

1. the parameter is a value transfer

2. assign values to external variables and use the global keyword

Def Func (x ):
Print   ' X is ' , X
X =   2
Print   ' Changed local X ' , X
X =   50
Func (X)
Print   ' Value of X is ' , X

3. Support for default parameters

Def Say (message, times =   1 ):
Print Message * Times
Say ( ' Hello ' )
Say ( ' World ' , 5 )

4. Key parameters are supported. That is, the parameter of the specified real parameter.

Def Func (a, B = 5 , C = 10 ):
Print   ' A is ' ,, ' And B is ' , B, ' And C is ' , C
Func ( 3 , 7 )
Func ( 25 , C = 24 )
Func (C = 50 , = 100 )

Output

>>>  
A Is   3   And B Is   7   And C Is   10
A Is   25   And B Is   5   And C Is   24
A Is   100   And B Is   5   And C Is   50
>>>  

5. Pass indicates an empty statement block.

Def Somefunction ():
Pass

6. docstrings

If the first logical line after function definition is a string, it is the document string of this function.

The description statement that can be called. The call method isFunction name. _ Doc __

The document string convention is:

A multi-line string whose first line starts with an uppercase letter and ends with a full stop.

The second line is empty,

The detailed description starts from the third line. 

Def Printmax (x, y ):
''' This is docstrings.

Return Max number.'''
X = INT (X) # Convert to integers, if possible
Y = INT (y)
If X > Y:
Print X, ' Is maximum '
Else :
Print Y, ' Is maximum '
Printmax ( 3 , 5 )
Print Printmax. _ Doc __


 

 

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.