python-- function basis

Function returns

The most important function is to facilitate our role to reuse the same period of the program. After the operation you want to achieve the same time, for example, you need to add a function or modify a function, only need to add or modify function, call the function name can be performed.

 

Defined Functions

 First, let's define a function

def name_1(x,y):
    z=x+y
  return z 

using python def keyword to declare defined functions, name_1 function name, x and y are parameters, it is an input to function. You can have multiple parameters, you can also completely (but want to keep the brackets).

There are three caveats:

  1. The function name must be a colon
  2. If the function body and def not in the same line, it must be indented
  3. return function return value specified for the end of the function. But the return statement is optional, if not to return, this is equivalent to a plus return Nonethat the function returns the default None structure

 

Function calls and parameter passing

After been defined function, this function can be used later in the program

print name_1(3,4)

Python by position , is a function of the corresponding known 3 definitions first parameter a, 4 corresponding to the second parameter B, and then pass parameters to the function name_1.

(Python has extensive parameter passing, as well as key transfer, transfer table, dictionaries and other transfer, based tutorial will only involve the transfer position)

After calculation function, the return value of 25, the print 25 is printed out.

 

We look at the following two examples

Copy the code
. 1 = A DEF change_integer (A): 
    A = A +. 1
     return A Print change_integer (A)
 Print A # === (in the Python "#" and is followed by a comment, not performed) 
B = [1,2, . 3 ] DEF change_list (B): 
    B [0] = B [0] +. 1
     return B Print change_list (B)
 Print B










Copy the code

The first example, we will be a integer variable passed to the function , the function to operate it, but the former does not change a variable integer .

The second example, we put a sheet transfer function to operate, the function, the original tables b changes .

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

But for tables, the table passed to the function is a pointer to the memory location in the sequence, the operation will be performed on the original table in the memory function, thereby affecting the original variables. (We call this pointer is passed )

 

to sum up

DEF FUNCTION_NAME (A, B, C): 
    Statement 
    return something   # return is not necessary

Function Objective: To improve the availability of repeat procedures.

return     None

Passing position, passing parameters.

Basic data types of the parameters: value passed

Table as a parameter: pointer is passed 

 

 

Function calls and parameter passing

After been defined function, this function can be used later in the program

print square_sum(3,4)

Python by position , is a function of the corresponding known 3 definitions first parameter a, 4 corresponding to the second parameter B, and then pass parameters to the function square_sum.

(Python has extensive parameter passing, as well as key transfer, transfer table, dictionaries and other transfer, based tutorial will only involve the transfer position)

After calculation function, the return value of 25, the print 25 is printed out.

 

We look at the following two examples

Copy the code
a = 1

def change_integer(a):
    a = a + 1
    return a

print change_integer(a)
print a

#===(Python中 "#" 后面跟的内容是注释,不执行 )

b = [1,2,3]

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

print change_list(b)
print b
Copy the code

第一个例子,我们将一个整数变量传递给函数,函数对它进行操作,但原整数变量a不发生变化

第二个例子,我们将一个表传递给函数,函数进行操作,原来的表b发生变化

对于基本数据类型的变量,变量传递给函数后,函数会在内存中复制一个新的变量,从而不影响原来的变量。(我们称此为值传递

But for tables, the table passed to the function is a pointer to the memory location in the sequence, the operation will be performed on the original table in the memory function, thereby affecting the original variables. (We call this pointer is passed )

 

to sum up

DEF FUNCTION_NAME (A, B, C): 
    Statement 
    return something   # return is not necessary

Function Objective: To improve the availability of repeat procedures.

return     None

Passing position, passing parameters.

Basic data types of the parameters: value passed

Table as a parameter: pointer is passed

Guess you like

Origin www.cnblogs.com/huajiu/p/11607106.html