Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CALLING SUBROUTINE

Former Member
0 Likes
506

Hi,

I wrote a program for subroutine. Now i want to call that subroutine. plz tell me how to call subroutine. plz tell me with one simple exam.

This is my first program to write subroutine.

REPORT ZAC_TAB21.

PERFORM ADDNUM.

FORM ADDNUM.

DATA:

NUM1 TYPE I VALUE 10,

NUM2 TYPE I VALUE 20,

RESULT TYPE I.

RESULT = NUM1 + NUM2.

WRITE:/ RESULT.

ENDFORM.

Now i want to call this subroutine

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
479
REPORT ZAC_TAB21.

DATA:NUM1 TYPE I VALUE 10,
     NUM2 TYPE I VALUE 20.

PERFORM ADDNUM using NUM1  NUM2.


*&---------------------------------------------------------------------*
*&      Form  ADDNUM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_NUM1  text
*      -->P_NUM2  text
*----------------------------------------------------------------------*
FORM ADDNUM  USING    P_NUM1
                      P_NUM2.

DATA: RESULT TYPE I VALUE 20  .

RESULT = P_NUM1 + P_NUM2.
WRITE:/ RESULT.

ENDFORM.                    " ADDNUM

in the above program perform is the call function for subrouine ... while execution of the program it come to the perform from ther it jumps to FORM ADDNUM and execute the lines in the Form ..... Endform returns to the perform statement and ends the program ...

<b>example of perform & form</b>

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.

reward points if it is usefull ........

Girish

3 REPLIES 3
Read only

Former Member
0 Likes
479

Hi

You have already written the Subroutine

make it a generic one like

PERFORM ADDNUM using v_no1 v_no2 .

FORM ADDNUM using p_no1 p_no2 .

DATA: RESULT TYPE I.

RESULT = p_no1 + p_no2.

WRITE:/ RESULT.

ENDFORM.

Now you can use the statement differently and pass different variables

No need of writing the FORM...ENDFORM statement again.

So main use of subroutine is reusability.

PERFORM ADDNUM using v_no3 v_no4 .

<b>Reward points for useful Answers</b>

Regards

Anji

Read only

Former Member
0 Likes
479

hI..,

You have called the subroutine ...

PERFORM ADDNUM. "<<== This statement calls the subroutine ADDNUM, defined between FORM ADDNUM and ENDFORM....

The statement we use to call a subroutine is <b>PERFORM</b> <formname>.

Please do remember to close the thread, when ur problem is solved !!

regards,

sai ramesh

Read only

Former Member
0 Likes
480
REPORT ZAC_TAB21.

DATA:NUM1 TYPE I VALUE 10,
     NUM2 TYPE I VALUE 20.

PERFORM ADDNUM using NUM1  NUM2.


*&---------------------------------------------------------------------*
*&      Form  ADDNUM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_NUM1  text
*      -->P_NUM2  text
*----------------------------------------------------------------------*
FORM ADDNUM  USING    P_NUM1
                      P_NUM2.

DATA: RESULT TYPE I VALUE 20  .

RESULT = P_NUM1 + P_NUM2.
WRITE:/ RESULT.

ENDFORM.                    " ADDNUM

in the above program perform is the call function for subrouine ... while execution of the program it come to the perform from ther it jumps to FORM ADDNUM and execute the lines in the Form ..... Endform returns to the perform statement and ends the program ...

<b>example of perform & form</b>

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.

reward points if it is usefull ........

Girish