码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础四

时间:2016-11-01 14:03:35      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:color   blog   odi   过程   rom   pre   testing   style   返回   

函数和过程

# -*- coding: utf-8 -*-
#函数
def func1():
    """testing1"""
    print(‘in the func1‘)
    return 0

#过程  即没有返回值的函数
def func2():
    """testing2"""
    print(‘in the func2‘)

x=func1()
y=func2()

print(‘from func1 return is %s‘%x)
print(‘from func2 return is %s‘%y)

 

以上代码运行结果

in the func1
in the func2
from func1 return is 0
from func2 return is None

 

函数调用

例一:

# -*- coding: utf-8 -*-
def test(x,y):
    print(‘x=%d‘%x)
    print(‘y=%d‘%y)

test(y=2,x=1)#关键字调用,与形参顺序无关

 

以上代码运行结果

x=1
y=2

 

例二:

# -*- coding: utf-8 -*-
def test(x,y):
    print(‘x=%d‘%x)
    print(‘y=%d‘%y)

test(1,2)   #位置参数调用,实参与形参位置一一对应

 

以上代码运行结果

x=1
y=2

 

例三:

# -*- coding: utf-8 -*-
def test(x,y,z):
    print(‘x=%d‘%x)
    print(‘y=%d‘%y)


test(3,z=2,y=6)#关键字要放在位置参数的后面

 

以上代码运行结果:

x=3
y=6

 

Python基础四

标签:color   blog   odi   过程   rom   pre   testing   style   返回   

原文地址:http://www.cnblogs.com/xone/p/6018834.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!