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

subroutines

Former Member
0 Likes
1,186

hi experts,

1.wat is pass by value, pass by reference, pass by value result?plz explain me with examples plz?

2.in a script, can we run a script form with out a main program?

hi experts,

1.wat is pass by value, pass by reference, pass by value result?plz explain me with examples plz?

2.in a script, can we run a script form with out a main program?

2 REPLIES 2
Read only

Former Member
0 Likes
601

Hello Murali,

Pass by reference:

A way of passing data from actual parameters to formal parameters. When you pass by reference, no local data object is specified for the actual parameter, but the procedure receives a reference to the actual parameter during call and works with the actual parameter itself. A change to the formal parameter in the subroutine also changes the value of the actual parameter.

Pass by value:

a local object with the same data type as the corresponding actual parameter is created in the subroutine and filled with its values. A change to the formal parameter in the subroutine does not change the value of the actual parameter. The actual parameter also retains its original value even after the subroutine has ended.

Example to make you understand better.

REPORT zkartest.

PARAMETER: test(2) TYPE c.

PERFORM change_test1 USING test.

WRITE:/ test.

PERFORM change_test2 USING test.

WRITE:/ test.

&----


*& Form change_test1

&----


  • Value of test will change here. Pass by REFERENCE

----


  • -->P_TEST text

----


FORM change_test1 USING p_test.

p_test = p_test + 2.

WRITE:/ p_test.

ENDFORM. "change_test1

&----


*& Form change_test2

&----


  • Value of test will change here but only in the subroutine. Pass by VALUE

----


  • -->VALUE(P_TEST) text

----


FORM change_test2 USING value(p_test).

p_test = p_test + 2.

WRITE:/ p_test.

ENDFORM. "change_test2

in a script, can we run a script form with out a main program?

No,it is not possible.

Reward Points if it is helpful

Thanks

Seshu

Read only

Former Member
0 Likes
601

Hello,

Check This :-

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

Pass by value

PERFORM subr USING a1 a2 a3 a4 a5

Click for example: http://help.sap.com/saphelp_47x200/helpdata/en/9f/db979d35c111d1829f0000e829fbfe/frameset.htm

Pass by reference

PERFORM subr CHANGING a1 a2 a3 a4 a5

Click for example Example: http://help.sap.com/saphelp_47x200/helpdata/en/9f/db979035c111d1829f0000e829fbfe/frameset.htm

Parameters are passed to the FORM either:

• By value
• By Reference
• By value and return.

By Value

Data : a type I value 20.
Perform sub1 using a.
Write a.
FORM sub1 using value (p_a)
P – a = 15
ENDORM.
In this case during subroutine call, the formal parameter are created as copies of actual parameter.
The formal parameters have the memory of their own. Changes made to formal parameter have no effect on the actual parameter.

Like in this case, though value of p_a is changed to 15, it has no effect on ‘a’ which remains as 20.

By Reference

Data: a type I value 20.
Perform sub1 using a.
Write a.
FORM sub1 using value (p_a)
P – a = 15.
ENDORM.
By default system calls all the forms by reference.

In this case, only the address of the actual parameter is transferred to the formal parameters. The formal parameter has no memory of its own. If you change the formal parameter, change is visible in actual parameter also.

By Value and Return

Data : a type I value 20.
Perform sub1 changing a.
FORM sub1 changing value (p_a)
P – a = 15.
ENDORM.
In this case if you change formal parameter, then the value of actual parameter is changed when the control is transferred back to the main program.

Assuming A is declared by DATA statement and has value 20 and subroutine SUB1 is called by passing A.

CALLING FORM VALUE OF A IN PROGRAM VALUE OF A IN FORM
(p_a = 15)
BEFORE CALLING FORM A = 20
A = 20 P_A = 100
BY VALUE
(USING) 

AFTER RETURNING FROM FORM A = 20 (changing value of p_a)
A = 20.
BEFORE CALLING FORM A = 20
A = 20 P_A = 100
BY REFERENCE
(USING) AFTER RETURNING FROM FORM A = 20 (changing value of p_a)
A = 100
BE VALUE AND RETURN BEFORE CALLING FORM A = 20
A = 20 P_A = 100
(CHANGING) AFTER RETURNING FROM FORM A = 100 (changing value of p_a)
A = 100.

Passing Table to a Subroutine

You can pass internal tables as parameters USING or CHANGING in the FORM and PERFORM statements. If you want to access the components of the internal table, you must specify the type of the corresponding formal parameter.

You also must distinguish between internal tables with or without header lines. For internal tables with header lies, you must specify the table body by using square brackets [ ], after the table name to distinguish it from the header line.

With internal subroutines, you can use TYPE or LIKE to refer to the internal table you want to pass directly.

You can pass all internal tables as parameters in the list after TABLES in the FORM and PERFORM statements. Internal tables passed with TABLES are always called by reference.

If you pass all internal table with a header line, the table body and the table work area are passed to the subroutine. If you pass an internal table without a header line, a header line is created automatically as a local data object in the subroutine. 

PROGRAM ZDEMO
DATA: Begin of itab occurs 0,
Number type I,
end of itab

PERFORM SUB1 TABLES ITAB.

LOOP AT ITAB.
WRITE: / itab-number.
ENDLOOP.

FORM SUB1 TABLES F_ITAB LIKE ITAB [ ].
DO 3 TIMES.
F_itab-number = SY-INDEX.
APPEND F_ITAB.
ENDDO.
ENDFORM.

After starting ZDEMO the output appears as follows:

1
2
3

In this example, an internal table ITAB is declared with a header line. ITAB is passed to the subroutine SUB1, where it is filled using the table work area F_ITAB. And itab is written in main program

Regards,

Deepu.K