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,411

Hi all,

I am new in ABAP .any one can explain breifly about

pass by value

pass by reference

pass by value and result

with coding examples.

thanks and regards,

Navneeth.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,167

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

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

[/code]

Regards,

Rohini.

6 REPLIES 6
Read only

Former Member
0 Likes
1,167

hi Navaneetha,

Actual parameters can be any data objects or field symbols of the calling program whose technical attributes are compatible with the type specified for the corresponding formal parameter. When you specify the actual parameters, note that those you pass by reference to a formal parameter and those you pass by value to an output parameter can be changed by the subroutine. You should therefore ensure that only data objects that you want to be changed appear in the corresponding position of the actual parameter list.

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

Also, this will help u.

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.

Check this thread for sample program to understand better

hope this helps,

cheers,

Hema.

Read only

Former Member
0 Likes
1,167

Hi,

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.

Check this thread for sample program to understand better

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

http://help.sap.com/saphelp_nw04/helpdata/en/9f/db975c35c111d1829f0000e829fbfe/content.htm

Read only

Former Member
0 Likes
1,167

hi,

Passing By Reference

To pass data between calling programs and subroutines by reference, use USING or CHANGING for the <pass> option of the FORM and PERFORM statements as follows:

Syntax:

PERFORM [USING <ai1> ... <ain>] [CHANGING <ao1> ... <aon>] …

FORM [USING <fi1> ... <fin>] [CHANGING <fo1> ... <fon>] ...

You specify the formal and actual parameters in the list behind USING and CHANGING without any addition.

pass by value:

To ensure that an input parameter is not changed in the calling program, even if it is changed in the subroutine, you can pass data to a subroutine by value. For this, use USING for the <pass> option of the FORM and PERFORM statements as follows:

Syntax:

PERFORM... USING .......<aii> ..

FORM ..... USING ...VALUE(<fii>) ..

Pass by value and Result:

If you want to return a changed output parameter from a subroutine

to the calling program only after the subroutine has run successfully,

use CHANGING for the <pass> option of the FORM and PERFORM statements as follows:

Syntax:

FORM ..... CHANGING ...VALUE(<fii>) ..

PERFORM... CHANGING .......<aii> ..

hope it is useful.

regards,

sreelakshmi.

Edited by: Sreelakshmi p on Feb 21, 2008 5:29 AM

Edited by: Sreelakshmi p on Feb 21, 2008 5:30 AM

Edited by: Sreelakshmi p on Feb 21, 2008 5:31 AM

Read only

Former Member
0 Likes
1,168

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

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

[/code]

Regards,

Rohini.

Read only

Former Member
0 Likes
1,167

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

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

[/code]

Regards,

Rohini.

Read only

Former Member
0 Likes
1,167

Hi,

Passing Data to Subroutines

When you work with global data in subroutines, you can put a copy of the global data on a local data stack and use it to avoid accidental loss of data (In this way you protect global data.)

You can pass data between calling program and subroutines by using parameters.

• Parameters, which are defined during definition of a subroutine with FORM statement are called ‘formal parameter’.

• Parameters which are specified during the call of a subroutine with the PERFORM statement are called ‘actual parameter’.

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.

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.

Ex.

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,

Bhaskar