‎2014 Jul 17 6:23 AM
Hello Folks,
I would like to know what are Local and Global variables...
why to declare as Global and Local in a progam...
Please let me know... Because im confused with LV and GV variables...
Thanks
Reddy
‎2014 Jul 17 7:55 AM
‎2014 Jul 17 7:07 AM
Hello Reddy,
LOCAL VARIABLES: these are having scope in the sun routine or particular coding block in which it is declared.
GLOBAL VARIABLES: These variables can be used any where in the program, in any sub routine which is a part of the current program.
lets take an example of tax calculation which is being calculated in a sub routine.
I am having a LOCAL variable lv_tax_value declared in the sub routine. in the sub routine I have calculated the tax and copied the value into this local variable lv_tax_value.
Then in this case the local variable will not be accessible after completing the execution of sub routine. We cannot access this variable from the main program.
If we are copying the same tax value calculated into a global variable gv_tax_value then this tax value can be accessible through out the entire program.
Security also matters here as the global values can be used in any where in the program with out having any restrictions.
Thanks,
Bhaskar
‎2014 Jul 17 7:14 AM
Hi Reddy
That is recommendation from SAP that you separate the naming only. It's easier for you to recognize which is local variable or global
LV stands for Local Variable
GV stands for Global variable
Regards,
Thien
‎2014 Jul 17 7:24 AM
Hi Reddy,
Actually Variables are diffentiated as Global and Local for Runtime memory management.
Sometimes, variables are required for a specific task in whole a process. In that case if those variables are being alive for whole process they consume runtime memory for that process necessarily. So, in that case we can make that task as separate block of code in that block we can declare required variables. The Scope of those variables will be upto that particular block and they are called LOCAL Variables for that Block. They are not available for the process out of that block.
But whereas Some variables are required for whole process and they need to accessed anywhere in the process. That time we need to declare them Globally as they available in whole program. The scope of those variables will be through out the program, even in separated blocks also. Those are called GLOBAL variables.
Regards,
Vijay
‎2014 Jul 17 7:32 AM
Their usage in Abap is the same than in most programming languages, definition of the scope of variables is a subject for beginner's classromm, no ?
Regards,
Raymond
‎2014 Jul 17 7:34 AM
Hi Reddy,
Please find the difference below.
Local Variable:
Local Variables are declared inside the particular block, the block may be the following.
These variables which are declared inside the block, the visibility of the particular variable is available for that particular block only. We can't use the same variable outside the block.
If the variable is required only for the particular block, we will define the variable as 'Local Variable'.
We can declare the local variable (L_COUNT, LV_COUNT).
Global Variable:
Global variables are shared variables that is available throughout the program. We can use the variable at any place inside the program.
We can declare the global variable (G_COUNT, GV_COUNT).
Regards
Rajkumar Narasimman
‎2014 Jul 17 7:37 AM
Hello Reddy,
From the name itself we can come to a conclusion that,
1. Local variable - Is used locally for a particular sub routine. It will not be available outside that subroutine.
2. Global variable - Is used globally for a complete program. It holds the same value till the end of the program if and only if the value is not changed anywhere in the middle.
Regards,
Imran.
‎2014 Jul 17 7:55 AM
‎2014 Jul 17 8:10 AM
Hi Reddy,
Global Variables once declared they can be used anywhere in the program.
Local Variable scope is limited and it is only available at the perform or subroutine you declared.
For example :
In your top include you declare a variable
GV_GLOBAL = 'GLOBAL'
this will be accessable through out your program.
FORM LOCAL.
lv_local = 'LOCAL'.
ENDFORM.
this will be limited to this perform only.
Form Variables.
Gv_global is available
Lv_local not available
Endform.
Regards
Arun VS
‎2014 Jul 17 8:35 AM
Hi Reddy,
Global variables have lifetime through entire program ( i.e in the includes defined inside, subroutines, FM's, etc)....
Local Variables have lifetime within the block you declared.....
Usage of Global Variables consumes memory throughout the program. So the best practice is try to avoid Global variable as much as possible. That's why local variable came into picture.
Hope it will clear your confusion.....
Reward if helpful..
Regards,
Karthick R.
‎2014 Jul 20 9:06 AM
Thank you guys for all your replies...
Finally i got clear idea....
Have a good weekend..