Optional parameters in function modules?

SDD64 picture SDD64 · Nov 6, 2014 · Viewed 7.8k times · Source

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?

Answer

Jagger picture Jagger · Nov 7, 2014

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++.