The basic function of using Python

First, the basic function is used

1. What is the function

Definition: function is a combination of a series of functions, it is a reusable tool.

2, why should function

1, to prevent redundant code
2, to prevent organizational structure is not clear, poor code readability
difficult to 3, without using the code to manage the maintenance of the great

3, how to use the function

1, defined functions to create tools ---- >>>
2, calling functions >>> ---- tools
emphasize: the use of the function must be defined before, after calling

4-defined functions

4.1 Syntax

def 函数名(参数1,参数2,......):
    """
    文档描述
    """
    代码块1
    代码块2
    代码块3
    return 值

def:定义函数的关键字
函数名:是用来调用函数的,函数名的命名必须能反映出函数的功能
"""文档描述""":描述函数代码体的功能,增强可读性
代码块:函数的功能,用来实现代码
return:函数的返回值
定义函数:
def func():
    print('hello world')
    
调用函数:
func():

4.2, the definition of three types of functions:

There are function parameters : the parameter is a function of the caller to the intermediary transfer function value thereof, if you need to define the logic function body dependent parameters transmitted to the outside has a function of parameters

def max(x,y):
    if x>y:
        print(x)
    else:
        print(y)
max2(100,101)

No reference function : when the calling code logic function body does not require incorporation of a function value, no parameters. When no parameters defined, meaning there is no need passed into the function when called.

Empty function : the function body to Pass

def func():
    pass
func()

5, call the function

5.1 Principles call function

That is the function name in parentheses calls the function
definition phase : the definition phase detection only the syntax, the code does not execute the function body
calls stages : Locate the memory address of the function according to the function name, and then execute the code function body

定义阶段:
def foo():
    print('from foo')
    soo()
def soo():
    print('from soo')
    
调用阶段:
foo()

执行结果:
from foo
from soo

Conclusion: The definition phase and the function foo soo no syntax errors, and call foo call the stage (), the function foo and soo are already present in memory, so there will not be any problems.

5.2 calls a function of three forms

1、语句形式:
foo()

2、表达式形式:
x=my_min(1,2) #将调用函数的返回值赋值给x
y=10*my_min(1,2) #将调用函数的返回值乘以10的结果赋值给y

3、函数调用作为参数的形式:
def max(x,y):
    if x>y:
        return x
    else:
        return y
res  = max(1,max(2,3))
print(res)

6, the return value of the function

6.1 Why should there be a return value:

There is a need to return the results to the caller after the function body code is finished running.

6.2 returns the value of several forms:

1、没有return:默认返回None

2、只写return:只有结束函数体代码的效果,返回None

3、写return None: 与只写return的效果一样

4、return后跟一个值,返回该值本身

5、return可以用逗号隔开,返回多个值,返回的多个值存入一个元组返回


注意:
    1.return返回值的值,没有类型限制,可以自己指定返回的数据类型
    2.返回的值不能被修改
    3.函数内可以写多个return,return执行一次,函数就立刻结束,并把return后的值作为本次调用的返回值

Guess you like

Origin www.cnblogs.com/baohanblog/p/12143033.html