Search code examples
abapfunction-module

Optional parameters in function modules?


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?


Solution

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