Concise Python tutorial-7. Functions

Source: Internet
Author: User

Concise Python tutorial --- 7. Functions

 

A function is a reusable statement block. Many languages allow defining functions to specify a name for a statement block, and reuse the statement block countless times in other places of the program.

In python, the def keyword is used to define functions. The def keyword is followed by the function name, followed by a pair of parentheses. Variable names can be included in parentheses, which end with a colon. Next is a statement, that is, the function body.

Def sum (A, B ):

Return A + B;

Result = sum (1, 2 );

Print 'result = ', result;

Function Parameters

When defining a function, the parameter name in parentheses is a form parameter. When calling a function, the value passed to the function is called a real parameter.

Local variable

Variables defined in the function cannot be accessed outside the function. That is to say, the scope of such variables is limited to the internal scope of the function. Such variables are called local variables.

Def sayhello ():

STR = "hello ";

Print STR;

Sayhello ();

Global

If you want to access the variable declared inside the function outside the function, the variable must be declared as global.

Def func ():

Global;

Print "A =",;

A = 100;

Func ();

Default parameter value

Def func (a, B = 2 ):

S = A + B;

Print S;

Func (1 );

Func (1, 2 );

Note: parameters with default values must be at the end of the parameter list; otherwise, an error occurs.

Key parameters

The key parameter is a call method, which is not explained. Here is an example:

Def func (a, B = 5, c = 10 ):

Print 'a = ', A,' B = ', B, 'c =', c

Func (3, 7 );

Func (25, c = 24 );

Func (C = 50,a = 100 );

The execution result is:

A = 3 B = 7 C = 10

A = 25 B = 5 c = 24

A = 100 B = 5 c = 50

Return Statement

Like C/C ++, Java, and C #, python uses the return statement to end the execution of a function and return values from the function.

In the method where the return statement is not displayed, return is placed in all the returned places by default.

The use of return statements has been shown in the first example in this chapter.

Docstring (document string)

Specify that if the first logical line of the function body is a string, the string is the document string of the function.

The document string can be accessed by adding "." and "_ Doc _" to the function name.

Def func ():

"This is docstring ";

Print func. _ Doc __;

Note that the document string language feature can also be used for modules and classes.

 

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.