Perl学习笔记 之 [ 函数, 参数, @_, $_, $_[0], shift ]
2013-04-26 00:00
435 查看
1. Perl函数
通过 & 调用.
2. Perl参数
Perl天然支持可变数目个参数。
在函数内部,所有参数按顺序放在数组 @_ 中。
在函数内部,$_[0] 表示函数的第一个参数。其余类推。
3. shift
shift 后跟一个数组,表示将数组的第一个值返回。数组也被改变,其第一个元素被弹出。
演示代码一(求最大值):
#!/usr/bin/perl -w
use strict;
# 调用函数max,取得一组数值的最大值,并输出。
my $maxValue = &max(11,22,33);
print "maxValue=$maxValue\n";
sub max {
# 采用遍历算法。先将参数中的第一个值赋给$currentMaxValue。
# @_ 是默认的包含本函数所有参数 [如(11,22,33)]的数组。
# shift @_ 有两个结果: 1. 将数组 @_ 中的第一个值做为返回值(赋给了$currentMaxValue). 2. 将@_数组第一个值弹出[此后@_的值变为(22,33)].
my $currentMaxValue = shift @_;
# 函数中使用shift时,@_可以省略。上面代码也可以写成这样。
# my $currentMaxValue = shift;
# 遍历整个@_数组。
foreach ( @_ ) {
# $_ 表示数组@_中当前被遍历到的元素.
if ( $_ > $currentMaxValue ) {
# 如果发现当前数组元素比$currentMaxValue大,那就将$currentMaxValue重新赋值为当前元素。
$currentMaxValue = $_;
}
}
# 函数返回值为标量$currentMaxValue.
return $currentMaxValue;
}
演示代码二(求和):
#!/usr/bin/perl -w
use strict;
# 求一组数的和并打印。
my $s1 = &sum1(11,22,33);
my $s2 = &sum2(22,33,44);
my $s3 = &sum3(11,22,33,44,55);
print "s1=$s1, s2=$s2, s3=$s3\n";
# 办法1
sub sum1 {
# 将参数数组的前三个元素值相应地赋给($first, $second, $third)
(my $first, my $second, my $third) = @_;
# 返回其和值。缺点: 如果是求四个参数的和,依然只能给出前三个的和。
return $first + $second + $third;
}
# 办法2
sub sum2 {
# $_[0] 表示参数数组@_的第一个元素。其余类推。
my $first = $_[0];
my $second = $_[1];
my $third = $_[2];
# 返回其和值。缺点: 同sum1. 只是通过这里学习 $_[0] 这种用法。
return $first + $second + $third;
}
# 办法3, 参数可以任意多。都能求其和。
sub sum3 {
my $s = shift @_;
foreach ( @_ ) {
$s = $s + $_;
}
# 同前面函数max。
return $s;
}
通过 & 调用.
2. Perl参数
Perl天然支持可变数目个参数。
在函数内部,所有参数按顺序放在数组 @_ 中。
在函数内部,$_[0] 表示函数的第一个参数。其余类推。
3. shift
shift 后跟一个数组,表示将数组的第一个值返回。数组也被改变,其第一个元素被弹出。
演示代码一(求最大值):
#!/usr/bin/perl -w
use strict;
# 调用函数max,取得一组数值的最大值,并输出。
my $maxValue = &max(11,22,33);
print "maxValue=$maxValue\n";
sub max {
# 采用遍历算法。先将参数中的第一个值赋给$currentMaxValue。
# @_ 是默认的包含本函数所有参数 [如(11,22,33)]的数组。
# shift @_ 有两个结果: 1. 将数组 @_ 中的第一个值做为返回值(赋给了$currentMaxValue). 2. 将@_数组第一个值弹出[此后@_的值变为(22,33)].
my $currentMaxValue = shift @_;
# 函数中使用shift时,@_可以省略。上面代码也可以写成这样。
# my $currentMaxValue = shift;
# 遍历整个@_数组。
foreach ( @_ ) {
# $_ 表示数组@_中当前被遍历到的元素.
if ( $_ > $currentMaxValue ) {
# 如果发现当前数组元素比$currentMaxValue大,那就将$currentMaxValue重新赋值为当前元素。
$currentMaxValue = $_;
}
}
# 函数返回值为标量$currentMaxValue.
return $currentMaxValue;
}
演示代码二(求和):
#!/usr/bin/perl -w
use strict;
# 求一组数的和并打印。
my $s1 = &sum1(11,22,33);
my $s2 = &sum2(22,33,44);
my $s3 = &sum3(11,22,33,44,55);
print "s1=$s1, s2=$s2, s3=$s3\n";
# 办法1
sub sum1 {
# 将参数数组的前三个元素值相应地赋给($first, $second, $third)
(my $first, my $second, my $third) = @_;
# 返回其和值。缺点: 如果是求四个参数的和,依然只能给出前三个的和。
return $first + $second + $third;
}
# 办法2
sub sum2 {
# $_[0] 表示参数数组@_的第一个元素。其余类推。
my $first = $_[0];
my $second = $_[1];
my $third = $_[2];
# 返回其和值。缺点: 同sum1. 只是通过这里学习 $_[0] 这种用法。
return $first + $second + $third;
}
# 办法3, 参数可以任意多。都能求其和。
sub sum3 {
my $s = shift @_;
foreach ( @_ ) {
$s = $s + $_;
}
# 同前面函数max。
return $s;
}
相关文章推荐
- Perl学习笔记 之 [ 函数, 参数, @_, $_, $_[0], shift ]
- Python中函数参数设置及使用的学习笔记
- 【Python学习笔记】函数参数
- Flex与As3学习笔记之:Part 3 函数参数、字符串处理、日期与时间类型
- JavaScript学习笔记8-多个参数的函数使用
- 函数的参数 学习笔记
- C++ Primer 学习笔记_21_函数 --函数定义、参数传递
- Scala 学习笔记(三)------定义具有可变参数的函数
- C语言学习笔记-----伊能---函数可变参数
- Python学习笔记(四)-- 函数中的参数传递
- C语言学习笔记 (005) - 二维数组作为函数参数传递剖析
- 【C++学习笔记】函数基础和参数传递
- Python3学习笔记05-字典、文件输入输出、模块、函数参数传递
- C语言学习笔记.指针3--指针变量作为函数参数
- python函数参数学习笔记
- 函数参数Windows程序设计_Chap01_起步_学习笔记
- GO学习笔记 - 函数名前面是否有输入参数肯定是不一样的!!
- Python学习笔记之一: 函数参数
- Swift学习笔记(三十)——函数的内部参数名和外部参数名
- 【Python】学习笔记——-3.3、函数的参数