Returning Internal Table as Returning Parameter of Method (by Value).

When you return an internal table as the returning parameter of a method, it is passed by value.  I understand the concept of passing by value vs. passing by reference, but exactly what this means in this case is ambiguous.
1)  It could mean that the entire table, including data, is copied from the local table in the method into the parameter provided by the calling code.
2)  Alternatively, it could mean that only the pointer to the table is passed by value and the data is not literally copied from one place in memory to another.  This would <b>not</b> be the same as passing by reference.  For instance, I believe this is how object references are passed by value.
I want to know how it works, so that I know if there is an efficiency problem with returning a huge table.  The returning parameter is wonderful for getter methods, and I prefer to use it over exporting parameters, but I have some concern about passing tables as returning parameters since I don't know how this works.
Can anyone either explain this, or at least make it clear to me whether or not there is an efficiency issue?
Thanks, in advance,
-Chris

Thanks to those who tried to help me with this question, but I finally had to just figure it out on my own.  I just realized today that there is a way to find the answer using the debugger's <i>Go To->Status Display->Memory Use</i> option.  This shows how variables are stored in memory.
The answer:
First of all, if you set one internal table equal to another like:
  i_tab1 = i_tab2.
or like:
  i_tab1[] = i_tab2[].
both will simply set <i>i_tab1</i> to point to the same memory that <i>i_tab2</i> is using.  It does <b>not</b> make a copy.  Now, if you attempt to change <i>i_tab1</i>, with an <i>append</i> statement for instance, a copy of <i>i_tab2</i>'s memory is made <b>then</b>!  The requested change to <i>i_tab1</i> is then applied to the copied data.  <b>AHA!!!</b>  This means that even if you think you are copying a table, you are not really doing it until it becomes necessary due to a divergence in values.
I specifically tested a returning parameter to see how memory is handled, and it is basically just like an '<i>=</i>' statment.  No copy of the data is performed at first.  The memory that is allocated for the local variable in the method is simply pointed to by the variable used in the calling code to recieve that value.
What if you then change the value in the calling code after the method has finished executing?  The answer depends on the situation.  If the value that you returned from the method is still being pointed to by another variable somewhere, then a copy is made when you attempt to change the returned table in the calling code, but if there is no longer another variable pointing to this memory, you can change the table in the calling program all you want without causing a copy in memory.
For instance, if you have a getter method that returns the value of an attribute, at first no copy will be made, but when you try to change the table in your calling code, a copy will be made then.  However, if you just fill a local table in your getter method and return that table, there will never be a copy made, because the local variable that originally pointed to that memory expired when the method completed.  That means that after the method completes, the only variable pointing to the allocated memory is the one in the calling code that recieved the returning value.
This is fantastic!!  This behaives in a way that seems to provide maximum efficiency in most cases.  Also, the table copies are <b>never</b> a waste, since they only happen upon changing of one of the table variables that point to the common memory, and in this case you would <b>want</b> to make a copy to avoid corrupting the other variable.
Also, even if you did return your table as an exporting parameter by reference, you would not gain any significant efficiency.  There would still be no table copy if you don't change the returned table.  Also, if you did change the returned table, you <b>would</b> still produce a table copy if there was another variable, like an attribute, still pointing to the memory that you set your exporting paramter from before.
The only situation that I can see resulting in a needless efficiency problem is if someone used a getter method to return the value of a table attribute, then changed the returned table in the calling program, and then used a setter method to set the whole table attribute equal to the changed table.  This would be a waste, so maybe this should be accomplished in another way.
In conclusion, there is essentially no reason to hesitate returning a whole internal table as a returning parameter from a method call, even though it is pass by value.
Kudos to the ABAP development team for their good design!

