4

I have an ABAP-OO class where I want to call a function module inside a method foo( ). There are two cases (A & B) where I have to use method foo( ). Lets say case A is default and uses requires the function module like this:

METHOD foo.
  CALL FUNCTION 'A_FUNCTION'
    EXPORTING
      required_param_x = something
      required_param_y = something_else.
      " optional_param = " i am commented out and only need for case B
ENDMETHOD.

Case B "is special" and also requires the optional_param above to be set. My current situation is to have a second method like that:

METHOD foo_b_case.
  CALL FUNCTION 'A_FUNCTION'
    EXPORTING
      required_param_x = something
      required_param_y = something_else
      optional_param = case_b_stuff.
ENDMETHOD.

Of course, this is very redundant. My real life coding is also much more complex as shown above. My question is, how can I get rid of that method foo_b_case( ) and make foo( ) suitable for both cases?

Lets say, I make the paramter "case_b_stuff" optional and just pass it in each case. How does ABAP handle the "optional_param" if "case_b_stuff" is initial?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
SDD64
  • 706
  • 13
  • 28
  • 1
    if your FM has a default for that optional parameter, you will probably override that default when calling the FM with an initial value. You could define your optional parameter with the default value (if there is one, otherwise initial is fine) and override it if necessary. – Dirk Trilsbeek Nov 06 '14 at 09:09
  • I checked the FM. As far as I can tell, it only does something if the optional paramter is not initial. So using the initial param should be fine. Thank you. – SDD64 Nov 06 '14 at 09:25

2 Answers2

3

For dynamically selecting parameters of a function module take a look at PARAMETER-TABLE keyword. This unfortunately does not work for RFC calls, but for local calls you could use it together with IS SUPPLIED condition.

IF case_b_stuff IS SUPPLIED.
  "add the parameter to the table to be included in PARAMETER-TABLE call
ENDIF.
CALL FUNCTION 'A_FUNCTION'
  PARAMETER-TABLE
    lt_parameter
  EXCEPTION-TABLE
    lt_exception.

This way you could have only one foo method. Whether the body of this method would be neat, is another question. This is all because ABAP does not allow overloading method names like it is in Java or C++.

Jagger
  • 10,350
  • 9
  • 51
  • 93
2

as already explained in the comment, first check what default value the function module for that optional parameter has. If there is none, submitting an initial value is fine. If there is a default value, submitting an initial value would override the default behavior. To make sure the FM works as expected, initialize your parameter variable with the default value of the FM in question and only override it if needed.

Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
  • 1
    This is not entirely correct: Whether "submitting an initial value is fine" depends on whether the function module checks the parameter with `IF param IS INITIAL` or `IF param IS SUPPLIED`. If it uses `IS SUPPLIED`, even submitting initial values may yield undesirable results. – vwegert Nov 09 '14 at 19:49
  • Thank you for your comment. Your statement is correct, but in my case the function actually doas a IF PARAM IS INITIAL check. So it was fine. – SDD64 Nov 10 '14 at 13:34