免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2742 | 回复: 2
打印 上一主题 下一主题

一个子程序的返回值问题~ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-12-02 21:35 |只看该作者 |倒序浏览
eg1:
sub myprog1{
    my $sum;
    for(1..9){
        ++$sum;
    }
    $sum;
}

print myprog1;

res:  9

sub myprog2{
    my $sum;
    for(1..9){
        ++$sum;
    }
}

print myprog2;
res: ''(啥都不打印)
按照learning perl 所说如果不加return显式返回值,则返回子程序最后一个表达式的返回值。
第一个是ok的啦。第二个是因为最后运行是for(){}中的‘}’字符(如果它算表达式的话)才不能正确返回么?


初学~别鄙视

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
2 [报告]
发表于 2013-12-03 07:37 |只看该作者
  1. If no return is found and if the last statement is an expression, its value is returned. If the last statement is a loop control structure like a foreach or a while , the returned value is unspecified. The empty sub returns the empty list.
复制代码
http://perldoc.perl.org/perlsub.html

论坛徽章:
0
3 [报告]
发表于 2013-12-03 12:35 |只看该作者
回复 2# rubyish


    谢谢,晓得啦。:wink:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP