Monday, March 28, 2011

By reference or by value?

Probably, you have already used an ABAP method parameters. In OOP world, a method is a behavior wherein all parameters/variables accompanying it should have there own memory allocation or should be initialized. In ABAP method, you have an option in terms of parameters. It is either passing it by value or by reference. Ok, let's take a closer look of the difference between the two.

Passing by VALUE:
  a. Create a different memory allocation between the formal and actual parameters. Formal parameters are the variable owned by a method or functions or forms. While, actual parameters are the variables that will be holding the value of the formal parameters inside a method, etc.

Ex: (V1 is the actual parameter while p_v1 is the formal parameters)

PERFORM f_form  USING V1 TYPE string.
FORM f_form USING p_v1 TYPE string.

  b. Moving a value into a formal parameter has no effect in the value of the actual parameter. Treat them as an independent variables.

 Passing by REFERENCE:
  a. Formal and actual variables share a common memory allocation
  b. Whatever value will be moved to formal parameters will be the value also of the actual parameter and vice-versa.

By default, methods pass the value by reference. But, the best practice and by not breaking the property of a method, it is advisable to pass a parameter by value to avoid initialization problem later on.

There are though various techniques on how to utilize this parameter passing. Putting brackets with a by reference or by value ABAP statement addition may change its purpose.

1 comment:

Post a Comment