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

[ABAP-OO] Returning vs Changing Method

Former Member
0 Likes
4,102

Hi Experts,

we have many way to change a variable value.

for example:

  • me->modif_value( CHANGING field = lv_field ).

or

  • lv_field = me->modif_value( lv_field ). "// returning


I would like to know what is the best way to use CHANGING or RETURNING and in which case we have to use it ?


Thank your very much.


Rachid.

6 REPLIES 6
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,953

If you only have ONE returned value, prefer functional method, you will also be allowed to use those as actual parameters of other methods.

Else you don't have any choice...

Regards,

Raymond

Read only

0 Likes
1,953

Thank you Raymond, for your answer.

Yes I know about nesting methods... I am just wondering about memory allocation?

Cause in Returning type, at the end of the method we have 2 identical memomry allocation:

and not through CHANGING type...

Any suggestion?

Rachid.

Read only

0 Likes
1,953

What's to wonder about? With returning you pass by value. With changing you can choose to pass by value or by reference. Passing by value copies the values, pass by references refers to the data object directly.

This is standard and common and not limited to OO or ABAP for that matter.

Read only

0 Likes
1,953

Hi,

yes I know about the different kind of passing data.

My question concerns the case when we have only one parameter: Which type we have to use CHANGING or RETURNING and also, why?

Rachid.

Read only

0 Likes
1,953

Which ever you want. I use returning when I want a function like call for use in expressions, changing when there's more than one value, or I want to pass by reference.

Read only

nomssi
Active Contributor
0 Likes
1,953

Hello Rachid,

newer Netweaver releases allow both a RECEIVING and CHANGING parameters. You can also introduce a parameter object and pass it as an IMPORTING parameter.

In your case I would propose to pass a pointer to your table TYPE REF TO Table_Type so you can change the table entries in the method. You could return the changed table as a value parameter without impact if it was never changed later  (check table sharing in

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables

As Matthew once said: There's the right way of doing things (my way)

regards,

JNN