Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Difference between Local variable and Global variable in abap programming?

Former Member
0 Likes
11,282

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

1 ACCEPTED SOLUTION
Read only

neminath
Explorer
6,061

I hope this will help you.

10 REPLIES 10
Read only

Former Member
6,061

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

Read only

Former Member
6,061

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

Read only

VijayaKrishnaG
Active Contributor
6,061

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

Read only

RaymondGiuseppi
Active Contributor
0 Likes
6,061

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

Read only

rajkumarnarasimman
Active Contributor
0 Likes
6,061

Hi Reddy,

Please find the difference below.


Local Variable:

  Local Variables are declared inside the particular block, the block may be the following.

  1. Function
  2. Method
  3. Subroutine

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

Read only

Former Member
6,061

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.

Read only

neminath
Explorer
6,062

I hope this will help you.

Read only

venuarun
Active Participant
0 Likes
6,061

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

Read only

Former Member
0 Likes
6,061

Hi Reddy,

  • Answer to What are global and local variables?

               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.....

  • Answer to Why it is used?

                    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.

Read only

Former Member
0 Likes
6,061

Thank you guys for all your replies...

Finally i got clear idea....

Have a good weekend..