Skip to Content
author's profile photo Former Member
Former Member

Difference between Local variable and Global variable in abap programming?

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

10 Answers

  • Best Answer
    Posted on Jul 17, 2014 at 06:55 AM

    I hope this will help you.


    6.png (12.9 kB)
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2014 at 06: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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2014 at 06: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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2014 at 06: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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2014 at 06: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 17, 2014 at 06: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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 20, 2014 at 08:06 AM

    Thank you guys for all your replies...

    Finally i got clear idea....

    Have a good weekend.. 😊 😊 😊

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2014 at 06: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.

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2014 at 07: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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2014 at 07:35 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.