Notes python function (beginner)

1, define a function

def  max(num1,num2) :   #函数头
	if num1>num2:
		return num1     #函数体
	else :
		return num2

Functions include functions head and body of the function, the function header DEF begin, brackets including parameter,
and ends in a colon
parameter as a placeholder, the value will be passed to the parameter when the calling function
the function parameters may not be

2, call a function
Note: 1, with the return value of the function can also be called as a statement, in this case, the function returns the value will be ignored.
2, the function is called in the memory, a function can be defined at any location in the script.

3, the call stack
each time the function is called, the system creates an activation record is stored as a function of its parameters and variables, and then activate the record on this memory area is known as a stack.
Call stack is called the execution stack, or stack operation, is often referred to as the stack.

When a function calls another function, activation record of the caller remains unchanged, and then create a new activation record for the new function calls. When a function finishes its work and will program transfers control to its caller, and remove it from the stack of activation records.

Stack uses advanced out of the way after the storage activation record

When heap object is not required, python will automatically be cleared of these objects

6.4 ## return value and without return value of the function
Note:
1, regardless of whether the use of return, all python function will return a value. If a value does not return a value, in the case of default, it returns a special value None.
Thus no return value of the function does not return a value, which is also referred None function. None function can be assigned to a variable to indicate that this variable does not point to any object.

None function to end a function and returns control to the caller

returnreturn  None

Example:

def printgrade():
	if score>100 or score <0
		return None

6.5, positional parameters and keyword parameters:

Two types of arguments: positional parameters and keyword arguments

Location parameters:

def nprintln(message,n):
	for i in range(n):
		print(message)

May be used nprintln ( 'a', 3) to a transfer to the messge, the third transmission to n, then a output three times
while nprintln (3, 'a') is a transfer to n, is passed to the Message 3
position parameters the argument must, sequence, and number match the type of the parameter defined in the function head.

Keyword parameters:
passing each parameter name = value by way of
example:

nprintln(n=5,message = 'a')使用关键字参数可以以任意的顺序出现

Keyword parameters and position parameters can be mixed, but the position parameter can not follow any keyword parameters
, for example:

def f(p1,p2,p3):
	f(30,p2=4,10)调用它会出错,因为位置参数10出现在关键字参数之后

6.6 to pass parameters passed by reference:

Key: When the function is called with a parameter, reference values ​​for each argument is passed to the parameter

The term & passed by value

数据通常是对象,对象的变量通常指向对象的引用
调用函数时,实参的值就被传递给形参。这个值通常就是对象的引用值。
***重点:所有变量指向的都是对象,他们的地址都是对象的地址

Numbers and strings are referred to immutable objects. Can not change the content of the object is not changed
*** A new digital assignment to a variable time, python will create a new object to a variable.

不可变对象的地址是不变的,每个变量指向的同一个变量的地址是相同的
if __name__ == '__main__':
    c= 1
    d= 2
    a = 1
    b = 2
    print(id(a),id(b),id(c),id(d))
    print(a,b)
``
结果:
8791529935936 8791529935904 8791529935904 8791529935936
Published 25 original articles · won praise 3 · Views 498

Guess you like

Origin blog.csdn.net/qq_44045101/article/details/101549787