Python 函数。函数的定义。函数的参数、返回值。函数嵌套

demo.py(函数定义):


# say_hello()  # 不能在定义函数之前调用函数

# Python 解释器知道下方定义了一个函数
def say_hello():
    """函数的说明文档"""
    print("hello 1")
    print("hello 2")
    print("hello 3")

print("调用函数之前")

# 只有在程序中,主动调用函数,才会让函数执行
say_hello()

print("调用函数之后")

demo.py(函数的参数、返回值):

def sum_2_num(num1, num2):
    """对两个数字的求和"""
    result = num1 + num2
    return result  # 通过return返回结果

# 可以使用变量,来接收函数执行的返回结果
sum_result = sum_2_num(10, 20)

print("计算结果:%d" % sum_result)

demo.py(函数的嵌套):

def test1():

    print("*" * 50)


def test2():

    print("-" * 50)

    # 函数的嵌套调用
    test1()

    print("+" * 50)

test2()

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/83867342
今日推荐