‎2007 Nov 27 11:33 AM
1.what is the external subroutines? waht is the use?please give example?
2.what is passby value and passsby reference and pass by result?
‎2007 Nov 27 11:37 AM
Hi,
1)external subroutiene is the one which is present in another program.
we will call that by using the syntax
PERFORM <name of subroutine> (<Program name>) IF FOUND.
2)
rgds,
bharat.
‎2007 Nov 27 11:39 AM
difference between external & internal subroutine :
Internal subroutines in the sense ..subroutines which are defined and used in a same program...
external in the sense if you create a sub routine in one program and you're calling this subroutine in another program ..then this is external subroutine.
Uses of External Sub routines:
1)Reuseability
2) Easy to Maintain
Refer these links for your query 2,
http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db982c35c111d1829f0000e829fbfe/content.htm
http://help.sap.com/saphelp_46c/helpdata/en/fc/eb2d67358411d1829f0000e829fbfe/content.htm
Hope this helps.
Reward points if u find helpful.
Tushar
‎2007 Nov 27 11:39 AM
1. Pass by value and pass by reference ( getting result )
a) Pass by value
PERFORM subr USING a1 a2 a3 a4 a5
Click for example: http://help.sap.com/saphelp_47x200/helpdata/en/9f/db979d35c111d1829f0000e829fbfe/frameset.htm
b) Pass by reference
PERFORM subr CHANGING a1 a2 a3 a4 a5
Click for example Example: http://help.sap.com/saphelp_47x200/helpdata/en/9f/db979035c111d1829f0000e829fbfe/frameset.htm
Go thru this docu
SUBROUTINES
The process of breaking down a large program into smaller modules is supported by ABAP/4 through subroutine, also called forms.
Subroutines are programs modules, which can be called from ABAP/4 programs. Frequently used parts of program can be put into subroutines and these subroutines can be called explicitly from the program. You use subroutines mainly to modularize and structure your program.
Defining Subroutines
A subroutine is block of code introduced by FORM and concluded by ENDFORM. Following syntax is used to define a form or subroutine:
FORM ].
Parameters are optional. i.e., you can call subroutine without passing any parameter
Perform SUB1.
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.
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
*********************************************
External subroutine is perform statement is one program and form routine is in another program.
sample code...
How to call a subroutine from SAPscripts
The Form :
/:PERFORM CDE_CENT IN PROGRAM ZKRPMM_PERFORM_Z1MEDRUCK
/:USING &EKKO-EBELN&
/:CHANGING &CDECENT&
/:ENDPERFORM
The report :
REPORT zkrpmm_perform_z1medruck .
DATA : BEGIN OF it_input_table OCCURS 10.
INCLUDE STRUCTURE itcsy.
DATA : END OF it_input_table.
déclaration de la table output_table contenant les
variables exportées
DATA : BEGIN OF it_output_table OCCURS 0.
INCLUDE STRUCTURE itcsy.
DATA : END OF it_output_table.
DATA : w_ebeln LIKE ekko-ebeln,
w_vbeln LIKE vbak-vbeln,
w_zcdffa LIKE vbak-zcdffa.
*----
*
FORM CDE_CENT
*
*----
*
FORM cde_cent TABLES input output.
it_input_table[] = input[].
it_output_table[] = output[].
READ TABLE it_input_table INDEX 1.
MOVE it_input_table-value TO w_ebeln.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = w_ebeln
IMPORTING
output = w_ebeln.
SELECT SINGLE zcdffa FROM ekko
INTO w_zcdffa
WHERE ebeln = w_ebeln.
it_output_table-name = 'CDECENT'.
MOVE w_zcdffa TO it_output_table-value.
MODIFY it_output_table INDEX 1.
output[] = it_output_table[].
ENDFORM.
*************************************************************************
/: PERFORM
/: USING &INVAR1&
/: USING &INVAR2&
......
/: CHANGING &OUTVAR1&
/: CHANGING &OUTVAR2&
......
/: ENDPERFORM
INVAR1 and INVAR2 are variable symbols and may be of any of the four SAPscript symbol types.
OUTVAR1 and OUTVAR2 are local text symbols and must therefore be character strings.
Example:
In script form
/: PERFORM READ_TEXTS IN PROGRAM 'Z08M1_FORM_EKFORM1'
/: USING &EKKO-EKORG&
/: USING &EKPO-WERKS&
/: USING &EKKO-EKGRP&
/: USING &EKKO-BSTYP&
/: CHANGING &COMPNAME&
/: CHANGING &SENDADR&
/: CHANGING &INVCADR&
/: CHANGING &COMPADR&
/: CHANGING &COVERLTR&
/: CHANGING &SHIPADR&
/: CHANGING &REMINDER&
/: CHANGING &REJECTION&
/: CHANGING &POSTADR&
/: CHANGING &LOGO&
/: ENDPERFORM
In program
*----
*
FORM Read_texts - To extract the standard texts from the table *
*----
*
FORM READ_TEXTS TABLES IN_PAR STRUCTURE ITCSY
OUT_PAR STRUCTURE ITCSY.
DATA : L_EKORG TYPE EKORG,
L_WERKS TYPE WERKS_D,
L_BSTYP TYPE BSTYP,
L_EKGRP TYPE BKGRP.
READ TABLE IN_PAR WITH KEY 'EKKO-EKORG' .
CHECK SY-SUBRC = 0.
L_EKORG = IN_PAR-VALUE.
READ TABLE IN_PAR WITH KEY 'EKPO-WERKS' .
CHECK SY-SUBRC = 0.
L_WERKS = IN_PAR-VALUE.
READ TABLE IN_PAR WITH KEY 'EKKO-EKGRP' .
CHECK SY-SUBRC = 0.
L_EKGRP = IN_PAR-VALUE.
READ TABLE IN_PAR WITH KEY 'EKKO-BSTYP' .
CHECK SY-SUBRC = 0.
L_BSTYP = IN_PAR-VALUE.
CLEAR Z08M1_ORG_TEXTS.
SELECT SINGLE * FROM Z08M1_ORG_TEXTS WHERE EKORG = L_EKORG
AND WERKS = L_WERKS
AND EKGRP = L_EKGRP
AND BSTYP = L_BSTYP.
IF SY-SUBRC NE 0.
SELECT SINGLE * FROM Z08M1_ORG_TEXTS WHERE EKORG = L_EKORG
AND WERKS = L_WERKS
AND EKGRP = L_EKGRP
AND BSTYP = SPACE.
ENDIF.
READ TABLE OUT_PAR WITH KEY 'COMPNAME'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_COMP.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'SENDADR'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_ADRS.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'INVCADR'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_INVC.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'COMPADR'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_CPAD.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'COVERLTR'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_COVR.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'SHIPADR'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_SHIP.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'REMINDER'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_RMDR.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'REJECTION'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_RJCT.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'POSTADR'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_POST.
MODIFY OUT_PAR INDEX SY-TABIX.
READ TABLE OUT_PAR WITH KEY 'LOGO'.
OUT_PAR-VALUE = Z08M1_ORG_TEXTS-TXT_LOGO.
MODIFY OUT_PAR INDEX SY-TABIX.
ENDFORM.
*************************************************************************
REPORT ZMPO1 .
form get_freight tables in_par structure itcsy out_par structure itcsy.
tables: ekko,konv,t685t.
data: begin of itab occurs 0,
ebeln like ekko-ebeln,
knumv like ekko-knumv,
end of itab.
data: begin of itab1 occurs 0,
knumv like konv-knumv,
kposn like konv-kposn,
kschl like konv-kschl,
kbetr like konv-kbetr,
waers like konv-waers,
kwert like konv-kwert,
end of itab1.
data: begin of iout occurs 0,
kschl like konv-kschl,
vtext like t685t-vtext,
kbetr like konv-kbetr,
kwert like konv-kwert,
end of iout.
data v_po like ekko-ebeln.
read table in_par with key 'EKKO-EBELN'.
if sy-subrc = 0.
v_po = in_par-value.
select
ebeln
knumv
from ekko
into table itab
where ebeln = v_po.
if sy-subrc = 0.
loop at itab.
select
knumv
kposn
kschl
kbetr
waers
kwert
into table itab1
from konv
where knumv = itab-knumv and
kappl = 'M'.
endloop.
loop at itab1.
if itab1-kposn <> 0.
select single * from t685t
where kschl = itab1-kschl
and kappl = 'M'
and spras = 'EN'.
iout-vtext = t685t-vtext.
iout-kschl = itab1-kschl.
iout-kbetr = itab1-kbetr.
iout-kwert = itab1-kwert.
append iout.
clear iout.
endif.
endloop.
sort itab1 by kposn.
loop at iout.
sort iout by kschl.
if ( iout-kschl eq 'GSDC' OR
iout-kschl eq 'GSFR' OR
iout-kschl eq 'GSIR' ).
at end of kschl.
read table iout index sy-tabix.
sum.
write:/ iout-kschl,iout-vtext,iout-kwert.
out_par-name = 'A1'.
out_par-value = iout-vtext.
append out_par.
out_par-name = 'A2'.
out_par-value = iout-kwert.
append out_par.
endat.
endif.
endloop.
endif.
endif.
endform.
IN THE FORM I AM WRITING THIS CODE.
/:DEFINE &A1& = ' '
/:DEFINE &A2& = ' '
/:PERFORM GET_FREIGHT IN PROGRAM ZMFORM_PO1
/:USING &EKKO-EBELN&
/:CHANGING &A1&
/:CHANGING &A2&
/:ENDPERFORM
&A1&
&A2&
This Code is to be written in the PO form under ADDRESS window.
-
-
/:DEFINE &A1& = ' '
/:DEFINE &A2& = ' '
/:DEFINE &A3& = ' '
/:DEFINE &A4& = ' '
/:DEFINE &A5& = ' '
/:DEFINE &A6& = ' '
/:PERFORM GET_VENDOR IN PROGRAM ZMFORM_PO
/:USING &EKKO-EBELN&
/:CHANGING &A1&
/:CHANGING &A2&
/:CHANGING &A3&
/:CHANGING &A4&
/:CHANGING &A5&
/:CHANGING &A6&
/:ENDPERFORM
&A1&
&A2&
&A3&
&A4&
&A5&
&A6&
Regards
vasu
Regards
Vasu
‎2007 Nov 27 11:41 AM
external function is the function that is present in other program u can use it using perform n parameters can be passed as shown below.
PERFORM background(<object name>) USING syst-repid.
‎2007 Nov 27 11:45 AM
Subroutines: can be used locally and globally (external subroutine calls). Subroutines have a formal interface ( USING, CHANGING, TABLES parameters). The interface parameters can be passed BY VALUE or BY REFERENCE. Data that is defined within a subroutine (FROM ... ENDFORM.) is local: lifetime and visibility is limited to the moment the subroutine is called.
External Subroutines are subroutines that are called from another program. Typical example can be found in the way SAPscript and SMARTforms printprograms are called by RSNAST00. External Subroutines can be grouped into Subroutine Pools.
pass by value example:
def hop(onur)
onur=onur+1
end
ahmet=5
hop(ahmet)
>> ahmet
=> 5
pass by reference example:
def lala(node)
node.body="hoppa"
end
n= Node.new
lala(n)
n.body
=> "hoppa"