Similar Messages

  • How to use internal table in Exporting Parameter of method.

    Hi Friends,
    I am new to abap oops and using the following code to read a select-option and pass the data in an internal table but on defining
    internal table of a standard  type it is giving me following error :
    ITAB is not an internal table - the OCCURS n specification is missing.
    code
    ====
      class lcl_get_details DEFINITION.
        PUBLIC SECTION.
          types : r_carrid type RANGE OF sflight-carrid.
          data  : itab type STANDARD TABLE OF sflight.
          METHODS : get_data IMPORTING s_carrid type R_carrid
                             EXPORTING itab type sflight.
      ENDCLASS.   
    class lcl_get_details IMPLEMENTATION.
      METHOD get_data.
        select *
        from sflight
        into table itab
        where carrid in s_carrid.
        if sy-subrc eq 0.
          sort itab by carrid.
        endif.
      endmethod.   
    ERROR : ITAB is not an internal table - the OCCURS n specification is missing.
    Kindly help.

    Hi,
    I think your problem is, because you use 2 variables named ITAB in method get_data.
    Instance-variable ITAB is defined as STANDARD TABLE OF sflight.
    Exporting-parameter ITAB is defined as sflight.
    It seems like in implementation of get_data exporting-parameter ITAB is used.
    Try this implementation:
    METHOD get_data.
      select * from sflight into table me->itab
       where carrid in s_carrid.
      if sy-subrc eq 0.
      sort me->itab by carrid.
      endif.
    ENDMETHOD.
    This should solve your compiler error.
    Nether the less, get_data will yield an empty structure as result.
    Regards, Hubert
    Edited by: Hubert Heitzer on Feb 24, 2010 10:22 AM

  • Error in passing internal table as returning parameter

    Hi
    Im new to ABAP OO.
    I declared a parameter ret_kna1 Returning Type KNA1.
    Inside the method, I am retrieving data from table KNA1 into internal table and then I want return the interal table value.
    But Im not able to assing the internal table for eg : code
    method READ_CUSTOMER_LIST.
    data: lt_kna1 type TABLE OF kna1,
          wa_kna1 LIKE LINE OF lt_kna1.
    data: lv_kunnr type kna1-kunnr,
          lv_land1 type kna1-LAND1.
    SELECT * FROM kna1
    INTO TABLE lt_kna1
    WHERE land1 eq lv_land1.
    insert LINES OF lt_kna1 INTO TABLE ret_kna1.
    endmethod.
    when I try to activate the error says 'ret_kna1 is not an internal table  "OCCURS n" specification is missing.
    but I can not declare the internal table 'ret_kna1' once again in the code, as it already defined in the parameter as type KNA1.
    please help me how to assign internal table values to the returing structur.

    Are you passing it as an EXPORT parameter?
    use and define it in the TABLES section of your function module as so:
    *"  IMPORTING
    *"     VALUE(INCLUDE_X_LEVELS) TYPE  CHAR1 OPTIONAL
    *"  TABLES
    *"      I_SELECTED_OU STRUCTURE  HRROOTOB
    *"      I_SELECTED_EE STRUCTURE  OBJEC
    *"      E_VIP_EPM_DISTR STRUCTURE  ZHR_VIP_EPM_DISTR
    *"      E_RETURN STRUCTURE  BAPIRET1
    That way you can pass value in the table (if needed) and then pass the table with your values based on your own logic

  • Reg:Submit statement Return Internal Table

    Hi Team,
    I have a query to return the internal table from the called program via SUBMIT statement to the calling statement ??
    Example : Thing is that consider reports A and B , Report B is getting called from report A where in B we have one internal table full of data that i need to pull to report A and there i need to process it.
    How can i achieve this?
    Please let me know the possible ways.
    Karthik.S

    Hi karthik,
    SUBMIT .... AND RETURN EXPORTING LIST TO MEMORY.
    Then use FM LIST_FROM_MEMORY.
    Thanks,
    Anil

  • Problem in returning internal table from FM in ABAP Objects

    Hi All,
    I have to return an internal table from one of the FM i created in the controller class (_IMPL) of a view.
    The problem here is, the internal table i m getting here
    is of type to a structure!.
    but i have to return it as type ref to DATA, which cant be assigned directly.
    Now, my question is.. How can i convert ..
    internal table type structure
    to
    internal table type ref to DATA.
    Thanks and Regards,
    sudeep v d.

    Hi Sudeep.
    Can you see if something like this works for you?
    DATA: dref type ref to DATA.
    FIELD-SYMBOL: <fs> type ANY.
    LOOP AT <internaltable> INTO <structure>
    ASSIGN "<component_name>" component of <structure> TO <fs>
    CREATE DATA dref type <structure>.
    dref->* = <fs>.
    ENDLOOP.

  • Why is the returned internal table contains same values?

    Hi all,
    I got a bapi that returned an internal table type MARC.
    For testing in R/3, the internal table returns 3 material no. with different values,
    but in vb.net, it returns 3 entries with same values,
    (I've passed the internal table as ByRef in the vb)
    what's wrong are there?
    Thanks.

    Please post your test code.

  • Return Nested Table  Using Out Parameter

    Hi Friends,
    I wrote a function in which i'm using a return type as NUMBER and OUT parameter as TABLE but i'm not able to call the function in PL/SQL because of TABLE ,Can anyone plz tell me how to call the function in PL/sql using TABLE as OUT parameter. The calling function is mentioned below..
    FUNCTION get_label_to_folder (
    i_folder_id IN NUMBER,
    i_label_id IN NUMBER,
    i_new_path IN VARCHAR2,
    i_src_path IN VARCHAR2 DEFAULT NULL,
    --i_output_loc_type   IN       VARCHAR2,
    o_vdoc_table OUT vdocs_table
    RETURN NUMBER;
    I tried to call that function in this manner but it won't work--
    DECLARE
    TYPE vdocs_table IS TABLE OF VARCHAR2 (50);
    vdoc vdocs_array := vdocs_array ();
    vret VARCHAR2 (20);
    BEGIN
    vret :=
    sce_label_util.get_label_to_folder (32272,
    324073,
    '/test-aaaa/a1/',
    NULL,
    vdoc
    FOR i IN vdoc.FIRST .. vdoc.LAST
    LOOP
    DBMS_OUTPUT.put_line (vret || ' ,' || vdoc (i));
    END LOOP;
    END;
    Kindly check it and plz resolve the problem.
    Regards,
    Anand

    It is not the correct approach to do this (passing a collection using an OUT parameter) using a function.
    Does not matter whether you dislike it. It is still wrong.
    It should be a procedure. It should likely define the OUT parameter as being passed by reference and not value in order to decrease the call overheads and increase performance.
    Also note that is it not called a table. The correct terminology is a collection or an associative array. A table is something you get inside the database engine. The structure you define in the PL engine is nothing like a database table and everything like an array.
    PS. And how do you expect us to determine the error? How do you expect us to test anything if we do not know what Oracle version you are using? Please.. FULL details, including Oracle version number and the full Oracle error message!

  • Define internal table depending on parameter

    Hello,
    does anybody know how internal tables can be defined when tablename is passed as parameter ?
    I have p_table and want to define an internal table depending on its value (e.g. KNA1, MARA, LFA1 ) dynamically, so for example if p_table= MARA
    DATA:
    l_tab_table TYPE STANDARD TABLE OF MARA.
    l_str_table TYPE MARA.
    Has anybody done this before ?
    Thank you!

    Hi
    U have to use field-symbols:
    PARAMETERS: P_TABLE(30).
    DATA: DYN_TAB TYPE REF TO DATA.
    FIELD-SYMBOLS: <TAB> TYPE TABLE.
    CREATE DATA DYN_TAB TYPE TABLE OF (P_TABLE).
    ASSIGN DYN_TAB->* TO <TAB>.
    I believe the option CREATE DATA ... TYPE TABLE is available from release 4.7.
    If you use a lower release you have to use the method
    CREATE_DYNAMIC_TABLE of class CL_ALV_TABLE_CREATE:
    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      EXPORTING
         I_STRUCTURE_NAME = P_TABLE
      CHANGING
         CT_FIELDCAT      = GT_FIELDCAT.
    call method cl_alv_table_create=>create_dynamic_table
        exporting it_fieldcatalog = gt_fieldcat
        importing ep_table        = DYN_TAB.
    ASSIGN DYN_TAB->* TO <TAB>.
    Max

  • (internal) Tables obsolete as parameter in function modules,

    Hi
    As you probably can see by the question, my ABAP Knowledge isn't exactly overwhelming !
    <i>anyway:</i>
    I'm trying to create a function module in NW2004s, This functionmodule should take an internal table, based on a structure as (import) parameter.
    In the earlier version, I've done this by defining it under the TAB 'Tables' in the function builder - But know its telling me that "<i>Tables Parameters are obsolete</i>", and that I should define it under "Changing" instead.
    I have tried that, but I'm only allowed to pass a single line, and if I try to do a Loop, in the function module, it tells me that ity isn't created as an internal table.
    So please - what is the "best practice" here ?
    Regards
    Morten Nielsen

    Hi All,
    I followed Steps 1 and 2 but when I use the following select statement I get a dump with the error enclosed.
    Step1. Created a structure ZDFKKCOHI which is a replica of DFKKCOHI.
    Step2. Created a table type ZDFKKCOHITABLE with line type as ZDFKKCOHI.
    Step3. In my function module I have a changing parameter ZDFKKCOHIPARAM of type ZDFKKCOHITABLE.
    Step 4. When I use the following select clause I am getting a dump.
    DATA: wa_zdfkkcohi LIKE LINE OF zdfkkcohiparam.
      SELECT *
      FROM dfkkcohi
      INTO  CORRESPONDING FIELDS OF TABLE zdfkkcohiparam
      WHERE cotyp EQ zcotyp
      AND gpart EQ zgpart
      AND corr_status EQ zcorr_status.
      LOOP AT zdfkkcohiparam INTO wa_zdfkkcohi.
    *Move IT_ZDFKKCOHI to ZDFKKCOHIOUT.
    *Append ZDFKKCOHIOUT.
    *Clear IT_ZDFKKCOHI.
      ENDLOOP.
    What could be wrong?
    Thanks for all your help in advance.
    Regards,
    Divya

  • ABAP OO: how to pass a internal table with objects as a method parameter

    Hello all,
    I have a class "ZCL_VEHICLE", this class contains the attribute WHEELS.
    This attribute contains objects of the class ZCL_WHEEL.
    (definition: data WHEELS TYPE TABLE OF REF TO zcl_wheel.).
    I now need a method that gives this table back (as return or export parameter). How can I define a parameter for this method that will contain this table?
    Thanks for the help.
    Best regards,
    Wim

    Hi Wim
    Excuse me! I understood you wanted to define it in a program, not in a class.
    You can define your type into CLASS (use the same statament: go to TYPES (there's a pushbotton on status gui).
    TYPES: MY_WHEELS TYPE TABLE OF REF TO .....
    Now you can use this type to create a parameter like that, but you can use that type for an object of class defined as PRIVATE.
    Infact if your object is public, you'll use it in a your program, so you'll probably have to define a variable like the type defined in the class.
    It shouldn't make sense to define the same type for two times or more (one in the class, the others in each program uses your class). So you should define your type in dictionary (all abap object could use it).
    If you think to have to use your class in few program (perhaps in olny one), you shuold consider to create your class as local class.
    Max
    Message was edited by: max bianchi

  • (internal) Tables obsolete as parameter in function modules in ECC 6.0

    Hi All,
    I followed Steps 1 and 2 in the thread  but when I use the following select statement I get a dump with the error enclosed.
    Step1. Created a structure ZDFKKCOHI which is a replica of DFKKCOHI.
    Step2. Created a table type ZDFKKCOHITABLE with line type as ZDFKKCOHI.
    Step3. In my function module I have a changing parameter ZDFKKCOHIPARAM of type ZDFKKCOHITABLE.
    Step 4. When I use the following select clause I am getting a dump.
    DATA: wa_zdfkkcohi LIKE LINE OF zdfkkcohiparam.
    SELECT *
    FROM dfkkcohi
    INTO CORRESPONDING FIELDS OF TABLE zdfkkcohiparam
    WHERE cotyp EQ zcotyp
    AND gpart EQ zgpart
    AND corr_status EQ zcorr_status.
    LOOP AT zdfkkcohiparam INTO wa_zdfkkcohi.
    *Move IT_ZDFKKCOHI to ZDFKKCOHIOUT.
    *Append ZDFKKCOHIOUT.
    *Clear IT_ZDFKKCOHI.
    ENDLOOP.
    Error: Das laufende ABAP-Programm wollte eine Open SQL-Anweisung ausführen,
    bei der die Treffermenge mit 'INTO CORRESPONDING FIELDS' in
    namensgleiche Felder der Zielbereichs gestellt werden soll. Hierbei
    müssen die namensgleichen Felder des Zielbereichs einen flachen Typ
    haben, oder vom Typ STRING oder XSTRING sein.
    Im vorliegenden Fall enthält der Zielbereich " " aber ein
    namensgleiches Feld "MANDT " mit dem verbotenen internen Typ "l".
    What could be wrong?
    Thanks for all your help in advance.
    Regards,
    Divya

    Translating the error message -;)
    The running ABAP-program wanted to carry out an opus SQL-direction, with which the hit quantity with 'INTO
    CORRESPONDING FIELDS' is supposed to be placed into name same fields the goal area.  Herewith the name same
    fields of the goal area must have be a flat type, or of the type STRING or XSTRING. 
    In the existing case, the goal area "" contains however a name same field "MANDT" with the forbidden internal type "l".
    You should check you structures and assign the correct TYPE to the fields -:)
    Greetings,
    Blag.

  • Read local folder, return filenames into internal table

    Assume I have 3 files in c:\abc folder:
    a1.xls
    a2.xls
    a6.xls
    Is there a fm or code sample to pass in folder name (ie c:\abc) and returns a1.xls, a2.xls and a6.xls into internal table?

    Use Class CL_GUI_FRONTEND_SERVICES Method DIRECTORY_LIST_FILES to get list of file in a folder . FILE_TABLE will give the table with list of files
    data: begin of itab ,
            file(26) type c,
          end of itab.
    data: tab type table of itab.
    data : cnt type i.
    CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES
      EXPORTING
        DIRECTORY                   = 'C:\temp'
    *    FILTER                      = '*.*'
    *    FILES_ONLY                  =
    *    DIRECTORIES_ONLY            =
      CHANGING
        FILE_TABLE                  = tab
        COUNT                       = cnt
    *  EXCEPTIONS
    *    CNTL_ERROR                  = 1
    *    DIRECTORY_LIST_FILES_FAILED = 2
    *    WRONG_PARAMETER             = 3
    *    ERROR_NO_GUI                = 4
    *    NOT_SUPPORTED_BY_GUI        = 5
    *    others                      = 6
    IF SY-SUBRC <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

  • Passing an Internal Table as parameter to a method

    Hi,
       Can we pass an internal table as a parameter to a method.if so how can we do that?i am new to abap objects..

    Hi Matt,
            Here is the code that i am trying to execute.I am extracting the data in the method "Extract" and passing it to the method "Display" to produce a report output.When i execute this it is giving me an error saying that t_mara is not an internal table since i just refered it with x_mara in the class definition.So how can i modify this code to pass t_mara as an internal table from the method "Extract".Please help me.
    *& Report  ZOBJ_PRAC                                      *
    REPORT  zobj_prac                                                   .
          CLASS example DEFINITION
    CLASS example DEFINITION.
      PUBLIC SECTION.
            TYPES : BEGIN OF x_mara,
                  matnr TYPE matnr,
                  ersda TYPE ersda,
               END OF x_mara.
        METHODS : extract EXPORTING t_mara TYPE x_mara,
                          display IMPORTING it_mara TYPE x_mara.
    ENDCLASS.                    "example DEFINITION
    "example IMPLEMENTATION
          CLASS example IMPLEMENTATION
    CLASS example IMPLEMENTATION.
      METHOD extract.
        DATA :lt_mara TYPE STANDARD TABLE OF x_mara INITIAL SIZE 0,
                  lw_mara TYPE x_mara.
        SELECT matnr ersda FROM mara INTO TABLE lt_mara UP TO 10 ROWS.
        t_mara[]  =  lt_mara[].
      ENDMETHOD.                    "extract
      METHOD display.
        DATA :lt_mara TYPE STANDARD TABLE OF x_mara INITIAL SIZE 0,
              lw_mara TYPE x_mara.
        lt_mara[]  =  it_mara[].
        LOOP AT lt_mara INTO lw_mara.
          WRITE:/ lw_mara-matnr,
                20 lw_mara-ersda.
        ENDLOOP.
      ENDCLASS.                    "example IMPLEMENTATION
    START-OF-SELECTION.
      DATA :  b1 TYPE REF TO example.
    CREATE OBJECT b1 TYPE example.
      TYPES : BEGIN OF x_mara,
                  matnr TYPE matnr,
                  ersda TYPE ersda,
               END OF x_mara.
    data : t_mara type standard table of x_mara initial size 0,
           it_mara type standard table of  x_mara initial size 0.
      call method b1->extract
        importing
         t_mara = t_mara.
         it_mara[]  =  t_mara[].
      call method b1->display
        exporting
         it_mara = it_mara.
    Edited by: Sai Chaitanya on Dec 2, 2008 5:30 AM

  • Passing Internal table as parameter to a Method

    Hi,
    Can somebody tell me how to pass an Internal table as a parameter to a method?
    Thanks in advance,
    Best Regards,
    Manish

    data: itab1 type string occurs 0 with header line.
    CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    exporting
    FILENAME = 'C:\ftext.txt'
    APPEND = 'X'
    changing
    DATA_TAB = <b>itab1[].</b>
    Svetlin

  • Export Internal table by calling static method

    Hi friends,
    Can u please tell me how to get internal table in export parameter by calling static method.
    OR  can u tell me how to declare internal table in method parameter.
    Thanks in advance

    Raja's method will work for all tables irrespective of the structure and is the best possible approach.  However, if your requirement is simple and you do not have to deal with a lot many tables,
    1. Create a Type-Pool, suppose ztypl
    2. Within the type pool, declare a Structure type,
    TYPES: BEGIN OF ztypl_struct1,
             "Place your fields here
           END OF ztypl_strict1,
           "Create a table of type ztypl_struct1 here
           ztypl_table1 TYPE TABLE OF ztypl_struct1.
    Use "ZTYPL_TABLE1" as the "Associated type" in your method's Parameter definition in SE24.
    eg.
    IT_TABLE      TYPE     ZTYPL_TABLE1
    If you are writing your class in an include,
        METHODS my_method1
          IMPORTING
            it_table1   TYPE ztypl_table1.
    Do remember to include
    TYPE-POOLS: ztypl.
    in your class's Constructor / Include program.
    Please award points if helpful.
    Regards,
    Ryan
    Message was edited by:
            Ryan Cannel

Maybe you are looking for

  • HT204088 I have put in two iTunes gift cards and a visa pre-paid giftcard

    the visa is out and i cant change my billing info. I cannot re-redeem my iTunes cards and i cleared my billing numbers and that hasnt worked. What do I do?

  • How to "hide" the pictures in a "Faces" selection

    I am trying to hide the pictures containing a certian face that I have identified in the "Faces" choice. Now when I choose that person in "Faces", I get all or most of those pictures showing up. Now I would like them to become "hidden" but the option

  • CSS padding in IE

    Hi , I have a simple div with this css: width: 600px; padding: 25px; In Firefox the div is the correct size 650px total width. But in IE , the padding is being applied but the total width is still 600px. IE did not include the padding in the total wi

  • I own a PC version of Photoshop CS6.  Just bought a Mac and want to use it... what can I do

    I am now a mac user but still have my registered PC version of Photoshop CS6.  Can I pay to change it over and how do I do this ?  I am willing to spend the $10 per month too.  I was able to install my lightroom on the mac with the registration numbe

  • Problem with quotation /salesorder

    hi, in one sales area we have material like m1,m2,m3...etc and customer like c1,c2,c3......etc i could't create quotation/order for  some of them combination .i am getting empty or null  as order number. can u pls tell me the reason regards Guru