Print "Demonstration of Perl function or subroutine with arguments:" Now we will see another example that uses parameters or arguments that are passed to the function or subroutine that is defined. In the above program, we can see we have defined subroutine or function using keyword “sub” before subroutine name, and in the above code, the subroutine name is “func_name”, and inside the function, we are just printing the message and then we are calling the subroutine using its name, and in the above code, we are defining the subroutine with no parameters. Print "The above defined function is now called " Print "Welcome to Educba with Perl tutorial" Print "The function or subroutine is defined using sub." Print "Demonstration of Perl function or subroutine:" Now let us see a few examples of how to define function or subroutine in Perl: Example #1 The above syntax differs if we are using Perl version below 5 as the above syntax for calling subroutine works only in Perl version 5 and above, and if we are using below 5 versions, then we need to use an ampersand (&) before func_name such as “&func_name(parameter_lst)” which is usually not used as it means as bypassing the subroutine prototypes and hence it is not recommended to use. Now let us see the syntax for calling the subroutines. The above syntax is used for defining or declaring the subroutine. In the above syntax, we can see whenever we are defining the subroutine we first use the “sub” keyword before the subroutine or function name and the set of code that needs to be performed again and again is written within the curly brace, which is known as the body of subroutine or function. Now let us see the syntax of defining the subroutines in Perl: In Perl, the function or subroutines as in other programming languages we can pass any number of parameters to subroutines, and also we can pass lists, arrays, hashes to subroutines in Perl but returning the values, we can return array and hashes not more than one which may lead to ambiguity of identities of array or hashes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |