Last active
December 15, 2021 09:59
-
-
Save rozer007/9cd368edb6e7476e9726e51fe339f2c7 to your computer and use it in GitHub Desktop.
Subroutines in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1.Do we need to define the return type of the subroutines in perl? | |
ans: No, we don't need to define the return type in perl. | |
Q2.In which array does the arguements receive by the subroutine are stored? | |
ans: All the arguments recieve by the subroutines is stored in @_ array. | |
Q3.How do we define a default value for an argument in a subroutine? | |
ans: syntax: $var_name ||=value; | |
Q4.What is a state variable ? | |
ans: This variable maintain the state and doesn't reinitialized upon multiple calls of the subroutines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1. what will be the output of this statement: | |
sub increment { | |
state $execute_total = 0; | |
$execute_total++; | |
return "Executed $execute_total times"; | |
} | |
increment(); | |
increment(); | |
print increment(); | |
a) 1 | |
b) 3 | |
c) 1 2 3 | |
d) none of the above | |
ans: b) | |
Q2.What will be the output: | |
sub fun { | |
my @num_array = @_; | |
$_ *= 2 for @num_array; | |
return @num_array; | |
} | |
my @rand_array = (2,2,2,2,2); | |
print join("1", fun(@rand_array)); | |
a) 2121212121 | |
b) 22222 | |
c) 4141414141 | |
d) 4,4,4,4,4 | |
ans:c) | |
Q3.What will be the output: | |
sub fun { | |
my$num = @_; | |
return $num/1.2; | |
} | |
print join("1", int(fun(45))); | |
a) 1 | |
b) 01 | |
c) 0 | |
d) 145 | |
ans: c) | |
Q4.What will be the output of this statement: | |
sub fun { | |
my$num = @_; | |
$num||=90; | |
return $num/1.2; | |
} | |
print join("1", int(fun())+1); | |
a) 70 | |
b) 76 | |
c) 66 | |
d) none of the above | |
ans: b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment