python---def函数

python—def函数

如何写

函数可以有无限个参数

def name(self1,self2,self3,......):

如何编写下级代码

	xxxxxx
	xxxxxx
	xxxxxx
	return xxxxxx

return

运行完return,这个函数就不会执行了后面的下级代码了。

def r(h):
	return h
	h += 1 #不会执行
	return h #同样不会执行

如何应用

如果你要编译,就得写上这个参数的名字写在下面

修改刚才的代码

def r(h):
	return h
	h += 1 #不会执行
	return h #同样不会执行
r(7)

输出
7

任务

你能不看源代码,做一个double函数,用一个参数,填进去就输出这个数的双倍值
是这样子吗?

def double(h):
	return h + h
double(8)

输出
16

或者这样:

def double(h):
	return h * 2
double(8)

输出
16

拓展

def创造的函数可以用print命令多行输出,而return是单行输出(除非你用\n换行符)

def double(h):
	print(h * 2)
	print(h)
double(8)

输出:
16
8

小结

今天我们知道了def来创造函数,还知道了return作返回值,通过拓展我们知道了print是多行输出,而return是单行输出。

发布了15 篇原创文章 · 获赞 14 · 访问量 247

猜你喜欢

转载自blog.csdn.net/niu1024/article/details/105117841