在Python中,定义一个函数要使用def语句,依次写出函数名,括号,括号中的参数和冒号:,
然后,在缩进块中编写函数体,函数的返回值用return 语句返回。
我们自定义结构求绝对值的my_abc函数示例:
# -*- coding: UTF-8 -*-
def my_abs(x):
if x >= 0:
return x
else:
return -x
print my_abs(20)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/t1.py
20
/***************
# -*- coding: UTF-8 -*-
def my_abs(x):
if x >= 0:
return x
else:
return
print my_abs(-20)
if my_abs(-20):
print 1111111
else:
print 2222222
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/t1.py
2222222
# -*- coding: UTF-8 -*-
def my_abs(x):
if x >= 0:
return x
else:
return
print my_abs(-20)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a5.py
None
# -*- coding: UTF-8 -*-
# -*- coding: UTF-8 -*-
def my_abs(x):
if x >= 0:
return x
else:
return
print my_abs(-20)
if my_abs(-20):
print 1111111
else: