Application Development 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: 

Tables Vs Changing

Former Member
0 Kudos
216

Hi What is the diff bw two methods :

If I define a form nd pass internal table using tables

form demo tables t_demo

....

....

endform

and if i pass internal table using changing

form demo changing t_demo.

...

...

endform.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
137

Hi rajesh,

PROGRAM FORM_TEST.

DATA: NUM1 TYPE I,

NUM2 TYPE I,

SUM TYPE I.

NUM1 = 2. NUM2 = 4.

PERFORM ADDIT USING NUM1 NUM2 CHANGING SUM.

NUM1 = 7. NUM2 = 11.

PERFORM ADDIT USING NUM1 NUM2 CHANGING SUM.

FORM ADDIT

USING ADD_NUM1

ADD_NUM2

CHANGING ADD_SUM.

ADD_SUM = ADD_NUM1 + ADD_NUM2.

PERFORM OUT USING ADD_NUM1 ADD_NUM2 ADD_SUM.

ENDFORM.

FORM OUT

USING OUT_NUM1

OUT_NUM2

OUT_SUM.

WRITE: / 'Sum of', OUT_NUM1, 'and', OUT_NUM2, 'is', OUT_SUM.

ENDFORM.

The produces the following output:

Sum of 2 and 4 is 6

Sum of 7 and 11 is 18

In this example, the actual parameters NUM1, NUM2, and SUM are passed by reference to the formal parameters of the subroutine ADDIT. After changing ADD_SUM, the latter parameters are then passed to the formal parameters OUT_NUM1, OUT_NUM2, and OUT_SUM of the subroutine OUT.

Input parameters which are changed in the subroutine are also changed in the calling program. To prevent this, you must pass the parameter by value in a USING addition.

Example of passing parameters by reference

PROGRAM FORM_TEST.

DATA: NUM TYPE I VALUE 5,

FAC TYPE I VALUE 0.

PERFORM FACT USING NUM CHANGING FAC.

WRITE: / 'Factorial of', NUM, 'is', FAC.

FORM FACT

USING VALUE(F_NUM)

CHANGING F_FACT.

F_FACT = 1.

WHILE F_NUM GE 1.

F_FACT = F_FACT * F_NUM.

F_NUM = F_NUM - 1.

ENDWHILE.

ENDFORM.

The produces the following output:

Factorial of 5 is 120

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. In this example, the factorial of a number NUM is calculated. The input parameter NUM is passed to the formal parameter F_NUM of the subroutine. Although F_NUM is changed in the subroutine, the actual parameter NUM keeps its old value. The output parameter FAC is passed by reference.

Example of output parameters

PROGRAM FORM_TEST.

DATA: OP1 TYPE I,

OP2 TYPE I,

RES TYPE I.

OP1 = 3.

OP2 = 4.

PERFORM MULTIP

USING OP1 OP2

CHANGING RES.

WRITE: / 'After subroutine:',

/ 'RES=' UNDER 'RES=', RES.

FORM MULTIP

USING VALUE(O1)

VALUE(O2)

CHANGING VALUE(R).

R = O1 * O2.

WRITE: / 'Inside subroutine:',

/ 'R=', R, 'RES=', RES.

ENDFORM.

The produces the following output:

Inside subroutine:

R= 12 RES= 0

regards,

keerthi.

5 REPLIES 5

former_member181962
Active Contributor
0 Kudos
137

Hi Rajesh,

There is not much of difference in your example.

One thing is that you cannot pass normal variables using tables parameters.

Regards,

ravi

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
137

From Help....

<i>

The TABLES Addition

To ensure compatibility with previous releases, the following addition is still allowed before the USING and CHANGING additions:

FORM <subr> TABLES ... <itabi> [TYPE <t>|LIKE <f>] ...

The formal parameters <itabi> are defined as standard internal tables with header lines. If you use an internal table without header line as the corresponding actual parameter for a formal parameter of this type, the system creates a local header line in the subroutine for the formal parameter. If you pass an internal table with a header line, the table body and the table work area are passed to the subroutine. Formal parameters defined using TABLES cannot be passed by reference. If you want to address the components of structured lines, you must specify the type of the TABLES parameter accordingly.

