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

parameters

Former Member
0 Likes
1,333

Hi Friends,

Will u tell me how to pass the parameters in FM & subroutine with proper example??

10 REPLIES 10
Read only

Former Member
0 Likes
1,126

Hi salil,

SUBROUTINE:

Example of Passing Parameters by Reference

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

After subroutine:

RES= 12

To return a changed formal parameter once the subroutine has finished successfully, you can use a CHANGING parameter and pass the parameter by reference. In this example, the actual parameters OP1 and OP2 are passed by value in the USING addition to the formal parameters O1 and O2. The actual parameter RES is passed by value to the formal parameter R using CHANGING. By writing R and RES onto the screen from within the subroutine, it is demonstrated that RES has not changed its contents before the ENDFORM statement. After returning from the subroutine, its contents have changed.

FUNCTION MODULE:

PROGRAM CALL_FUNCTION.

DATA: TEXT(10) TYPE C VALUE '0123456789',

TEXT1(6) TYPE C,

TEXT2(6) TYPE C.

PARAMETERS POSITION TYPE I.

CALL FUNCTION 'STRING_SPLIT_AT_POSITION'

EXPORTING

STRING = TEXT

POS = POSITION

IMPORTING

STRING1 = TEXT1

STRING2 = TEXT2

EXCEPTIONS

STRING1_TOO_SMALL = 1

STRING2_TOO_SMALL = 2

POS_NOT_VALID = 3

OTHERS = 4.

CASE SY-SUBRC.

WHEN 0.

WRITE: / TEXT, / TEXT1, / TEXT2.

WHEN 1.

WRITE 'Target field 1 too short!'.

WHEN 2.

WRITE 'Target field 2 too short!'.

WHEN 3.

WRITE 'Invalid split position!'.

WHEN 4.

WRITE 'Other errors!'.

ENDCASE.

The function module splits an input field at a particular position into two output fields. If the contents of POSITION are in the interval [4,6], the function module is executed without an exception being triggered. For the intervals [1,3] and [7,9], the system triggers the exceptions STRING2_TOO_SMALL and STRING2_TOO_SMALL respectively. For all other values of POSITION, the exception POS_NOT_VALID is triggered.

reward if helpful,

keerthi.

Message was edited by: keerthi kiran varanasi

Read only

Former Member
0 Likes
1,126

Hi,

DATA: T_MATNR TYPE STANDARD TABLE OF MARA WITH HEADER LINE.

SELECT * FROM MARA INTO TABLE T_MATNR.

PERFORM DISPLAY USING T_MATNR[]

'ASDFSDF'.

FORM DISPLAY USING PT_MATNR LIKE T_MATNR[]

PV_KUNNR TYPE KUNNR.

DATA: PWA_MARA TYPE MARA.

WRITE: / PV_KUNNR.

LOOP AT PT_MATNR INTO PWA_MARA.

WRITE: / PWA_MARA-MATNR.

ENDLOOP.

ENDFORM.

data: v_answer.

CALL FUNCTION 'POPUP_TO_CONFIRM'

EXPORTING

text_question = 'Test'

TEXT_BUTTON_1 = 'Yes'(001)

TEXT_BUTTON_2 = 'No'(002)

IMPORTING

ANSWER = v_answer

EXCEPTIONS

TEXT_NOT_FOUND = 1

OTHERS = 2

.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Thanks,

Naren

Read only

Former Member
0 Likes
1,126

HI

GOOD

function module->

this is sample of function modules and it differs of passing parameters for different function modules

CALL FUNCTION 'SUSR_USER_ADDRESS_READ'

EXPORTING

user_name = ld_userid

  • READ_DB_DIRECTLY = ' '

IMPORTING

user_address = ld_address

user_usr03 = ld_usr03

EXCEPTIONS

user_address_not_found = 1

OTHERS = 2.

  • Get additional user address details (i.e. email)

CALL FUNCTION 'ADDR_PERS_COMP_COMM_GET'

EXPORTING

  • ADDRESS_HANDLE = ' '

ADDRESS_NUMBER = ld_address-addrnumber

  • DATE_FROM = '00010101'

  • LANGUAGE = SY-LANGU

  • PERSON_HANDLE = ' '

PERSON_NUMBER = ld_address-persnumber

table_type = 'ADSMTP' "email details

  • Other valid entries for table_type include:

  • ADFAX for fax details

  • ADTEL for telephone details

  • IMPORTING

  • RETURNCODE =

tables

comm_table = ld_smtp

  • ERROR_TABLE =

EXCEPTIONS

PARAMETER_ERROR = 1

ADDRESS_NOT_EXIST = 2

PERSON_NOT_EXIST = 3

INTERNAL_ERROR = 4

OTHERS = 5.

SUBROUTINE->

