大蟒日志6

十、函数3

  1. 返回语句
  2. pass语句
  3. 文本字符串引用
# 2018.4.2
# 1.Return Statement
def maximum(x,y):
    if x > y:
        return x
    elif x == y:
        return'The numbers are equal.'
    else:
        x < y
        return y

print(maximum(-2.5,-2.51))

# same as the following block
def maximum(x,y):
    if x > y:
        print(x)
    elif x == y:
        print('The numbers are equal.')
    else:
        x < y
        print(y)

maximum(-2.5,-2.51)
'''
运行结果: 
-2.5
-2.5

①如果return没有返回值,输出结果相当于None。即return x 返回x的值,return  返回None
②每个函数结尾都隐含了一个return None语句。即:除非你写了一个return有实值的语句,否则
返回无。我们可以通过print一个空白函数来发现这个隐藏的“小秘密”。如下:
'''

# 2.Pass statement
def some_function():
    pass

print(some_function())
'''
运行结果:
None
在函数中引用pass语句,表明定义这个函数的语句块是空白语句块,返回结果自然是None
'''

# 2018.4.3
# Docstrings: Documentation Strings
'''
在之前的日志中,经过多次尝试偶然间发现'''...'''三对单引可以用来注释多行,而后
屡试不爽。这里正篇讲到了文本字符串引用,接下来就仔细学习一下。
'''

def print_max(x, y):
    '''Prints the maximum of two numbers.
    The two values must be integers.'''
# convert to integers, if possible
    x = int(x)
    y = int(y)
    if x > y:
        print(x, 'is maximum')
    else:
        print(y, 'is maximum')

print_max(3, 5)
print(print_max.__doc__)
'''
运行结果:(注意第三行前的tab)
5 is maximum
Prints the maximum of two numbers.
    The two values must be integers.

①在函数中加入三对单引号注释可以在运行时,将注释的内容call出来,以便注释函数功能
②Docstring不仅可以应用在函数中, 也可用在modules和classes中
'''

【声明】本博文为学习笔记,含有个人修改成分,并非完全依照《a byte of python》,仅供参考。若发现有错误的地方,请不吝赐教,谢谢。同时,我也会不定期回顾,及时纠正。#

猜你喜欢

转载自blog.csdn.net/csdner_0/article/details/79820859