<b>From Release 3.0, you should use USING or CHANGING instead of the TABLES addition for internal tables, although for performance reasons, you should not pass them by value.</b>

</i>

Regards,

Rich Heilman

Former Member
0 Kudos
138

Hi rajesh,

PROGRAM FORM_TEST.

DATA: NUM1 TYPE I,

NUM2 TYPE I,

SUM TYPE I.

NUM1 = 2. NUM2 = 4.

PERFORM ADDIT USING NUM1 NUM2 CHANGING SUM.

NUM1 = 7. NUM2 = 11.

PERFORM ADDIT USING NUM1 NUM2 CHANGING SUM.

FORM ADDIT

USING ADD_NUM1

ADD_NUM2

CHANGING ADD_SUM.

ADD_SUM = ADD_NUM1 + ADD_NUM2.

PERFORM OUT USING ADD_NUM1 ADD_NUM2 ADD_SUM.

ENDFORM.

FORM OUT

USING OUT_NUM1

OUT_NUM2

OUT_SUM.

WRITE: / 'Sum of', OUT_NUM1, 'and', OUT_NUM2, 'is', OUT_SUM.

ENDFORM.

The produces the following output:

Sum of 2 and 4 is 6

Sum of 7 and 11 is 18

In this example, the actual parameters NUM1, NUM2, and SUM are passed by reference to the formal parameters of the subroutine ADDIT. After changing ADD_SUM, the latter parameters are then passed to the formal parameters OUT_NUM1, OUT_NUM2, and OUT_SUM of the subroutine OUT.

Input parameters which are changed in the subroutine are also changed in the calling program. To prevent this, you must pass the parameter by value in a USING addition.

Example of passing parameters by reference

PROGRAM FORM_TEST.

DATA: NUM TYPE I VALUE 5,

FAC TYPE I VALUE 0.

PERFORM FACT USING NUM CHANGING FAC.

WRITE: / 'Factorial of', NUM, 'is', FAC.

FORM FACT

USING VALUE(F_NUM)

CHANGING F_FACT.

F_FACT = 1.

WHILE F_NUM GE 1.

F_FACT = F_FACT * F_NUM.

F_NUM = F_NUM - 1.

ENDWHILE.

ENDFORM.

The produces the following output:

Factorial of 5 is 120

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. In this example, the factorial of a number NUM is calculated. The input parameter NUM is passed to the formal parameter F_NUM of the subroutine. Although F_NUM is changed in the subroutine, the actual parameter NUM keeps its old value. The output parameter FAC is passed by reference.

Example of output parameters

PROGRAM FORM_TEST.

DATA: OP1 TYPE I,

OP2 TYPE I,

RES TYPE I.

OP1 = 3.

OP2 = 4.

PERFORM MULTIP

USING OP1 OP2

CHANGING RES.

WRITE: / 'After subroutine:',

/ 'RES=' UNDER 'RES=', RES.

FORM MULTIP

USING VALUE(O1)

VALUE(O2)

CHANGING VALUE(R).

R = O1 * O2.

WRITE: / 'Inside subroutine:',

/ 'R=', R, 'RES=', RES.

ENDFORM.

The produces the following output:

Inside subroutine:

R= 12 RES= 0

regards,

keerthi.

Former Member
0 Kudos
137

HI Rajesh,

if you pass a value as CHANGING VALUE()..

the change is not reflected within the FORM ..ENDFORM for the actual parameter.. ie in case you call another PERFORM like follows..

b=5.

PERFORM a CHANGING B.

FORM a CHANGING VALUE(B)

b=10.

PERFORM C.

ENDFORM.

FORM c.

write : /b.

endform.

......

now

in the form c.

the value of b is still unchanged since the endform of FORM a is still encountered

Regards,

Laxmi.

Former Member
0 Kudos
137

CHANGING would only pass the header line of the internal table to the form whereas TABLES would pass the entire internal table.

-Kiran