Data declarations in procedures create local data types and objects that are only visible within that procedure. There are two kinds of data types and objects – dynamic and static. Dynamic data objects only exist while the subroutine is running, while static objects still exist after the subroutine has finished running, and retain their values until the next time the subroutine is called. Field symbols can be declared locally. You can also use a special kind of data object for subroutines – copies of global data on a local data stack. You define and address them using field symbols.

EXAMPLE->

REPORT demo_mod_tech_data_types .

TYPES word(10) TYPE c.

DATA text TYPE word.

text = '1234567890'. WRITE / text.

PERFORM datatest.

WRITE / text.

FORM datatest.

TYPES word(5) TYPE c.

DATA text TYPE word.

text = 'ABCDEFGHJK'. WRITE / text.

ENDFORM.

THANKS

MRUTYUN

Read only

Former Member
0 Likes
1,126

Send me ur e Mail ID i will send you a nice document on Funtion Builder.Write keyword "function" in ABAP editor and you will get help on how to pass parameters.

Please Give Points in Helpful

Regards

Read only

0 Likes
1,126

Hi Tushar,

My id is sap_crm01@yahoo.com.

Thanks in advance.

Read only

0 Likes
1,126

Hi Tushar,

My id is sap_crm01@yahoo.com.

Thanks in advance.

Read only

0 Likes
1,126

Hi Tushar,

My mail id is sap_crm01@yahoo.com.

Thanks in advance.

Read only

0 Likes
1,126

Hi Tushar,

My id is sap_crm01@yahoo.com.

Thanks in advandce

Read only

Former Member
0 Likes
1,126

hi,

<b>Passing paramter in subroutine.</b>

You can define the subroutine like

PERFORM subr_identifier [parameter_list].

This statement calls the subroutine specified with the name subr_identifier and assigns the actual parameters specified in parameter_list to the formal parameters of the subroutine.

and the parameter list can be like

... [TABLES itab1 itab2 ...]

[USING a1 a2 ...]

[CHANGING a1 a2 ...].

These additions assign actual parameters to the formal parameters from the parameter interface for the subroutine subr. You can specify all data objects whose data type matches the typing of the corresponding formal parameter (see Check Typing) as actual parameters. Each formal parameter assumes all the properties of the actual parameter assigned to it when it is called.

... TABLES itab1 itab2 ...

If you specify the addition TABLES, each table parameter t1 t2 ... for the subroutine called that is defined with the addition TABLES to the FORM statement must be assigned an internal table itab as the actual parameter. The assignment of the actual parameters to the formal parameters takes place using their positions in the lists t1 t2 ... and itab1 itab2 ... .

You can only specify standard tables for itab. Transfer takes place by means of a reference. If a specified table itab has a header line, this is also transferred; otherwise, the header line in the corresponding table parameter t is blank when it is called.

<b>Example</b>

Static call of the internal subroutine select_sflight transferring a table parameter.

PARAMETERS: p_carr TYPE sflight-carrid,

p_conn TYPE sflight-connid.

DATA sflight_tab TYPE STANDARD TABLE OF sflight.

...

PERFORM select_sflight TABLES sflight_tab

USING p_carr p_conn.

...

FORM select_sflight TABLES flight_tab LIKE sflight_tab

USING f_carr TYPE sflight-carrid

f_conn TYPE sflight-connid.

SELECT *

FROM sflight

INTO TABLE flight_tab

WHERE carrid = f_carr AND

connid = f_conn.

ENDFORM.

... USING a1 a2 ...

... CHANGING a1 a2 ...

If you specify the additions USING and CHANGING, an actual parameter a1 a2 ... of the appropriate type must be assigned to each of the formal parameters u1 u2 ... and c1 c2 ... defined with the same additions to the FORM statement. The actual parameters specified after USING and CHANGING form one shared list. They are assigned to the formal parameters after the position in the shared list. The type of parameter transfer is defined with the additions USING and CHANGING to the FORM statement. The addition USING must be before CHANGING. Otherwise, the assignment of the actual parameters to the additions USING and CHANGING is irrelevant to the PERFORM statement. It is also irrelevant whether only one or both of the additions is specified.

<b>Example</b>

The following five PERFORM statements mean the same but only the fourth is recommended, since it is the only one that documents the interface of the subroutine called.

DATA: a1 TYPE string,

a2 TYPE string,

a3 TYPE string,

a4 TYPE string.

PERFORM test USING a1 a2 a3 a4.

PERFORM test CHANGING a1 a2 a3 a4.

PERFORM test USING a1 CHANGING a2 a3 a4.

PERFORM test USING a1 a2 CHANGING a3 a4.

PERFORM test USING a1 a2 a3 CHANGING a4.

...

FORM test USING p1 TYPE string

p2 TYPE string

CHANGING value(p3) TYPE string

value(p4) TYPE string.

...

ENDFORM.

Regards,

Richa