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: 

Defining and passing an optional parameter in subroutine

former_member367551
Participant
0 Kudos

Dear forumers,

I checked up some documentations and nothing is mentioned regarding defining a subroutine (FORM ... ENDFORM) with optional parameters. I guess it's not possible to be done in a subroutine. Just wondering if there is a way to resolve an issue here or not (see no. 2 below):-

1. Perform subroutine using parameters FETXT, EQUNR, SERNR and MATNR

- EQUNR, SERNR and MATNR passed here are from ITAB A

2. Perform subroutine using parameters FETXT, EQUNR, SERNR, MATNR and QMART

- EQUNR, SERNR, MATNR, QMART passed here are from ITAB B

- QMART is only available in ITAB B, unavailable in ITAB A

This is how the subroutine is currently defined, but I'm unable to add an optional parameter here for QMART:-

FORM update_out_sb USING pv_fetxt TYPE viqmfe-fetxt
                         pv_equnr TYPE objk-equnr
                         pv_sernr TYPE objk-sernr
                         pv_matnr TYPE objk-matnr. 

   .....

ENDFORM.

Please help. Appreciate any inputs here at all. Thanks.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

you can use define to add optional parameters. with define you can pass parameters as &1 &2 etc.

they do not need any signature for them.

else.

as suggested above:

use

perform update_out_sb USING itabB-equnr 
                             itabB_sernr 
                             itabB-matnr
                             itabA-QMART.

you can call this perform inside a loop of itabB and read table itabA to get itabA-qmart.

form update_out_sb USING pv_equnr TYPE objk-equnr
                         pv_sernr TYPE objk-sernr
                         pv_matnr TYPE objk-matnr
                         QMART  like itab-QMART.

endform.

please let us know if you have any more doubt.

Somu.

7 REPLIES 7

former_member156446
Active Contributor
0 Kudos

Do some thing like this :

perform update_out_sb USING itaba-fetxt itaba-equnr itaba-sernr itabb-matnr itaba-qmart....
FORM update_out_sb USING pv_fetxt TYPE viqmfe-fetxt
                         pv_equnr TYPE objk-equnr
                         pv_sernr TYPE objk-sernr
                         pv_matnr TYPE objk-matnr
                         pv_qmart type table-qmart.

0 Kudos

Hi J@Y,

I'm unable to do this because I still have two scenarios in my codes, one without the QMART parameter and another with the QMART parameter:-

1. Perform subroutine using parameters FETXT, EQUNR, SERNR and MATNR

2. Perform subroutine using parameters FETXT, EQUNR, SERNR, MATNR and QMART

Please advise further. Thanks.

0 Kudos

1. Perform subroutine using parameters FETXT, EQUNR, SERNR and MATNR

2. Perform subroutine using parameters FETXT, EQUNR, SERNR, MATNR and QMART

You can use the condition 2. for 1 also... it means 1 is automatically covered in 2 scenario... may be you need to use it when necessary based on some condition...

if qmart is initial flag = X.
else.
flag = space.
endif.
Perform subroutine using parameters FETXT, EQUNR, SERNR, MATNR ,QMART, flag.

form.......

if flag = 'X'.
 do process 1..
else.
  do process 2..
endif.

endform.

Former Member
0 Kudos

you can use define to add optional parameters. with define you can pass parameters as &1 &2 etc.

they do not need any signature for them.

else.

as suggested above:

use

perform update_out_sb USING itabB-equnr 
                             itabB_sernr 
                             itabB-matnr
                             itabA-QMART.

you can call this perform inside a loop of itabB and read table itabA to get itabA-qmart.

form update_out_sb USING pv_equnr TYPE objk-equnr
                         pv_sernr TYPE objk-sernr
                         pv_matnr TYPE objk-matnr
                         QMART  like itab-QMART.

endform.

please let us know if you have any more doubt.

Somu.

0 Kudos

Hi Somu,

How may I use define here? Please help me to elaborate here, as I didn't find any documentation for DEFINE for FORM subroutine (it seems that DEFINE is only relevant for macros).

I'm unable to use the other example you gave me. That's because I still have two scenarios in my codes:-

PERFORM subroutine update_out_sb USING parameters FETXT, EQUNR, SERNR and MATNR

...

...

PERFORM subroutine update_out_sb USING parameters FETXT, EQUNR, SERNR, MATNR and QMART

...

0 Kudos

Hi Deborah,

let me first tell you about using perform.

keep the signature same. but do not pass any thing in that QMART.

what i mean is even if you are not having any value for QMART pass a blank value to it:

PERFORM update_out_sb USING FETXT EQUNR SERNR MATNR QMART.
                                                        "pass QMART = '' to this.

and in the form.

FORM update_out_sb USING FETXT type XXXX, 
                         EQUNR type XXX,
                         SERNR type XXX,
                         MATNR type XXX,
                         QMART type XXX,.

        if QMART is not initial.
          do this ..
        else.
          do that.
        endif.

endform.

-


Now coming to define

it actually replaces a piece of code where you call them.

to use define you will need this.

DEFINE  update_out_sb.
  FETXT =  &1. " first argument
  EQUNR = &2.
  SERNR = &3.
  MATNR = &4.
  QMART = &5.

do this .. do that... " some code what ever you need.
..... 
....

END-OF-DEFINITION.

and now to use these.

you can call them with what ever signature you need.

update_out_sb FETXT EQUNR SERNR matnr.

or

update_out_sb FETXT EQUNR SERNR MATNR QMART.

the &1 &2 will be filled up accordingly.

hope this helps

Somu

0 Kudos

Hi Somu,

Many thanks for the very helpful explanation. Didn't think about passing an empty string earlier. 😛

Thanks again! This works well now.