Basic Python Tutorial-functions

Source: Internet
Author: User
The most important purpose of a function is to make it easier for us to reuse the same program. Some operations are affiliated to a function. when you want to implement the same operation in the future, you only need to call the function name. The most important purpose of the function is to make it easier for us to reuse the same program.

Some operations are affiliated to a function. when you want to implement the same operation in the future, you only need to call the function name, without repeating all the statements.

Function definition

First, we need to define a function to describe the function.

def square_sum(a,b):    c = a**2 + b**2    return c

This function is used to calculate the sum of squares of two numbers.

First, def, this keyword notifies python: I am defining a function. Square_sum is the function name.

A and B in parentheses are the parameters of the function and are the input of the function. There can be multiple or none of the parameters (but the brackets should be retained ).

We have seen the affiliation expressed by colons and indentation in the loop and selection.

C = a ** 2 + B ** 2 # The return c # operation in the function returns the value of c, that is, the output function. Python functions allow no return value, that is, no return. Return can return multiple values separated by commas. Returns a tuple (value table ). Return a, B, c # is equivalent to return (a, B, c)

In Python, when the program executes return, the program stops executing the remaining statements in the function. Return is not required. if there is no return, or there is no return value after return, the function will automatically return None. None is a special data type in Python, used to indicate nothing, equivalent to NULL in C. None is mostly the default value used for passing keyword parameters.

Function call and parameter transfer

After defining a function, you can use this function in subsequent programs.

Print square_sum (3, 4)

Python knows through location that 3 corresponds to the first parameter a and 4 in the function definition, corresponding to the second parameter B, and then passes the parameter to the function square_sum.

(Python has rich parameter passing methods, including keyword passing, table passing, and dictionary passing. The basic tutorial will only involve location passing)

After calculation, the function returns Value 25, which is printed by print.

Let's look at the following two examples.

A = 1def change_integer (a): a = a + 1 return aprint change_integer (a) print a #== (in Python, "#" is followed by a comment, do not execute) 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. the Function operates on it, but the original integer variable a does not change.

In the second example, a table is passed to the function for operations. The original table B changes.

For a variable of the basic data type, after the variable is passed to the function, the function will copy a new variable in the memory, so as not to affect the original variable. (We call this value transfer)

However, for a table, the table is passed to the function as a pointer pointing to the position of the sequence in the memory. operations on the table in the function will be performed in the original memory, thus affecting the original variables. (We call this pointer transfer)

Summary

Def function_name (a, B, c): statement return something # return is not required

Function purpose: to improve the repeated availability of the program.

Return None

Pass parameters by location.

Parameter of the basic data type: value transfer

Table as a parameter: pointer transmission

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.