📜  珀尔 |子程序或函数 |套装 – 2

📅  最后修改于: 2022-05-13 01:54:34.134000             🧑  作者: Mango

珀尔 |子程序或函数 |套装 – 2

先决条件:Perl 中的子例程或函数

Perl函数或子例程是一组共同执行特定任务的语句。在每种编程语言中,用户都希望重用代码。因此用户将这段代码放在函数或子程序中,这样就不需要一次又一次地编写代码。在本文中,我们将讨论以下概念:

  • 将哈希传递给子程序
  • 将列表传递给子程序
  • 从子程序返回值
  • 子程序中的局部变量和全局变量
  • 子程序调用中不同数量的参数

将散列传递给子例程:散列也可以传递给子例程,子例程会自动转换为其键值对。
例子:

Perl
# Perl program to demonstrate the
# passing of hash to subroutines
 
#!/usr/bin/perl
 
# Subroutine definition
sub Display_hash {
    
   # hash variable to store
   # the passed arguments
   my (%hash_var) = @_;
    
   # to display the passed list elements
   foreach my $key (keys %hash_var )
   {
      my $val = $hash_var{$key};
      print "$key : $val\n";
   }
}
 
# defining hash
%hash_para = ('Subject' => 'Perl', 'Marks' => 97);
 
# calling Subroutine with hash parameter
Display_hash(%hash_para);


Perl
# Perl program to demonstrate the
# passing of lists to subroutines
 
#!/usr/bin/perl
 
# Subroutine definition
sub Display_List {
    
   # array variable to store
   # the passed arguments
   my @para_list = @_;
    
   # to print the passed list elements
   print "Given list is @para_list\n";
}
 
# passing scalar argument
$sc = 100;
 
# passing list
@li = (10, 20, 30, 40);
 
# Calling Subroutine with scalar
# and list parameter
Display_List($sc, @li);


Perl
# Perl Program to demonstrate the
# returning values subroutine
 
#!/usr/bin/perl
 
# subroutine definition
sub Sum {
     
   # To get total number
   # of parameters passed.
   $num = scalar(@_);
   $s = 0;
 
   foreach $i (@_)
   {
      $s += $i;
   }
    
   # returning sum
   return $s;
}
 
# subroutine calling and storing result
$result = Sum(30, 2, 40);
 
# displaying the result
print "Sum of the given numbers : $result\n";


Perl
# Perl program to demonstrate the Local
# and Global variables in subroutine
 
#!/usr/bin/perl
 
# A Global variable
$str = "GeeksforGeeks";
 
# subroutine definition
sub Geeks {
     
   # Private variable by using my
   # keyword for Geeks function
   my $str;
    
   $str = "GFG";
   print "Inside the Subroutine: $str\n";
}
 
# Calling Subroutine
Geeks();
 
print "Outside the Subroutine: $str\n";


Perl
# Perl program to demonstrate the variable
# number of parameters to the subroutine
 
#!/usr/bin/perl
 
use strict;
use warnings;
 
# defining subroutine
sub Multiplication {
     
    # private variable containing
    # default value as 1
    my $mul = 1;
     
    foreach my $val (@_)
    {
        $mul *= $val;
    }
     
     
    return $mul;
}
 
# Calling subroutine with 4 parameters
print Multiplication(8, 2, 3, 4);
 
print "\n";
 
# Calling subroutine again but
# with 3 parameters
print Multiplication(3, 5, 4);


输出:
Marks : 97
Subject : Perl

将列表传递给子程序:我们知道@_是函数或子程序中的一个特殊数组变量,因此它用于将列表传递给子程序。 Perl 有一种不同的方式来接受和解析数组和列表,这使得从@_中提取离散元素变得很困难。为了将列表与其他标量参数一起传递,有必要将列表作为最后一个参数。
例子:

Perl

# Perl program to demonstrate the
# passing of lists to subroutines
 
#!/usr/bin/perl
 
# Subroutine definition
sub Display_List {
    
   # array variable to store
   # the passed arguments
   my @para_list = @_;
    
   # to print the passed list elements
   print "Given list is @para_list\n";
}
 
# passing scalar argument
$sc = 100;
 
# passing list
@li = (10, 20, 30, 40);
 
# Calling Subroutine with scalar
# and list parameter
Display_List($sc, @li);
输出:
Given list is 100 10 20 30 40

从子程序返回值:子程序也可以像在其他编程语言中一样返回一个值,如 C、C++、 Java等。如果用户不手动从子程序返回一个值,那么子程序将自动返回一个值。在这种情况下,自动返回的值将是子程序中执行的最后一次计算。返回值可以是标量、数组或散列。
例子:

Perl

# Perl Program to demonstrate the
# returning values subroutine
 
#!/usr/bin/perl
 
# subroutine definition
sub Sum {
     
   # To get total number
   # of parameters passed.
   $num = scalar(@_);
   $s = 0;
 
   foreach $i (@_)
   {
      $s += $i;
   }
    
   # returning sum
   return $s;
}
 
# subroutine calling and storing result
$result = Sum(30, 2, 40);
 
# displaying the result
print "Sum of the given numbers : $result\n";
输出:
Sum of the given numbers : 72

子程序中的局部变量和全局变量:默认情况下,Perl 程序中的所有变量都是全局变量。但是在my关键字的帮助下,您可以在块内创建局部或私有变量。私有变量的范围有限,例如在块(if、while、for、foreach 等)和方法等之间。在块或方法之外,这些变量不能使用。
例子:

Perl

# Perl program to demonstrate the Local
# and Global variables in subroutine
 
#!/usr/bin/perl
 
# A Global variable
$str = "GeeksforGeeks";
 
# subroutine definition
sub Geeks {
     
   # Private variable by using my
   # keyword for Geeks function
   my $str;
    
   $str = "GFG";
   print "Inside the Subroutine: $str\n";
}
 
# Calling Subroutine
Geeks();
 
print "Outside the Subroutine: $str\n";
输出:
Inside the Subroutine: GFG
Outside the Subroutine: GeeksforGeeks

子程序调用中不同数量的参数: Perl 没有为我们提供任何内置工具来声明子程序的参数,这使得向函数传递任意数量的参数变得非常容易。
例子:

Perl

# Perl program to demonstrate the variable
# number of parameters to the subroutine
 
#!/usr/bin/perl
 
use strict;
use warnings;
 
# defining subroutine
sub Multiplication {
     
    # private variable containing
    # default value as 1
    my $mul = 1;
     
    foreach my $val (@_)
    {
        $mul *= $val;
    }
     
     
    return $mul;
}
 
# Calling subroutine with 4 parameters
print Multiplication(8, 2, 3, 4);
 
print "\n";
 
# Calling subroutine again but
# with 3 parameters
print Multiplication(3, 5, 4);
输出:
192
60

注意:通常,将多个数组或散列作为参数传递给子程序会导致它们失去各自的身份。类似地,从子程序返回多个数组或散列也会导致失去它们各自的身份。我们可以通过引用来解决这些问题。