Exercise 3: Control Methods in Subroutines 
You can also call the methods of a control in a subroutine or in a method. This way, you can isolate a specific function and improve the program structure.
In this exercise, you combine method calls for checking the control status in a subroutine.
In the PAI module, call subroutine get_lines in branch CASE in which you determine function code PROTECT. Pass parameters from_idx, to_idx, and index by reference:
Syntax
WHEN 'PROTECT'.
DATA: FROM_IDX TYPE I,
TO_IDX TYPE I,
INDEX TYPE I.
PERFORM GET_LINES CHANGING FROM_IDX TO_IDX INDEX.
Create the subroutine, and move the GET_SELECTION_POS and GET_FIRST_VISIBLE_LINE method calls into this subroutine.
Caution
Ensure that the formal parameters of get_lines are identical to the actual parameters of the methods copied.
The behavior of your program does not change, but the code structure of your program has improved.
Methods GET_SELECTION_POS and GET_FIRST_VISIBLE_LINE import values into actual parameters from_idx, to_idx, and index. The Control Framework stores references to these parameters when it buffers the methods in the Automation Queue.
The values are assigned to the actual parameters at flush time. In our example, this does not take place within the subroutine. This is rather astonishing since the actual parameters of the methods must be valid at flush time. However, the reference of a parameter in the subroutine turns invalid after the program has ended. Nevertheless, no runtime error occurs.
The reason is that the reference parameters of subroutines are passed on to the Automation Queue (if USING and CHANGING are used). This means that the references are still known when the Automation Queue is flushed.
Note
Using global variables as actual parameters of control methods is another feasible solution. However, you are not recommended to do this since programs with global variables are more complex.
Actual parameters from_idx, to_idx, and index are reused in the PAI module after the subroutine has been processed. In our example, we could therefore not pass the subroutine parameters by call by value - in this case, the return values of methods GET_SELECTION_POS and GET_FIRST_VISIBLE_LINE would be lost.
As a test, we will nevertheless pass one of these parameters by value. To do this, change the form definition as follows:
FORM GET_LINES CHANGING VALUE(FROM_IDX) TO_IDX INDEX.
Start your program and try to protect a line. The program terminates with a runtime error. The reason is that method GET_SELECTION_POS uses local variable FROM_IDX in the form which is not valid at flush time - that is, outside the form.
Caution
This problem occurs only if the actual parameters of the control methods are stored as references in the Automation Queue.
This means: If you use use local variables in a subroutine that are held as references in the Automation Queue, a flush call is required in the subroutine.