Python basic functions

Source: Internet
Author: User

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

The most important purpose of the function is to make it easy for us to reuse the same program.

Some operations are subordinate to a function, and in the future when you want to implement the same operation, you can simply invoke the function name, without having to repeatedly knock all the statements.

Definition of a function

First, we'll define a function to illustrate the function.

Def square_sum (A, B):    c = a**2 + b**2    return c

The function is to find the sum of squares of two numbers.

First, Def, this keyword informs Python: I'm defining a function. Square_sum is the function name.

A in parentheses, B is the parameter of the function, and is the input to the function. Arguments can have more than one or none at all (but the parentheses remain).

We have seen in loops and choices the affiliation of the colon and the indented representation.

c = a**2 + b**2 # This sentence is an operation inside a function

Return C # Returns the value of C, which is the function of the output. Python's functions allow for no return value, i.e. not returning.

Return can return multiple values, separated by commas. is equivalent to returning a tuple (a fixed value table).

Return A,b,c # equals return (A,B,C)

In Python, when the program executes to return, the program stops executing the remaining statements within the function. Return is not required, and the function will automatically return none when there is no return, or there are no returned values after return. None is a special data type in Python that indicates nothing, equivalent to NULL in C. None is used for the default value passed by the keyword parameter.

function calls and parameter passing

Once you have defined the function, you can use it in later programs

Print Square_sum (3,4)

Python passes the location, knowing that 3 corresponds to the first parameter A in the function definition, 4 corresponds to the second parameter B, and then passes the argument to the function square_sum.

(Python is rich in parameter passing, keyword passing, table passing, dictionary passing, etc., the basic tutorial will only involve location passing)

The function is calculated and returns a value of 25, and the 25 is printed by print.

Let's take a look at the following two examples

A = 1def Change_integer (a):    a = a + 1    return aprint Change_integer (a) print a#=== (the content followed by "#" in Python is commented, not executed) b = [1 , 2,3]def change_list (b):    b[0] = b[0] + 1    return bprint change_list (b) Print B

In the first example, we pass an integer variable to the function, and the function operates on it, but the original integer variable a does not change.

In the second example, we pass a table to the function, the function is manipulated, and the original table B is changed.

For variables of the base data type, after the variable is passed to the function, the function copies a new variable in memory without affecting the original variable. (We call this a value pass)

But for a table, the table is passed to the function as a pointer to the position of the sequence in memory, and the operation of the table in the function will be in the original memory, thereby affecting the original variable. (We call this a pointer pass)

Summarize
def function_name (A,B,C):    statement    return something # return is not required

Purpose of the function: to increase the repetition of the program's availability.

Return None

Pass the parameter through the position.

Parameters for the base data type: value passing

Table as parameter: pointer passing

Practice:

Write a function that determines the leap year, with the parameters for the years, months, and days. If it is a leap year, return True

Python basic functions

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.