Passing values and references in SAP ABAP

Pass By Value And Pass By Reference In Sap Abap

This article explores the concept of pass by value and pass by reference in SAP ABAP. It delves into how data is passed between variables and methods, discussing the differences between these two approaches. By understanding this fundamental aspect of programming in SAP ABAP, developers can effectively manipulate and manage data within their applications.

Definitions

The ABAP Keyword Documentation gives the following explanation of pass by value:

In pass by value, a local data object is created as a copy of the actual parameter. Output parameters and return values are initialized when the procedure is called. Input parameters and input/output parameters are given the value of the actual parameter when they are called. Modified formal parameters are only passed to actual parameters if the procedure was concluded without errors, that is once the last statement is reached or if there is an exit using RETURN (or EXIT or CHECK ).

Pass By Reference in SAP ABAP

The ABAP Keyword Documentation offers the following explanation for pass by reference:

Passing by reference means that a local data object is not created for the actual parameter. Instead, the procedure receives a reference to the actual parameter when it is called and works directly with it. However, input parameters passed by reference cannot be altered within the procedure.

Scenarios

To help simplify the understanding of these concepts, let us consider a few different situations as examples.

The IMPORTING scenario

In the IMPORTING part of the method signature, we use VALUE and REFERENCE keywords to specify how data is passed. If none of these keywords are used, the parameter is passed by reference as a default. Changing an input parameter that is passed by reference in the method is not permitted, whereas it can be done in the pass by value situation.

REFERENCE ( iv_time_by_reference_explicit ) TYPE t

PRINT: / `After a 2-second delay, the implicit pass by reference is used:`..

Print the statement “Pass by reference (explicit) after a 2-second delay” on the screen.

Print: “After adding 10 seconds to the input parameter, pass by value.

The output of the report is displayed on the screen as follows.

Pass by reference (implicit) after a 2-second delay:

Passing by reference (explicit) after a 2-second delay.

Passing by value involves adding 10 seconds to the input parameter.

The EXPORTING scenario

In the EXPORTING section of the method signature, the keywords VALUE and REFERENCE are used to indicate how the data is passed. If none of these keywords are used, the parameter is passed by reference by default. When the parameter is passed by reference, the exporting parameter doesn’t necessarily have an initial value as illustrated by the do_nothing method in the following example. When passing by value, the modified content of the parameter is assigned to the actual parameter only if the method is completed without errors. This is illustrated by the methods get_time_120000_successfully and get_time_120000_with_exception.

You might be interested:  Step-by-Step Guide: Creating a GL Account in SAP

REFERENCE ( ev_time_by_reference_explicit ) TYPE t

REFERENCE ( ev_time_by_reference_explicit ) TYPE t.

REFERENCE (ev_time_by_reference_explicit) TYPE t should be rewritten as follows:

Modify the code to include a reference parameter named “ev_time_by_reference_explicit” of type “t”.

The value of gv_time_by_reference_implicit is assigned to ev_time_by_reference_implicit.

The value of gv_time_by_reference_explicit is assigned to ev_time_by_reference_explicit.

Initialize the variables gv_time_by_reference_implicit, gv_time_by_reference_explicit, and gv_time_by_value to empty values.

lcl_time_utility successfully retrieves the time value of 12:00:00.

The value of gv_time_by_reference_implicit is assigned to ev_time_by_reference_implicit.

ev_time_by_reference_explicit is assigned the value of gv_time_by_reference_explicit.

Initialize the variables gv_time_by_reference_implicit, gv_time_by_reference_explicit, and gv_time_by_value to empty values.

lcl_time_utility => get_time_120000_with_exception can be rewritten as lcl_time_utility retrieves the time of 12:00:00 with an exception.

The value of gv_time_by_reference_implicit is assigned to ev_time_by_reference_implicit.

ev_time_by_reference_explicit is assigned the value of gv_time_by_reference_explicit.

The program displays the resulting information on the screen.

The CHANGING scenario

In the CHANGING section of the method signature, the keywords VALUE and REFERENCE are used to indicate how the data is passed. If none of these keywords are used, the parameter is passed by reference by default. When passing by value, the modified content of the parameter is assigned to the actual parameter only if the method is completed without errors. This is illustrated in the following example:

REFERENCE ( cv_time_by_reference_explicit ) TYPE t

Declare a reference variable named cv_time_by_reference_explicit of type t.

The value of cv_time_by_reference_implicit is increased by 10.

cv_time_by_reference_explicit is incremented by 10 in this statement.

cv_time_by_reference_implicit is incremented by 10.

cv_time_by_reference_explicit is incremented by 10 in this statement.

The value of gv_time_by_reference_implicit is assigned to cv_time_by_reference_implicit using pass by reference implicitly.

The value of gv_time_by_reference_explicit is assigned to cv_time_by_reference_explicit.

lcl_time_utility => add_10_seconds_with_exception can be rewritten as lcl_time_utility calls the method add_10_seconds_with_exception.

