BC ABAP Programming PHẦN 4 pptx

153 391 0
BC ABAP Programming PHẦN 4 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

BC - ABAP Programming SAP AG The Parameter Interface 462 December 1999 screen, that is, in a PAI module, in the AT SELECTION-SCREEN event, or after an interactive list event. Specifying the Type of Formal Parameters Formal parameters can have any valid ABAP data type. You can specify the type of a formal parameter, either generically or fully, using the TYPE or LIKE addition. If you specify a generic type, the type of the formal parameter is either partially specified or not specified at all. Any attributes that are not specified are inherited from the corresponding actual parameter when the subroutine is called. If you specify the type fully, all of the technical attributes of the formal parameter are defined with the subroutine definition. The following remarks about specifying the types of parameters also apply to the parameters of other procedures (function modules and methods). If you have specified the type of the formal parameters, the system checks that the corresponding actual parameters are compatible when the subroutine is called. For internal subroutines, the system checks this in the syntax check. For external subroutines, the check cannot occur until runtime. By specifying the type, you ensure that a subroutine always works with the correct data type. Generic formal parameters allow a large degree of freedom when you call subroutines, since you can pass data of any type. This restricts accordingly the options for processing data in the subroutine, since the operations must be valid for all data types. For example, assigning one data object to another may not even be possible for all data types. If you specify the types of subroutine parameters, you can perform a much wider range of operations, since only the data appropriate to those operations can be passed in the call. If you want to process structured data objects component by component in a subroutine, you must specify the type of the parameter. Specifying Generic Types The following types allow you more freedom when using actual parameters. The actual parameter need only have the selection of attributes possessed by the formal parameter. The formal parameter adopts its remaining unnamed attributes from the actual parameter. Type specification Check for actual parameters No type specification TYPE ANY The subroutine accepts actual parameters of any type. The formal parameter inherits all of the technical attributes of the actual parameter. TYPE C, N, P, or X The subroutine only accepts actual parameters with the type C, N, P, or X. The formal parameter inherits the field length and DECIMALS specification (for type P) from the actual parameter. TYPE TABLE The system checks whether the actual parameter is a standard internal table. This is a shortened form of TYPE STANDARD TABLE (see below). TYPE ANY TABLE The system checks whether the actual parameter is an internal table. The formal parameter inherits all of the attributes (line type, table type, key) from the actual parameter. TYPE INDEX TABLE The system checks whether the actual parameter is an index table (standard or sorted table). The formal parameter inherits all of the attributes (line type, table type, key) from the actual parameter. TYPE STANDARD TABLE The system checks whether the actual parameter is a standard internal table. The formal parameter inherits all of the attributes (line type, key) from the actual parameter. SAP AG BC - ABAP Programming The Parameter Interface December 1999 463 TYPE SORTED TABLE The system checks whether the actual parameter is a sorted table. The formal parameter inherits all of the attributes (line type, key) from the actual parameter. TYPE HASHED TABLE The system checks whether the actual parameter is a hashed table. The formal parameter inherits all of the attributes (line type, key) from the actual parameter. Note that formal parameters inherit the attributes of their corresponding actual parameters dynamically at runtime, and so they cannot be identified in the program code. For example, you cannot address an inherited table key statically in a subroutine, but you probably can dynamically. TYPES: BEGIN OF LINE, COL1, COL2, END OF LINE. DATA: WA TYPE LINE, ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1, KEY(4) VALUE 'COL1'. WA-COL1 = 'X'. INSERT WA INTO TABLE ITAB. WA-COL1 = 'Y'. INSERT WA INTO TABLE ITAB. PERFORM DEMO USING ITAB. FORM DEMO USING P TYPE ANY TABLE. READ TABLE P WITH TABLE KEY (KEY) = 'X' INTO WA. ENDFORM. The table key is addressed dynamically in the subroutine. However, the static address READ TABLE P WITH TABLE KEY COL1 = 'X' INTO WA. is syntactically incorrect, since the formal parameter P does not adopt the key of table ITAB until runtime. Specifying Full Types When you use the following types, the technical attributes of the formal parameters are fully specified. The technical attributes of the actual parameter must correspond to those of the formal parameter. Type specification Technical attributes of the formal parameter TYPE D, F, I, or T The formal parameter has the technical attributes of the predefined elementary type TYPE <type> The formal parameter has the type <type> This is a data type defined within the program using the TYPES statement, or a type from the ABAP Dictionary TYPE REF TO <cif> The formal parameter is a reference variable (ABAP Objects) for the class or interface <cif> TYPE LINE OF <itab> The formal parameter has the same type as a line of the internal table <itab> defined using a TYPES statement or defined in the ABAP Dictionary LIKE <f> The formal parameter has the same type as an internal data object <f> or structure, or a database table from the ABAP Dictionary BC - ABAP Programming SAP AG The Parameter Interface 464 December 1999 When you use a formal parameter that is fully typed, you can address its attributes statically in the program, since they are recognized in the source code. Structured Formal Parameters Since formal parameters can take any valid ABAP data type, they can also take structures and internal tables with a structured line type, as long as the type of the formal parameter is fully specified . You can address the components of the structure statically in the subroutine. Generic Structures If you pass a structured actual parameter generically to a formal parameter whose type is not correctly specified, you cannot address the components of the structure statically in the subroutine. For internal tables, this means that only line operations are possible. To access the components of a generically passed structure, you must use field symbols, and the assignment ASSIGN COMPONENT <idx>|<name> OF STRUCTURE <s> TO <FS>. [Page 214] <idx> is interpreted as the component number and the contents of <name> are interpreted as a component name in the generic structure <s>. DATA: BEGIN OF LINE, COL1 VALUE 'X', COL2 VALUE 'Y', END OF LINE. DATA COMP(4) VALUE 'COL1'. PERFORM DEMO USING LINE. FORM DEMO USING P TYPE ANY. FIELD-SYMBOLS <FS>. ASSIGN COMPONENT COMP OF STRUCTURE P TO <FS>. WRITE <FS>. ASSIGN COMPONENT 2 OF STRUCTURE P TO <FS>. WRITE <FS>. ENDFORM. The output is: XY The components COL1 and COL2 of the structure of P (passed generically) are assigned to the field symbol <FS>. You cannot address the components directly either statically or dynamically. Fitting Parameters into Structures Instead of using TYPE or LIKE, you can specify the type of a structure as follows: <p i > [STRUCTURE <s>] where <s> is a local structure in the program (data object, not a type) or a flat structure from the ABAP Dictionary. The formal parameter is structured according to <s>, and you can address its individual components in the subroutine. When the actual parameter is passed, the system only checks to ensure that the actual parameter is at least as long as the structure. STRUCTURE therefore allows you to force a structured view of any actual parameter. SAP AG BC - ABAP Programming The Parameter Interface December 1999 465 DATA: BEGIN OF LINE, COL1, COL2, END OF LINE. DATA TEXT(2) VALUE 'XY'. PERFORM DEMO USING TEXT. FORM DEMO USING P STRUCTURE LINE. WRITE: P-COL1, P-COL2. ENDFORM. The output is: XY The string TEXT is fitted into the structure LINE. The TABLES Addition To ensure compatibility with previous releases, the following addition is still allowed before the USING and CHANGING additions: FORM <subr> TABLES <itab i > [TYPE <t>|LIKE <f>] The formal parameters <itab i > 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. 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. BC - ABAP Programming SAP AG Terminating Subroutines 466 December 1999 Terminating Subroutines A subroutine normally ends at the ENDFORM statement. However, you can terminate them earlier by using the EXIT or CHECK statement. When you terminate a subroutine with EXIT or CHECK, the current values of the output parameters (CHANGING parameters passed by value) are passed back to the corresponding actual parameter. Use EXIT to terminate a subroutine unconditionally. The calling program regains control at the statement following the PERFORM statement. PROGRAM FORM_TEST. PERFORM TERMINATE. WRITE 'The End'. FORM TERMINATE. WRITE '1'. WRITE '2'. WRITE '3'. EXIT. WRITE '4'. ENDFORM. The produces the following output: 1 2 3 The End In this example, the subroutine TERMINATE is terminated after the third WRITE statement. Use CHECK to terminate a subroutine conditionally. If the logical expression in the CHECK statement is untrue, the subroutine is terminated, and the calling program resumes processing after the PERFORM statement. PROGRAM FORM_TEST. DATA: NUM1 TYPE I, NUM2 TYPE I, RES TYPE P DECIMALS 2. NUM1 = 3. NUM2 = 4. PERFORM DIVIDE USING NUM1 NUM2 CHANGING RES. NUM1 = 5. NUM2 = 0. PERFORM DIVIDE USING NUM1 NUM2 CHANGING RES. NUM1 = 2. NUM2 = 3. PERFORM DIVIDE USING NUM1 NUM2 CHANGING RES. FORM DIVIDE USING N1 N2 CHANGING R. CHECK N2 NE 0. R = N1 / N2. WRITE: / N1, '/', N2, '=', R. ENDFORM. The produces the following output: 3 / 4 = 0.75 SAP AG BC - ABAP Programming Terminating Subroutines December 1999 467 2 / 3 = 0.67 In this example, the system leaves the subroutine DIVIDE during the second call after the CHECK statement because the value of N2 is zero. If the EXIT or CHECK statement occurs within a loop in a subroutine, it applies to the loop, and not to the subroutine. EXIT and CHECK terminate loops in different ways (see Loops [Page 246]), but behave identically when terminating subroutines. BC - ABAP Programming SAP AG Calling Subroutines 468 December 1999 Calling Subroutines You call subroutines using the statement PERFORM [USING <p i > ] [CHANGING <p i > ]. Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). Once a subroutine has finished running, the calling program carries on processing after the PERFORM statement. You can use the USING and CHANGING additions to supply values to the parameter interface of the subroutine. Naming Subroutines [Page 469] Passing Parameters to Subroutines [Page 472] SAP AG BC - ABAP Programming Naming Subroutines December 1999 469 Naming Subroutines With the PERFORM statement, you can call subroutines which are coded in the same ABAP program (internal calls), or subroutines which are coded in other ABAP programs (external calls). You can also specify the name of the subroutine dynamically at runtime, and call subroutines from a list. Internal Subroutine Calls To call a subroutine defined in the same program, you need only specify its name in the PERFORM statement: PERFORM <subr> [USING <p i > ] [CHANGING <p i > ]. The internal subroutine can access all of the global data of the calling program. PROGRAM FORM_TEST. DATA: NUM1 TYPE I, NUM2 TYPE I, SUM TYPE I. NUM1 = 2. NUM2 = 4. PERFORM ADDIT. NUM1 = 7. NUM2 = 11. PERFORM ADDIT. FORM ADDIT. SUM = NUM1 + NUM2. PERFORM OUT. ENDFORM. FORM OUT. WRITE: / 'Sum of', NUM1, 'and', NUM2, 'is', SUM. ENDFORM. The produces the following output: Sum of 2 and 4 is 6 Sum of 7 and 11 is 18 In this example, two internal subroutines ADDIT and OUT are defined at the end of the program. ADDIT is called from the program and OUT is called from ADDIT. The subroutines have access to the global fields NUM1, NUM2, and SUM. External subroutine calls The principal function of subroutines is for modularizing and structuring local programs. However, subroutines can also be called externally from other ABAP programs. In an extreme case, you might have an ABAP program that contained nothing but subroutines. These programs cannot run on their own, but are used by other ABAP programs as pools of external subroutines. However, if you want to make a function available throughout the system, you should use function modules instead of external subroutines. You create function modules in the ABAP Workbench using the Function Builder. They are stored in a central library, and have a defined release procedure. You can encapsulate functions and data in the attributes and methods of classes in ABAP Objects. For any requirements that exceed pure functions, you can use global classes instead of external subroutines. BC - ABAP Programming SAP AG Naming Subroutines 470 December 1999 When you call a subroutine externally, you must know the name of the program in which it is defined: PERFORM <subr>(<prog>) [USING <p i > ] [CHANGING <p i > ] [IF FOUND]. You specify the program name <prog> statically. You can use the IF FOUND option to prevent a runtime error from occurring if the program <prog> does not contain a subroutine <sub>. In this case, the system simply ignores the PERFORM statement. When you call an external subroutine, the system loads the whole of the program containing the subroutine into the internal session of the calling program (if it has not already been loaded). For further information, refer to Memory Structure of an ABAP Program [Page 66]. In order to save memory space, you should keep the number of subroutines called in different programs to a minimum. Suppose a program contains the following subroutine: PROGRAM FORMPOOL. FORM HEADER. WRITE: / 'Program started by', SY-UNAME, / 'on host', SY-HOST, 'date:', SY-DATUM, 'time:', SY-UZEIT. ULINE. ENDFORM. The subroutine can then be called from another program as follows: PROGRAM FORM_TEST. PERFORM HEADER(FORMPOOL) IF FOUND. In this example, no data is passed between calling program and subroutine. Specifying Subroutines Dynamically You can specify the name of a subroutine and, in the case of external calls, the name of the program in which it occurs, dynamically as follows: PERFORM (<fsubr>)[IN PROGRAM (<fprog>)][USING <p i > ] [CHANGING <p i > ] [IF FOUND]. The names of the subroutine and the external program are the contents of the fields <fsubr> and <fprog> respectively. By using the option IF FOUND, you can prevent a runtime error from being triggered if <fprog> does not contain a subroutine with the name <fsubr>. If you omit the parentheses, this variant of the PERFORM statement behaves like the static variant. Suppose a program contains the following subroutines: PROGRAM FORMPOOL. FORM SUB1. WRITE: / 'Subroutine 1'. ENDFORM. FORM SUB2. WRITE: / 'Subroutine 2'. ENDFORM. Dynamic subroutine specification: PROGRAM FORM_TEST. DATA: PROGNAME(8) VALUE 'FORMPOOL', SUBRNAME(8). SAP AG BC - ABAP Programming Naming Subroutines December 1999 471 SUBRNAME = 'SUB1'. PERFORM (SUBRNAME) IN PROGRAM (PROGNAME) IF FOUND. SUBRNAME = 'SUB2'. PERFORM (SUBRNAME) IN PROGRAM (PROGNAME) IF FOUND. The produces the following output: Subroutine 1 Subroutine 2 The character field PROGNAME contains the name of the program, in which the subroutines are contained. The names of the subroutines are assigned to the character field SUBRNAME. Calling Subroutines from a List You can call a subroutine from a list as follows: PERFORM <idx> OF <subr 1 > <subr 2 > <subr n >. The system calls the subroutine specified in the subroutine list in position <idx>. You can only use this variant of the PERFORM statement for internal subroutine calls, and only for subroutines without a parameter interface. The field <idx> can be a variable or a literal. PROGRAM FORM_TEST. DO 2 TIMES. PERFORM SY-INDEX OF SUB1 SUB2. ENDDO. FORM SUB1. WRITE / 'Subroutine 1'. ENDFORM. FORM SUB2. WRITE / 'Subroutine 2'. ENDFORM. The produces the following output: Subroutine 1 Subroutine 2 In this example, the two internal subroutines, SUB1 and SUB2, are called consecutively from a list. [...]... depending on their corresponding technical attributes December 1999 47 3 BC - ABAP Programming SAP AG Passing Parameters to Subroutines 47 4 December 1999 SAP AG BC - ABAP Programming Examples of Subroutines Examples of Subroutines 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 =... FORM_TEST DATA: A1 TYPE P DECIMALS 3, A2 TYPE I, A3 TYPE D, A4 TYPE SPFLI-CARRID, A5 TYPE C PERFORM SUBR USING A1 A2 A3 A4 A5 PERFORM SUBR CHANGING A1 A2 A3 A4 A5 PERFORM SUBR USING A1 A2 A3 CHANGING A4 A5 FORM SUBR USING VALUE(F1) TYPE P VALUE(F2) TYPE I F3 LIKE A3 CHANGING VALUE(F4) TYPE SPFLI-CARRID F5 ENDFORM 47 2 December 1999 SAP AG BC - ABAP Programming Passing Parameters to Subroutines This example... all other values of POSITION, the exception POS_NOT_VALID is triggered 49 0 December 1999 SAP AG BC - ABAP Programming Calling Function Modules December 1999 49 1 BC - ABAP Programming SAP AG Creating Function Modules Creating Function Modules You can only create function modules and function groups using the Function Builder in the ABAP Workbench For further information, refer to Creating New Function... particularly to the interface Programs that use a released function module will not cease to work if the function module is changed Function Groups [Page 48 4] Calling Function Modules [Page 48 6] Creating Function Modules [Page 49 2] December 1999 48 3 BC - ABAP Programming SAP AG Function Groups Function Groups Function groups are containers for function modules You cannot execute a function group When you... 150.00 A310 150.00 B 747 500.00 December 1999 48 1 BC - ABAP Programming SAP AG Shared Data Areas A310 150.00 All of the programs can access the table work area SFLIGHT, declared with the TABLES statement In the subroutine TABTEST2, the global work area is protected against changes in the subroutine by the LOCAL statement 48 2 December 1999 SAP AG BC - ABAP Programming Function Modules Function Modules... source code of our function module To do this, choose Source code in the Function Builder This opens the ABAP Editor for the include program LU (see Function Groups [Page 48 4] ) This is the include that will hold the program code for the function module; 49 4 December 1999 SAP AG BC - ABAP Programming Creating Function Modules Change F Module: READ_SPFLI_INTO_TABLE/LDEMO_SPFLI_U01 FUNCTION READ_SPFLI_INTO_TABLE... However, you can also pass internal tables with other parameters if you specify the parameter type appropriately 48 6 December 1999 SAP AG BC - ABAP Programming Calling Function Modules You can specify the types of the interface parameters, either by referring to ABAP Dictionary types or elementary ABAP types When you call a function module, you must ensure that the actual parameter and the interface parameters... to call a function module is to use the Insert statement function in the ABAP Editor If you select Call Function and specify the name of the function module (F4 help is available), the system inserts a CALL FUNCTION statements with all of the options of that function module in the source code 48 8 December 1999 SAP AG BC - ABAP Programming Calling Function Modules Insert Statement CALL FUNCTION STRING_SPLIT_AT_POSITION... number → SY-MSGNO • SY-MSGV1 to SY-MSGV4 (contents of fields to , included in a message) You can use the system fields to trigger the message from the calling program To ensure that you use the right data types for the actual parameters, you must refer to the function module interface If you double-click the name of a function December 1999 48 9 BC - ABAP Programming SAP AG Calling Function Modules... NUM1, 'and', NUM2, 'is', SUM ENDFORM 48 0 December 1999 SAP AG BC - ABAP Programming Shared Data Areas A calling program FORM_TEST includes INCOMMON and calls the subroutine ADDIT from the program FORMPOOL PROGRAM FORM_TEST INCLUDE INCOMMON NUM1 = 2 NUM2 = 4 PERFORM ADDIT(FORMPOOL) NUM1 = 7 NUM2 = 11 PERFORM ADDIT(FORMPOOL) This produces the following output: Sum of 2 and 4 is 6 Sum of 7 and 11 is 18 The . technical attributes. BC - ABAP Programming SAP AG Passing Parameters to Subroutines 47 4 December 1999 SAP AG BC - ABAP Programming Examples of Subroutines December 1999 47 5 Examples of Subroutines Example. the subroutine. Naming Subroutines [Page 46 9] Passing Parameters to Subroutines [Page 47 2] SAP AG BC - ABAP Programming Naming Subroutines December 1999 46 9 Naming Subroutines With the PERFORM. loops in different ways (see Loops [Page 246 ]), but behave identically when terminating subroutines. BC - ABAP Programming SAP AG Calling Subroutines 46 8 December 1999 Calling Subroutines You

Ngày đăng: 09/08/2014, 14:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan