Python Learning notes-functions

Source: Internet
Author: User

1: Definition of 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.



2: Function calls and parameter passing


Once you have defined a 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

Copy Code
A = 1
Def Change_integer (a):
A = a + 1
Return a

Print Change_integer (a)
Print a

#=== (the content followed by "#" in Python is commented, not executed)

b = [[+]

def change_list (b):
B[0] = b[0] + 1
Return b

Print Change_list (b)
Print B
Copy Code
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)




3: Summary


def function_name (a,b,c):
Statement
Return something # Return is not a must
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


Python Learning notes-functions

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.