Chinaunix首页 | 论坛 | 博客
  • 博客访问: 837432
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-07-25 09:59:23

要定义自己的子程序,使用关键字sub,子程序的名字(无&这个符号),组成子程序的缩进的代码块(花括号中),如:
#!/usr/bin/perl

$n = 0;
sub add{
    $n += 1;
    print "Hello, sailor number $n\n";
}

&add;
&add;

sub add_a_b{
    print "the sum is \n";
    $f + $b;
}

sub larger_one{
    if($f > $b){
        $f;
    }else{
        $b;}
}


$f = 3;
$b = 4;
$sum = &add_a_b;
print $sum . "\n";
$larger = &larger_one;
print "the larger one is $larger\n";


阅读(438) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~