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

Parameter passing by value or by reference in function module

former_member200271
Participant
0 Likes
16,215

hi everybody:

Im a beginner for abap.

Below description is described in online help.

In function module, the CALL FUNCTION statement can pass import, export, and changing parameters either by value or by reference. Table parameters are always transferred by reference.

I understand parameters passing by value means values carried by parameters are transferred, but I do not understand what is "by reference".

Please kindly give me a explanation.

Regards.

Andy

1 ACCEPTED SOLUTION
Read only

Former Member
6,557

Hi,

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.

Regards,

Bhaskar

8 REPLIES 8
Read only

Former Member
0 Likes
6,557

hi,

Function modules are modular units with interfaces. The interface can contain the following elements:

Import parameters are parameters passed to the function module. In general, these are assigned

standard ABAP Dictionary types. Import parameters can also be characterized as optional.

Export parameters are passed from the function module to the calling program. Export parameters

are always optional and for that reason do not need to be accepted by the calling program.

Changing parameters are passed to the function module and can be changed by it. The result is

returned to the calling program after the function module has executed.

Exceptions are used to intercept errors. If an error triggers an exception in a function module, the

function module stops. You can assign exceptions to numbers in the calling program, which sets the

system field SY-SUBRC to that value. This return code can then be handled by the program.

By reference Passes a pointer to the original memory location. Very efficient

By value Allocates a new memory location for use within the subroutine. The memory is freed when the subroutine ends. Prevents changes to passed variable

By value and result Similar to pass by value, but the contents of the new memory is copied back into the original memory before returning. Allows changes and allows a rollback

When you pass a parameter by reference, new memory is not allocated for the value. Instead, a pointer to the original memory location is passed. All references to the parameter are references to the original memory location. Changes to the variable within the subroutine update the original memory location immediately.

1 report ztx1804.

2 data f1 value 'A'.

3

4 perform s1 using f1.

5 write / f1.

6

7 form s1 using p1.

8 p1 = 'X'.

9 endform.

The code in Listing produces the following output:

X

Hope this helps, Do reward.

Read only

Former Member
0 Likes
6,557

Hi Andy,

By Refence mean the values you passed in a variable are not copied but the same will be used in the FM.

Regards,

Atish

Read only

0 Likes
6,557

Hi Atish:

Thanks for the reply.

Can you give me an example? I am not quite understand, maybe because of language difference.

Andy

Read only

0 Likes
6,557

hi,

1 report ztx1804.

2 data f1 value 'A'.

3

4 perform s1 using f1.

5 write / f1.

6

7 form s1 using p1.

8 p1 = 'X'.

9 endform.

The code in Listing produces the following output:

X

Hope this helps, Do reward.

Read only

Former Member
0 Likes
6,557

Andy,

Check this docs.

Parameters Passed by Reference

You list these parameters after USING or CHANGING without the VALUE addition:

FORM <subr> USING ... <pi> [TYPE <t>|LIKE <f>] ...

CHANGING ... <pi> [TYPE <t>|LIKE <f>] ...

The formal parameter occupies no memory of its own. During a subroutine call, only the address of the actual parameter is transferred to the formal parameter. The subroutine works with the field from the calling program. If the value of the formal parameter changes, the contents of the actual parameter in the calling program also change.

For calling by reference, USING and CHANGING are equivalent. For documentation purposes, you should use USING for input parameters which are not changed in the subroutine, and CHANGING for output parameters which are changed in the subroutine.

To avoid the value of an actual parameter being changed automatically, you must pass it by value.

Input Parameters That Pass Values

You list these parameters after USING with the VALUE addition:

FORM <subr> USING ... VALUE(<pi>) [TYPE <t>|LIKE <f>] ...

The formal parameter occupies its own memory space. When you call the subroutine, the value of the actual parameter is passed to the formal parameter. If the value of the formal parameter changes, this has no effect on the actual parameter.

Read only

Former Member
0 Likes
6,557

passing by reference means u actually pass the actual address of ur variable(/whatever u want to pass). in the subroutine whatever is done to the variable is actually done into its actual memory location. so whatever is the final value of the variable within the subroutine, will be the value of the variable when you are out of the subroutine also.

lets put it in another way. you(program) give a glass of water(variable passed by reference) to your friend(subroutine) . friend mixes colour(manipulate your variable's data) in the water. now he(subroutine) returns it(final value within subroutine) to you(calling program). you(program) get coloured water(same value returned by subroutine) because u had actually given your own container(original address of variable) of water to him.this i s pass by reference.

pass by value.- you give another glass of water(dummy variable) not your own glass. so whatever he does in that glass(dummy variable) wont affect water in your glass(original addresss). but you can see the result when he returns it(dummy variable ).

Read only

Former Member
6,558

Hi,

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.

Regards,

Bhaskar

Read only

0 Likes
6,557

Hi Bhaskar,


I know this is a very old post, but your examples both look identical.

Your explanation is fine, so I presume it's just a typo in your code sample.

I think your 'By Reference' example should read

By Reference

     Data: a type I value 20.

     Perform sub1 changing a.

     Write a.

     FORM sub1 changing value (p_a)

          P – a = 15.

     ENDORM.

I like to think of it as the difference between creating a copy of a file (pass by value) vs. creating a shortcut to the file (pass by reference).


Regards,

Niall