Local variables meaning is self-explanatory. These variables are very local to the program or function or include etc,.
The variables life time is until end of the program or function or include etc, execution where the variable was originally defined.
Guidelines for variable declaration –
- Hyphens should be avoided in variable declaration.
- Any ABAP keyword or clause name should not be used.
- User can’t use special characters like ‘t’, ‘,’.
Example –
Write a simple program to get clear understanding of local variable.
Code –
*&---------------------------------------------------------------------*
*& Report Z_LOCAL
*&---------------------------------------------------------------------*
*& Program Written by TUTORIALSCAMPUS
*&---------------------------------------------------------------------*
REPORT Z_LOCAL.
* Loop for 5 times.
DO 5 TIMES.
* Perform addition subroutine.
PERFORM addition.
ENDDO.
* Subroutine addition coding.
FORM addition.
* Initializing local variable.
DATA lv_local TYPE I VALUE 99.
* Adding 1 to local variable.
lv_local = lv_local + 1.
* Display local variable.
WRITE: / lv_local.
ENDFORM.
Output –
Explaining Example –
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear picture of example code.
Coming to output, lv_local variable is declared in the subroutine and it is local to the addition subroutine. So the lv_local variable got initialized everytime whenever addition subroutine called.
In the above example, we have called addition subroutine 5 times. So, the lv_local variable got initialized with 99 on every time and adds 1 to 99 and display output 100. The same happens for 5 times.