Skip to Content
author's profile photo Former Member
Former Member

passing parameters to subroutine

Hi All

Can anybody give a better example of passing parameters to subroutine.please

Thanks and Regards

vijaya

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

9 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 10:43 AM

    Check this out

    DATA: NUMBER_I TYPE I VALUE 5, 
          NUMBER_P TYPE P VALUE 4, 
          BEGIN OF PERSON, 
            NAME(10)      VALUE 'Paul', 
            AGE TYPE I    VALUE 28, 
          END   OF PERSON, 
          ALPHA(10)       VALUE 'abcdefghij'. 
    FIELD-SYMBOLS TYPE ANY. 
    ASSIGN NUMBER_P TO . 
    PERFORM CHANGE USING 1 
                         NUMBER_I 
                         NUMBER_P 
                         
                         PERSON 
                         ALPHA+NUMBER_I(). 
    
    FORM CHANGE USING VALUE(PAR_1) 
                      PAR_NUMBER_I 
                      PAR_NUMBER_P 
                      PAR_POINTER 
                      PAR_PERSON STRUCTURE PERSON 
                      PAR_PART_OF_ALPHA. 
      ADD PAR_1 TO PAR_NUMBER_I. 
      PAR_NUMBER_P = 0. 
      PAR_PERSON-NAME+4(1) = ALPHA. 
      PAR_PERSON-AGE = NUMBER_P + 25. 
      ADD NUMBER_I TO PAR_POINTER. 
      PAR_PART_OF_ALPHA = SPACE. 
    ENDFORM. 
    
    
    
    Field contents after the PERFORM statement: 
    
    NUMBER_I    = 6 
    NUMBER_P    = 6 
       = 6 
    PERSON-NAME = 'Paula' 
    PERSON-AGE  = 25 
    ALPHA       = 'abcde    j' 
    

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 16, 2006 at 10:45 AM

    Check this program:

    demo_mod_tech_describe

    Regards,

    ravi

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 10:46 AM

    HI Vijaya,

    Details below will give you better understanding of Subroutines:

    What are the different methods of passing data?

    A. Calling by reference: During a subroutine call, only the address of the actual parameter is transferred to the formal parameters. The formal parameter has no memory of its own, and we work with the field of the calling program within the subroutine. If we change the formal parameter, the field contents in the calling program also change.

    B. Calling by value: During a subroutine call, the formal parameters are created as copies of the actual parameters. The formal parameters have memory of their own. Changes to the formal parameters have no effect on the actual parameters.

    C. Calling by value and result: During a subroutine call, the formal parameters are created as copies of the actual parameters. The formal parameters have their own memory space. Changes to the formal parameters are copied to the actual parameters at the end of the subroutine.

    The method by which internal tables are passed is By Reference.

    Syntax1: PERFORM <XXXX> using <YYY>

    changing <MMM>

    FORM <XXXX> using <YYY> like <ZZZ>

    changing <MMM> like <NNN> -<b> Pass by reference</b>

    OR

    FORM <XXXX> using value (YYY) like <ZZZ> - Pass by value, creates another copy of the variable.

    Example1:

    PERFORM date-invert using in-date

    Changing out-date

    FORM date-invert using in-date like datum

    Syntax2: PERFORM function-name(program) IF FOUND.

    Reward points if this helps.

    Manish

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 10:47 AM

    In the below example i am reading the text using read_text function module.

    The suroutine name is read_text1,the paramteres are p_var and p_obj.

    Example:

    PERFORM read_text1 TABLES g_t_lines USING p_var p_obj.

    FORM read_text1 TABLES g_t_lines STRUCTURE tline

    USING p_var TYPE c

    p_obj TYPE c.

    READ TABLE xvttp INDEX 1.

    g_f_tdname1 = xvttp-vbeln.

    g_f_obj1 = p_obj.

    g_f_langu1 = 'DE'.

    REFRESH g_t_lines.

    CLEAR g_t_lines.

    CALL FUNCTION 'READ_TEXT'

    EXPORTING

    id = p_var

    language = g_f_langu1

    name = g_f_tdname1

    object = g_f_obj1

    TABLES

    lines = g_t_lines

    EXCEPTIONS

    id = 1

    language = 2

    name = 3

    not_found = 4

    object = 5

    reference_check = 6

    wrong_access_to_archive = 7

    OTHERS = 8.

    IF sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    ENDIF.

    endform.

    Please check and let me know if u face any problem.

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 10:49 AM

    Hi,

    perform add using a b changing c

    write c.

    form add using a b changing c.

    c = a + b.

    DATA: NUMBER_I TYPE I VALUE 5,

    NUMBER_P TYPE P VALUE 4,

    BEGIN OF PERSON,

    NAME(10) VALUE 'Paul',

    AGE TYPE I VALUE 28,

    END OF PERSON,

    ALPHA(10) VALUE 'abcdefghij'.

    FIELD-SYMBOLS POINTER.

    ASSIGN NUMBER_P TO POINTER.

    PERFORM CHANGE USING 1

    NUMBER_I

    NUMBER_P

    POINTER

    PERSON

    ALPHA+NUMBER_I(<POINTER>).

    FORM CHANGE USING VALUE(PAR_1)

    PAR_NUMBER_I

    PAR_NUMBER_P

    PAR_POINTER

    PAR_PERSON STRUCTURE PERSON

    PAR_PART_OF_ALPHA.

    ADD PAR_1 TO PAR_NUMBER_I.

    PAR_NUMBER_P = 0.

    PAR_PERSON-NAME+4(1) = ALPHA.

    PAR_PERSON-AGE = NUMBER_P + 25.

    ADD NUMBER_I TO PAR_POINTER.

    PAR_PART_OF_ALPHA = SPACE.

    ENDFORM.

    Field contents after the PERFORM call are as follows:

    NUMBER_I = 6

    NUMBER_P = 6

    POINTER = 6

    PERSON-NAME = 'Paula'

    PERSON-AGE = 25

    ALPHA = 'abcde j'

    TEXT(50),

    NUMBER TYPE I,

    END OF ITAB.

    STRUC LIKE T005T.

    ...

    PERFORM DISPLAY TABLES ITAB

    USING STRUC.

    FORM DISPLAY TABLES PAR_ITAB STRUCTURE ITAB

    USING PAR STRUCTURE T005T.

    DATA LOC_COMPARE LIKE PAR_ITAB-TEXT.

    WRITE: / PAR-LAND1, PAR-LANDX.

    ...

    LOOP AT PAR_ITAB WHERE TEXT = LOC_COMPARE.

    ...

    ENDLOOP.

    ...

    ENDFORM.

    Regards

    amole

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 10:58 AM
    1.  perform <form_name> using tables
    
    
    TYPES: BEGIN OF ITAB_TYPE, 
             TEXT(50), 
             NUMBER TYPE I, 
           END OF ITAB_TYPE. 
    
    DATA:  ITAB TYPE STANDARD TABLE OF ITAB_TYPE WITH 
                     NON-UNIQUE DEFAULT KEY INITIAL SIZE 100, 
           BEGIN OF ITAB_LINE, 
             TEXT(50), 
             NUMBER TYPE I, 
           END OF ITAB_LINE, 
           STRUC like T005T. 
    ... 
    PERFORM DISPLAY TABLES ITAB 
                    USING  STRUC. 
    
    FORM DISPLAY TABLES PAR_ITAB STRUCTURE ITAB_LINE 
                 USING  PAR      like      T005T. 
      DATA: LOC_COMPARE LIKE PAR_ITAB-TEXT. 
    
      WRITE: / PAR-LAND1, PAR-LANDX. 
      ... 
      LOOP AT PAR_ITAB WHERE TEXT = LOC_COMPARE. 
        ... 
      ENDLOOP. 
    ENDFORM.
    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 10:46 AM

    http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db984635c111d1829f0000e829fbfe/content.htm

    PROGRAM form_test.

    DATA: a1 TYPE p DECIMALS 3,

    a2 TYPE i,

    a3 TYPE d,

    a4 TYPE spfli-carrid,

    a5(1) TYPE c.

    ...

    PERFORM subr USING a1 a2 a3 a4 a5.

    ...

    PERFORM subr CHANGING a1 a2 a3 a4 a5.

    ...

    PERFORM subr USING a1 a2 a3

    CHANGING a4 a5.

    ...

    FORM subr USING

    value(f1) TYPE p

    value(f2) TYPE i

    f3 LIKE a3

    CHANGING

    value(f4) TYPE spfli-carrid

    f5.

    ...

    ENDFORM.

    For more details have a look at below link.

    http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db979d35c111d1829f0000e829fbfe/content.htm

    Best Regards,

    Vibha

    *Please mark all the helpful answers

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 10:46 AM

    Hi,

    Go thru this Docu.

    PERFORM - parameter_list

    Syntax

    ... [TABLES itab1 itab2 ...]

    [USING a1 a2 ...]

    [CHANGING a1 a2 ...].

    Extras:

    1. ... TABLES itab1 itab2 ...

    2. ... USING a1 a2 ...

    3. ... CHANGING a1 a2 ...

    Effect

    These additions assign actual parameters to the formal parameters from the parameter interface for the subroutine subr. You can specify all data objects whose data type matches the typing of the corresponding formal parameter (see Check Typing) as actual parameters. Each formal parameter assumes all the properties of the actual parameter assigned to it when it is called.

    Addition 1

    ... TABLES itab1 itab2 ...

    Effect

    If you specify the addition TABLES, each table parameter t1 t2 ... for the subroutine called that is defined with the addition TABLES to the FORM statement must be assigned an internal table itab as the actual parameter. The assignment of the actual parameters to the formal parameters takes place using their positions in the lists t1 t2 ... and itab1 itab2 ... .

    You can only specify standard tables for itab. Transfer takes place by means of a reference. If a specified table itab has a header line, this is also transferred; otherwise, the header line in the corresponding table parameter t is blank when it is called.

    Note

    Use of table parameters in the interface for subroutines is obsolete but a large number of subroutines have not yet been converted to appropriately typed USING or CHANGING parameters, so that they must still be supplied with data by the TABLES addition to the PERFORM statement.

    Example

    Static call of the internal subroutine select_sflight transferring a table parameter.

    PARAMETERS: p_carr TYPE sflight-carrid,

    p_conn TYPE sflight-connid.

    DATA sflight_tab TYPE STANDARD TABLE OF sflight.

    ...

    PERFORM select_sflight TABLES sflight_tab

    USING p_carr p_conn.

    ...

    FORM select_sflight TABLES flight_tab LIKE sflight_tab

    USING f_carr TYPE sflight-carrid

    f_conn TYPE sflight-connid.

    SELECT *

    FROM sflight

    INTO TABLE flight_tab

    WHERE carrid = f_carr AND

    connid = f_conn.

    ENDFORM.

    Addition 2

    ... USING a1 a2 ...

    Addition 3

    ... CHANGING a1 a2 ...

    Effect

    If you specify the additions USING and CHANGING, an actual parameter a1 a2 ... of the appropriate type must be assigned to each of the formal parameters u1 u2 ... and c1 c2 ... defined with the same additions to the FORM statement. The actual parameters specified after USING and CHANGING form one shared list. They are assigned to the formal parameters after the position in the shared list. The type of parameter transfer is defined with the additions USING and CHANGING to the FORM statement. The addition USING must be before CHANGING. Otherwise, the assignment of the actual parameters to the additions USING and CHANGING is irrelevant to the PERFORM statement. It is also irrelevant whether only one or both of the additions is specified.

    Notes

    For the sake of program documentation, we advise that you specify the additions USING and CHANGING in the FORM statement according to the definition of the parameter interface.

    In non-Unicode programs, you can address memory area outside an actual parameter if an actual parameter a1 a2 ... is assigned offset or length specifications. In non-Unicode programs, the length is set to the length of the current parameter if an offset is specified without a length. Both of these lead to warnings in the syntax check and to syntax errors in Unicode programs. The rules for the ASSIGN statement apply to the addressable memory area in non-Unicode programs.

    Example

    The following five PERFORM statements mean the same but only the fourth is recommended, since it is the only one that documents the interface of the subroutine called.

    DATA: a1 TYPE string,

    a2 TYPE string,

    a3 TYPE string,

    a4 TYPE string.

    PERFORM test USING a1 a2 a3 a4.

    PERFORM test CHANGING a1 a2 a3 a4.

    PERFORM test USING a1 CHANGING a2 a3 a4.

    PERFORM test USING a1 a2 CHANGING a3 a4.

    PERFORM test USING a1 a2 a3 CHANGING a4.

    ...

    FORM test USING p1 TYPE string

    p2 TYPE string

    CHANGING value(p3) TYPE string

    value(p4) TYPE string.

    ...

    ENDFORM.

    Thanks

    Sunil

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 16, 2006 at 11:11 AM

    Hi all

    Iam still confused..ican understand theory wise..when iam trying to implement iam getting lot of doubts..esepcially when passing parameters using USING and CHANGING.i understood the perform part...when we r defining the perfrom using Form iam getting stuck...please is there any other way which can explain me better..the examples given by u's r very good..but i need more clear and easy ones...please.

    Thanks and regards

    vijaya

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.