The value of gv_time_by_reference_implicit is assigned to cv_time_by_reference_implicit.

cv_time_by_reference_explicit is assigned the value of gv_time_by_reference_explicit.

The output displayed on the screen is produced by the report.

The RETURNING scenario

In the RETURNING section of the method signature, the keyword VALUE must be used since pass by value is the only option. The modified content of the parameter is assigned to the actual parameter only if the method is completed without errors. This is illustrated in the following example:

gv_time_by_value is assigned the value of lcl_time_utility after calling the do_nothing method.

gv_time_by_value is assigned the value returned by the method get_time_120000_successfully() of the lcl_time_utility class.

The value of gv_time_by_value is assigned the result of calling the method get_time_120000_with_exception() from the local class lcl_time_utility.

You might be interested:  Sap Bi: A Hub for Open Destination

The output produced by the report is displayed on the screen.

Pass by value vs pass-by-reference: Understanding the distinction

Pass-by-reference is a concept in programming where, when a method is called, the arguments of the method refer to the same variable in memory as the caller. This means that any changes made to these arguments within the method will also affect the original variable in memory. For example, if you have a method that takes an array as an argument and modifies its elements, calling this method will directly modify the original array.

To better understand pass-by-reference and pass-by-value concepts practically: imagine you have a shopping list (which represents your variable) written on paper (memory). If you give someone else (method) access to your shopping list directly (pass-by-reference), they can add or remove items from it which would reflect on your original list too since both are referring to exactly same piece of paper.

– Pass-by-reference allows methods to directly modify variables passed as arguments.

Understanding how data is passed between methods helps programmers choose appropriate approaches based on whether they want direct modification or independent copies while developing software.

The escape character !

The exclamation mark (!) can be utilized in the method signature to differentiate a parameter name from an ABAP word that has the same name. The exclamation mark is not considered as part of the actual name itself. For instance:

PRINT: / `After a 2-second pause, the pass by reference (implicit) takes place:`..

The output displayed on the screen by the report is as follows:

Passing by reference (implicit) after a 2-second delay.

I have not encountered any situation where I found it necessary to utilize the escape character, and it is advisable to refrain from using it whenever feasible.

Distinguishing call by value and call by reference in SAP ABAP

Call by Value is a method of passing variables to a function where a copy of the variable is made. This means that any changes made to the variable within the function do not affect the original value outside of it. For example, if you pass an integer variable with a value of 5 to a function using Call by Value, and then modify it within the function to be 10, the original value of 5 will remain unchanged.

On the other hand, Call by Reference passes the actual variable itself to a function. This means that any modifications made to this variable within the function will directly affect its value outside as well. For instance, if you pass an array using Call by Reference and modify one of its elements inside the function, this change will also be reflected in the original array.

In Call by Value, when passing arguments from outside functions into parameters inside functions or vice versa, they are stored in different memory locations. This ensures that any changes made within one scope do not impact variables in another scope. It can be useful when you want to manipulate data locally without affecting global values or other parts of your program.

You might be interested:  Sap Best Practices For Sap S 4Hana

– Use Call by Value when you want your changes within a specific scope (function) not affecting variables outside.

– Use Call by Reference when you need modifications applied globally throughout your codebase.

Understanding these two methods of passing variables to functions is crucial for efficient programming and can help you design more effective algorithms.

Further reading

There have been some interesting discussions on pass by value and pass by reference in the following issues of the GitHub repository for SAP’s ABAP Styleguide:

  • Issue #150: Prefer EXPORTING parameters passed by value?
  • Issue #33: Passing value type importing parameters

Understanding Pass by Value in SAP

In the pass by value approach, a local data object is generated as a duplicate of the actual parameter. When calling a procedure, output parameters and return values are initialized. On the other hand, input parameters and input/output parameters receive the value of the actual parameter during their invocation.

Passing by value means that when we call a function or method in SAP ABAP, any changes made to the formal parameter within that function or method will not affect the original variable from which it was called. Instead, a copy of the original variable is created and used within the function or method.

For example, if we have an integer variable named “num” with an initial value of 5 and pass it as an argument to a function using pass by value mechanism, any modifications made to “num” inside that function will only be applicable locally. The original “num” outside of this function remains unaffected.

This approach ensures data integrity as it prevents unintended modifications to variables outside of their respective functions or methods. However, it also means that passing large amounts of data can consume more memory since copies need to be created for each call.

Passing by value vs passing by reference: What sets these language types apart?

On the other hand, pass by reference involves passing an alias or reference to the actual parameter itself. Instead of creating a copy, this approach allows direct access to and manipulation of the original parameter within the method. Any modifications made inside will directly affect its corresponding object outside as well.

P.S: Pass by value creates a separate copy for use in methods, while pass by reference allows direct access and modification of original parameters within methods.

By value vs. by reference: What sets them apart?

On the other hand, in the Call by Reference method, modifications can be made directly to the original value. When using this method, instead of passing copies of parameter values, references or pointers to those values are passed to a function. This allows for direct access and modification of these referenced variables within the function.