2014 Jan 15 3:44 PM
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
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.
2014 Jan 15 6:16 PM
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
2014 Jan 16 4:00 AM
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
2014 Jan 16 5:47 AM
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
2014 Jan 16 6:05 AM
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.
2014 Jan 17 4:29 PM
2014 Jan 17 5:30 PM
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.
2014 Jan 17 7:09 PM
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.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |