There are following types of the functions based on their parameters and return values:
根据函数的参数和返回值,有以下几种类型的函数:
Function with no argument and no return value
没有参数也没有返回值的函数
Function with no argument but return value
没有参数但返回值的函数
Function with argument and no return value
有参数且无返回值的函数
Function with arguments and return value
带参数和返回值的函数
1)没有参数也没有返回值的函数 (1) Function with no argument and no return value )
def sum():
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c=a+b
print("Sum :", c)
def main():
sum()
if __name__=="__main__":
main()
Output
输出量
Enter A: 10
Enter B: 20
Sum : 30
2)没有参数但返回值的函数 (2) Function with no argument but return value )
def sum():
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c=a+b
return c
def main():
c = sum()
print("Sum :",c)
if __name__=="__main__":
main()
Output
输出量
Enter A: 10
Enter B: 20
Sum : 30
3)有参数且无返回值的函数 (3) Function with argument and no return value)
def sum(a,b):
c=a+b
print("Sum :", c)
def main():
a = int(input("Enter A: "))
b = int(input("Enter B: "))
sum(a,b)
if __name__=="__main__":
main()
Output
输出量
Enter A: 10
Enter B: 20
Sum : 30
4)带参数和返回值的函数 (4) Function with arguments and return value)
def sum(a,b):
c=a+b
return c
def main():
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = sum(a,b)
print("Sum :",c)
if __name__=="__main__":
main()
Output
输出量
Enter A: 10
Enter B: 20
Sum : 30
翻译自: https://www.includehelp.com/python/function-classifications-on-the-basis-of-parameters-and-return-values.aspx