Functions (function)
A reusable code is usually organized to form a function. Perl declares the function with the keyword sub declaration, followed by {开始 开始}:
Functions Are Used to Organize Your Code InTo Small Pieces Which Can Be Reused. Perl Declares Functions Using The Sub Keyword Function by To} To end IT:
Sub function1 {code here}
Way to call the Perl function: Tighten with the function name with the "& symbol. If the function is included in the function, the parameters should be included with parentheses:
In order to call a Perl function it's suggested that you call it using the & sign followed by the function name If you have parameters, they can be also passed, so it's suggested that you enclose them in parentheses.:
& function1;
In Perl, the function does not return a value as the process, and the function can also return a value after the function is called (how to operate, will be described later). The called function called the above example code is equivalent to a process, and the following example indicates that the function is called back to a value (we also need to pass parameter value to the function).
In Perl, functions can serve as procedures which do not return a value (in reality they do, but that will be discussed later), or functions which do return a value. The above example is of a function that acts as a procedure. Below IS An EXAMPLE OF A FUNCTION THAT ACTS AS A FUNCTION, BECAUSE It Returns a value (We Are Also Passing Values To the function):
$ answer = & add (1, 2);
The parameter value passed to the function is not necessarily limited to the scalar, you can also pass an array: @_
YOU AREN 'THUES TO PASING ONLY SCALUES TO A FUNCTION. You Can Also Pass IT ARRAYS, But It Will Be Easier To Pass Them at the end.
Perl Functions Receive Their Values in an Array: @_
The array transmits all the values stored to the function. In the above example, we pass two values (array forms), and we receive them in the function in the following manner:
This Array Holds All The Values of The Parameters Passed to The Function. So in The Above Example, Where We Had 2 Values Passed, WE Would Retrieve The Following Manner:
Sub Function1 {($ VAL1, $ VAL2) = @_;} or you can do this:
Or We Could Do It in this Manner:
Sub function1 {$ VAL1 = $ _ [0]; $ VAL2 = $ _ [1];
Parameter is passed, meaning that you are using the original parameter copy instead of the original parameter itself. If you want to pass in a reference method, the variable must be used directly ($ _ [subscript]).
The variable range of Perl is different from other languages, and the variable is not required in the beginning of the program, or in a function: you can use these variables. The range of variables is always global. If you use a variable in a function, this variable is globally, which can be applied to the rest of the program. If you want this variable to be visible in a function, you must declare it for a local variable (Note: Indicate a local variable with keyword local):
The parameters are being passed by value, meaning that you will be working with copies of the original parameters and not the actual parameters themselves. If you wish to pass them by reference, then you would have to work directly with the variable ($ _ [ Subscript]).
Perl's scope of variables is different than most other languages You do not have to declare your variables at the beginning of the program, or in the functions:... You just use them The scope of the variables is always global If you use a . variable in a function, it is global to the rest of the program If you want the variable to be seen only by code within that particular function (and any other functions it may call), then you must declare it as a local variable:
Sub Function1 {Local ($ myvar)}
The above code declares that $ myvar is a local variable, so it cannot be used by the rest of the program.
The Above Code Will Declare $ MyVar As Local, So It Can't Be Seen by The Rest of the Program.
The function can also be called itself, for example, a recursive function.
Functions Can Also Be Nested With Other, and You Create Recursive Functions That Call Themselves.