Enhance your understanding of Perl subroutines and function calls with this quiz, which covers syntax, calling conventions, argument passing, return values, and best practices for defining and using functions in Perl scripts.
Which keyword is used in Perl to define a subroutine named 'greet' that takes no arguments?
Explanation: The correct keyword for defining a subroutine in Perl is 'sub'. This is followed by the subroutine name and a code block. 'def', 'func', and 'function' are not valid Perl keywords; they are used in other programming languages but have no meaning for Perl subroutine definitions.
How do you correctly call a Perl subroutine named 'compute_sum' with two arguments: 5 and 10?
Explanation: Function calls in Perl use parentheses to enclose arguments, so 'compute_sum(5, 10);' is correct. The square, curly, and angle brackets, as in the other options, are either not valid for function calls or have different meanings in Perl.
In a Perl subroutine, how are arguments accessed inside the subroutine body?
Explanation: Arguments passed to a Perl subroutine are available in the special array '@_'. '$arguments' and 'argv[]' are not used for this purpose in Perl, while '%ARGS' is not automatically set by the language. Only '@_' provides the functionality needed for argument access.
What is the primary method for returning a value from a Perl subroutine?
Explanation: The 'return' statement is used to send a value from a subroutine back to the caller. Assigning to '$_' or just printing output does not return a value, and simply exiting the code block with no return value may lead to undefined results or default behavior.
If a Perl subroutine does not explicitly use 'return', what does it return by default?
Explanation: When 'return' is omitted, Perl returns the value of the last evaluated expression within the subroutine. It does not default to undefined, zero, or nothing; instead, it always uses the last expression's value, which differs from how some other languages handle this.
How do you define a Perl subroutine 'hello' that takes no arguments and enforces this at compile time?
Explanation: Perl allows you to specify a prototype using parentheses after the subroutine name, as in 'sub hello () { ... }'. The other options either use incorrect syntax ('hello[]', 'def', 'function') or keywords not valid in Perl for subroutine definitions.
Which symbol do you use in Perl to create a reference to a subroutine named 'display'?
Explanation: To create a reference to a subroutine in Perl, use the backslash before the subroutine sigil: '&display'. 'u0026display' calls the subroutine, '$display' is a scalar variable, and '^display' is not valid syntax for functions or references.
How can you define an anonymous subroutine in Perl and assign it to a variable?
Explanation: The correct way to define an anonymous subroutine and assign it to a variable is 'my $sub = sub { ... };'. The options using 'var', 'func', or 'def' are incorrect for Perl and will result in errors. 'sub $sub = { ... };' is incorrect syntax.
Which way is valid for calling a Perl subroutine named 'show' without any arguments?
Explanation: Subroutines with zero or more arguments are called with parentheses (e.g., 'show();') or even just 'show;', but 'show();' is conventionally clear. The notations 'show[];', 'show{};', and 'showu003Cu003E;' are not valid for subroutine calls in Perl.
Which keyword ensures a Perl variable inside a subroutine is only accessible within that subroutine?
Explanation: 'my' declares a lexical variable in Perl, scoping it to the enclosing block such as a subroutine. 'our' creates a variable with package scope, 'local' temporarily backs up and changes a global variable, and 'global' is not a Perl keyword.