python-函数

# 函数
#函数 方法 功能
#说白了,函数就是把一堆代码组合到一起,变成一个整体。
#函数不调用不会被执行
#提高代码的复用性
#全局变量、局部变量
def hello():
print('hello')
#函数不调用不会被执行
hello()#调用函数

def hello(file_name,content=''):#形参,形式参数
#函数里面的变量都是局部变量
f=open(file_name,'a+',encoding='utf-8')
if content:
f.write(content)
else:
f.seek(0)
res=f.read()
return res
f.close
print(hello('a.txt','hh'))#实参,实际参数
print(hello('b.txt'))

#直接写的参数叫位置参数,必填参数
#默认值参数 ,不是必填的---上面的content=''

#返回值
#如果想获取到函数的结果,必须用return
#如果函数没有写return,那么返回值为none,return不是必须写,需要获取到函数的返回结果才写
#return 立即结束函数
#递归:
# 递归 自己调用自己
count = 0
def test1():
# num = int(input('please enter a number:'))
global count
count+=1
num = 8
if num % 2 == 0: # 判断输入的数字是不是偶数
pass
print(count)
return test1() # 如果不是偶数的话继续调用自己,输入值
print(test1()) # 调用test
# 用递归能实现的用循环都能实现,最好别用递归。
#递归最多能递归999次。



posted @ 2018-01-11 17:37  hoby2017  阅读(128)  评论(0编辑  收藏  举报