Application Development 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: 

How do I pass the value of a subroutine to another subroutine?

walkerist
Participant
0 Kudos

Hi I have two subroutines, one that converts the necessary PO currencies and one that displays it in ALV.

FORM convert_foreign_to_local.
DATA: lv_foreign_amt LIKE ekbe-wrbtr,
lv_inv_amt LIKE ekbe-wrbtr.
CALL FUNCTION 'CONVERT_AMOUNT_TO_CURRENCY'
EXPORTING
foreign_currency = ekbe-waers
foreign_amount = ekbe-wrbtr
local_currency = ekko-waers
IMPORTING
local_amount = lv_inv_amt
EXCEPTIONS
error = 1
others = 2.
IF sy-subrc = 0.
lv_foreign_amt = lv_inv_amt.
ENDIF.
ENDFORM.

FORM display_alv.
>>> I want to use the lv_foreign_amt here however, I can't get the LV_FOREIGN_AMT value here.

>>> g_test_variable = lv_foreign_amt.
ENDFORM.

8 REPLIES 8

DominikTylczyn
Active Contributor
0 Kudos

rpalotai
Participant
0 Kudos

Hi,

If they are belongs to the same program than you can use global variable (like using the TOP include).

If not than the easiest way to create a new calss, ot use an existing one and use a static attribute for this purpose. Class static attributes are available at all level in the call stack.

Br

Richard

0 Kudos

Wouldn't it be easier to use FORM parameters if a FORM needs to either accept a value or return it?

0 Kudos

rpalotaiebc global variable is bad, very very bad, you should have a look to the Clean Code ABAP

https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md

0 Kudos

Hi Frederic,

Pls. let me know which part of the clean code suggestion refersto the global variable usage recommendations?

I can't find that, many thanks.

You are right that a subroutine should always use only data that it recieves via its interface. This is the best way to keep the code easily understandable for others.

But there are many cases where you need to use tricks, specially if you are modifying standard code, the subroutines are in different programs and levels of the call stack...

Back to our current problem Anuja has provided the rigth solution if you are running the subroutines from the same program. First you have to define the lv_foreign_amt parameter in your main program and then pass this variable to the subroutines(of this program) as part of their interface.

Br

Richard

0 Kudos

You are right, it is not writen, I am really surprise

anujawani2426
Active Participant
0 Kudos

Hi,

While calling both subroutine need to pass parameters with same name.

Perform convert_foreign_to_local changing lv_foreign_amt.

Perform display_alv using lv_foreign_amt.

form convert_foreign_to_local changing lv_foreign_amt type ekbe-wrbtr.

.

.

endform.

form display_alv using lv_foreign_amt type ekbe-wrbtr .


endform.

0 Kudos

Your code formatting is missing.