Quick Contact

    SAP Tutorial

    Types Subroutines

    Basically there are two types of Subroutines in SAP ABAP programming.

    1. Local Subroutines.
    2. External Subroutines.
    Local Subroutines

    These are Subroutines in which definition (perform) and implementation (from) are available in the same program.

    External Subroutines

    Subroutines which contains definition and implementation are available in different programs are called as external Subroutines. The implemented program must be of type sub-routine pool.

    Passing values with Subroutines

    There are three ways to pass the values with Subroutines.

    1. Pass by reference.
    2. Pass by value.
    3. Pass by value and return.
    1. Pass by reference

      In this type, the actual and formal parameters are referred to the same memory. If the formal parameter is changed, the actual parameter is also changed.

      PERFORM < PERFORM NAME> USING < A> . "A IS ACTUAL PARAMETER
      FORM < PERFORM NAME> USING < F> .
      " F IS FORMAL PARAMETER
      
      < F> = SOME IMPLEMENTATION
      ENDFORM.
      
    2. Pass by Value

      In this type, the actual and formal parameters will be referring to separate memory. The formal parameter is changed, the actual parameter will not be changed.

      The key word VALUE( ) will identify that it is pass by value else it is pass by reference.

      PERFORM < PERFORM NAME> USING VALUE (< A>) . "A IS ACTUAL PARAMETER
      FORM < PERFORM NAME> USING < F> ." F IS FORMAL PARAMETER
      
      < F> = SOME IMPLEMENTATION
      ENDFORM.
      
    3. Pass by value and return

      In this type, the actual and formal parameters will be referring to separate memories. If the formal parameter is changed, the actual parameter is also changed after executing the FORM…ENDFORM.

      The key word CHANGING( ) will identify that it is pass by value else it is pass by reference.

      PERFORM < PERFORM NAME> CHANGING VALUE (< A>) . "A IS ACTUAL PARAMETER
      FORM < PERFORM NAME> USING < F> ." F IS FORMAL PARAMETER
      
      < F> = SOME IMPLEMENTATION
      ENDFORM.
      
    Subroutines statements addition
    Addition Method
    using v1 Pass by reference
    changing v1 Pass by reference
    using value(v1) Pass by value
    changing value(v1) Pass by value and result
    Things to remember before creating Subroutines
    • Perform and form must contain the same number of parameters.
    • The syntax on the form statement alone determines the method by which a parameter is passed.
    • The ‘VALUE()’ addition cannot be used on the perform statement, only be used in form.
    • ‘USING’ must come before changing.
    • The addition ‘USING’ can only occur once in a statement. The same rule applies to changing.
    Passage Method Description Advantages
    By reference Passes a pointer to the original memory location. Very efficient
    By value Allocates a new memory location for use within the subroutine. The memory is freed when the subroutine ends. Prevents changes to passed variable
    By value and result Similar to pass by value, but the contents of the new memory is copied back into the original memory before returning. Allows changes and allows a rollback


    Copyright 1999- Ducat Creative, All rights reserved.