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

Global Data Object

sachin_thombre
Participant
0 Likes
1,309

Hello Team,

In the below example & highlighted in Red is the output of the below program. In first write statement 1234567890 is displayed then write statement below datatest displays ABCDE, I am confused why 1234567890 again is displayed becuase in the 3rd write statment ABDCEFGHJK is written. I am looking ABCDE.... to be displayed twice not sure why numbers are dislayed twice, can some one please expalin me this ? Thansk in advance for your reply

TYPES word(10) TYPE c.
DATA  text TYPE word.

text = '1234567890'.  WRITE / text.

PERFORM datatest.

WRITE / text.

FORM datatest.  TYPES word(5) TYPE c.
  DATA  text TYPE word.
  text = 'ABCDEFGHJK'. WRITE / text.
ENDFORM.

the following is displayed: 

1234567890

ABCDE

1234567890

Regards,

Sachin

Hi,

The scope of the local variables is limited to the subroutines, forms, class methods,etc.

1st the number is printed as expected.

Then because of the form the alphabets are printed.

Then again the control comes back to the main program and proceeds with the code written after the perform.

Regards

7 REPLIES 7
Read only

former_member207661
Active Participant
0 Likes
1,142

Hello Sachin,

text = 'ABCDEFGHJK' has scope only inside form dataset.

So, once control comes out of DATASET, text = 'ABCDEFGHJK' is no more valid.

Warm Regards,

Shyam Agrawal

Read only

Former Member
0 Likes
1,142

Hi,

The scope of the local variables is limited to the subroutines, forms, class methods,etc.

1st the number is printed as expected.

Then because of the form the alphabets are printed.

Then again the control comes back to the main program and proceeds with the code written after the perform.

Regards

Read only

Former Member
0 Likes
1,142

Hi,

The first data declaration acts as a global vairable and second data declaration inside the subroutine is local to the subrotuine.

so obviously once the control comes out of subroutine the variable will have the value '1234567890', that is the reason you can see 3rd time '123..' isntead of 'ABCD..'.

If you are expecting 3rd time also ABCD then you have use the same global variable in the subroutine signature instead of declaring it locally again.

Logic:

TYPES word(10) TYPE c.

DATA  text TYPE word.
text = '1234567890'

WRITE / text.

PERFORM datatest CHANGING text.

WRITE / text.


FORM datatest CHANGING text type word.

  text = 'ABCDEFGHJK'. WRITE / text.

ENDFORM.

Hope this is helpful.

Thanks,

Naresh

Read only

Former Member
0 Likes
1,142

You have declared text twice,

No need to declare text inside FORM....ENDFORM,

As you have declared text inside FORM....ENDFORM, this text is local for subroutine.

Delete that text declaration part from inside FORM....ENDFORM and execute, u'll get the your result.


Your subroutine should be like this..


FORM datatest. TYPES word(5) TYPE c.

   text = 'ABCDEFGHJK'. WRITE / text.

ENDFORM.

Read only

sachin_thombre
Participant
0 Likes
1,142

Thanks All for your replies...

Read only

suneel_uggina
Participant
0 Likes
1,142

Hi Sachin Thombre.

                      abap executes programs in line by line(like interpreter). So, according to first write statement, it shows the number '1234567890

after that it comes to the execution of perform statement. Whenever a perform statement executes its call the subroutine to be executed. So in subroutine, it's giving the output  'abcde. It's come out of the routine and executes the next write statement.

Best regards,

Suneel.

Read only

Former Member
0 Likes
1,142

Hello Sachin,

The first Write statement executes with the value '1234567890', which is global.

The second Write statement inside the perform executes with the value 'ABCDEFGHIJK', which is local to that particular form.

The third Write statement executes with the value '1234567890', which is again from global value.

Regards,

Imran.