Sei sulla pagina 1di 65

ABAP Training

Program Modularisation
ABAP Training Program Modularisation 2

Why Modularisation?

 Improve program structure


 Make the program easier to maintain and update
 Improve readability
 Reduce redundancy
 Allow for component reuse
 Event processing
 ABAP is an event driven language. The processing associated with

each event is written in a program module


ABAP Training Program Modularisation 3

Modularisation in ABAP

 Types of modules in an ABAP program


 Subroutines

 Internal subroutines - source code is in the same ABAP program

as the calling procedure


 External subroutines - source code of external subroutines is in an

ABAP program other than the calling procedure


 Functions

 Stored in central library

 Offer a defined interface

 Event handling code blocks


ABAP Training Program Modularisation 4

Steps Involved In MODULARISATION

 Defining sub routines and function modules.


 Calling sub routines and function modules.
 Passing parameters in sub routines .
 Passing parameters in function modules.
ABAP Training Program Modularisation 5

Overview Of Modularisation
PROGRAM RSAAA10A .
CALL : CALCULATE
SUB ROUTINE
LOSS .

INTERNAL SUB
ROUTINE :
CALL : CALCULATE PROGRAM
EXTERNAL LOSS . RSAAA10C .
SUB ROUTINE
PROGRAM
EXTERNAL SUB
RSAAA10C .
ROUTINE :
CALL : CALCULATE
CALCULATE LOSS .
LOSS .
FUNCTION PROGRAM ABAP/4 FUNCTION
MODULE RSAAA10E . LIBRARY
CALL : CALCULATE Function Module:
LOSS . CALCULATE LOSS .
ABAP Training Program Modularisation 6

Subroutines

FORM <subr> [<pass>]


<Statement block>
ENDFORM.

• <subr> - name of the subroutine


• [<pass>] - data passing specification

PERFORM <subr> [<pass>]


[IN PROGRAM <prog>].
ABAP Training Program Modularisation 7

Global & Local Data


REPORT ZSAPTEST.
* Global Data TABLES
TABLES: ... Global Data
DATA: X, Y.
* Subroutine Call
PERFORM <name> USING X Y.

* Subroutine
FORM <name> USING A B. TABLES
* Local Data Global Data
DATA: S TYPE i. Formal Parameters
* Statements Local Data
ENDFORM.
ABAP Training Program Modularisation 8

Passing Data By Parameters


 Formal parameters - defined within FORM
 Actual parameter - specified with PERFORM
 Parameter types
 Input - used to pass data to subroutines

 Output - used to pass data from subroutines

 Input/output - pass data to & from subroutines

 Call by reference
 Call by value
 Normal scoping rules apply to calling & called data items
ABAP Training Program Modularisation 9

Different Methods Of Passing Data


Call by reference :
During a subroutine call, 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, the field contents in the calling program also
change.

Call by value :
During a subroutine call, the formal parameters are created as
copies of the actual parameters. The formal parameters have
memory of their own. Changes to the formal parameter have no
effect on the actual parameter.
ABAP Training Program Modularisation 10

Types Of Parameter Passing

a1
CALL BY VALUE

f1

a1

CALL BY REFERENCE
f1
ABAP Training Program Modularisation 11

Passing Data By Parameters

 Passing by reference
 USING or CHANGING

 USING for input parameters that are not changed

 CHANGING for output parameters that are changed

 Passing by value
 FORM <subr> … USING value(<fi>) …
ABAP Training Program Modularisation 12

Passing Data By Parameters

FORM <subr> [TABLES <formal table list>]


[USING <formal input list>]
[CHANGING <formal output list>]
ENDFORM.
PERFORM <subr> [TABLES <formal table list>]
[USING <formal input list>]
[CHANGING <formal output list>]
NOTE: no comma in parameters list
ABAP Training Program Modularisation 13

Passing Values From Actual To Formal Parameters

PROGRAM RSAAA10B .

PERFORM CALCULATE_LOSS

ACTUAL PARAMETERS
SFLIGHT-PRICE SFLIGHT-SEATSOCC REVENUE LOSS

1 1 3
2

PRICE SEATSOCC REVENUE LOSS

FORMAL PARAMETERS
1 . CALL BY VALUE
2 . CALL BY REFERENCE
3 . CALL BY VALUE & RESULT .
ABAP Training Program Modularisation 14

Parameters Passing With Types

PROGRAM RSAA10B . CALL BY VALUE :


: A1 --> F1 .
PERFORM <name> A2 --> F2 .
using A1 A2 A3 A3 --> F3 .
changing A4 A5 .
:
FORM <name> USING
VALUE(F1)
VALUE(F2)
VALUE(F3) CALL BY REFERENCE :
CHANGING F4 A4 F4 .
VALUE(F5) .
:
ENDFORM .

CALL BY VALUE & RESULT :


A5 F5 .
ABAP Training Program Modularisation 15

Parameters Passing With Type Checking


PROGRAM RSAA10C .
DATA : REVENUE . . . CALL BY VALUE :
A1 --> F1 .
PERFORM <name> A2 --> F2 .
using A1 A2 A3 A3 --> F3 .
changing A4 A5 .
:
FORM <name> USING
VALUE(F1) TYPE P
VALUE(F2) TYPE I
VALUE(F3) TYPE P
CHANGING F4 LIKE REVENUE
VALUE(F5) TYPE P .
CALL BY REFERENCE :
: A4 F4 .
ENDFORM .

CALL BY VALUE & RESULT :


A5 F5 .
ABAP Training Program Modularisation 16

Passing Structures As Parameters

PROGRAM RSAA10D .
TABLES : SFLIGHT .
DATA : REVENUE . . .
:
PERFORM write_sflight1 USING SFLIGHT .
PERFORM write_sflight2 USING SFLIGHT .
:
FORM write_sflight1 USING REC .
WRITE : / REC .
ENDFORM .
FORM write_sflight2 USING REC LIKE SFLIGHT .
WRITE : / REC-CARRID , REC-CONNID .. . . .
ENDFORM .
ABAP Training Program Modularisation 17

Passing Internal Tables As Parameters

PROGRAM RSAA10E .
TABLES : SFLIGHT .
DATA : TAB LIKE SFLIGHT OCCURS 50 WITH HEADER LINE .
:
PERFORM calc_write1 USING TAB[] .
PERFORM calc_ write2 TABLES TAB .
:
FORM calc_write1 USING TABBODY LIKE TAB[] .
DATA : TAB LIKE SFLIGHT .
LOOP AT TABBODY INTO TAB .
WRITE : / TAB-CARRID .
ENDLOOP .
ENDFORM .
FORM calc_write2 TABLES ITAB STRUCTURE TAB .
LOOP AT ITAB .
WRITE : / ITAB-CARRID .
ENDLOOP .
ENDFORM .
ABAP Training Program Modularisation 18

External Sub Routines

PERFORM <name> (<prog-name ) USING . . .

PROGRAM RSAA10F . PROGRAM RSAA10B .


TABLES : SFLIGHT , SPFLI . TABLES : SFLIGHT , SPFLI .
PERFORM calculate_loss (RSAAA10B) . FORM calculate_loss .
: :
: :
ENDFORM .
ABAP Training Program Modularisation 19
ABAP Training Program Modularisation 20

Terminating Subroutines

 Unconditional termination
 EXIT in the subroutine

 Processing resumes after the PERFORM statement

 Conditional termination
 CHECK <cond> in the subroutine

 If <cond> is not satisfied control leaves the subroutine and

processing begins after the calling PERFORM statement


ABAP Training Program Modularisation 21

Function Modules

 Classified in function groups


 Group of functions that serve similar purposes

 eg, calendar functions

 Stored in the function library


 Main difference between subroutines and functions is a clearly
defined interface for passing data between program and
function
ABAP Training Program Modularisation 22

Function Builder
FM Group: FIBU
FM_01 ...
FM_02 ...
Maintaining Function FM Group: ZIBU
FM_03 …
Using Function
Modules FM_04 ... Modules
FM_02
REPORT ...
Interface
TABLES: …
Import
Export
CALL FUNCTION
Tables
‘FM_02’
Exceptions
EXPORTING…
Program
IMPORTING...
Documentation
Administration
ABAP Training Program Modularisation 23

Calling a List of Functions


ABAP Training Program Modularisation 26

Testing Function Modules

 From the function builder: initial screen choose single test


 Assign values to the import parameters
 Press execute to display the test function module: results screen
 System provides results as well as a runtime analysis of
execution time
ABAP Training Program Modularisation 29

Calling Functions

CALL FUNCTION <function_name>


[EXPORTING f1=a1 …fn=an]
[IMPORTING f1=a1 … fn=an]
[CHANGING f1=a1 … fn=an]
[TABLES f1=a1 …fn=an]
[EXCEPTIONS e1=r1 … en=rn
[ERROR_MESSAGE = re]
[OTHERS = ro]].
ABAP Training Program Modularisation 30

Calling Functions

 EXPORTING f1=a1 … fn=an


 Enables the passing of actual parameters ai to formal input

parameters fi of the function


 IMPORTING f1=a1 … fn=an
 Enables the passing of formal output parameters fi of the

function to the corresponding actual parameters ai of the


calling program
ABAP Training Program Modularisation 31

Calling Functions

 CHANGING f1=a1 …fn=an


 Enables the passing of actual parameters ai to the formal paramters

fi, and, after processing the system passes the (changed) formal
parameters fi back to the actual parameters ai
 TABLES f1=a1 …fn=an
 Enables the passing of internal tables between actual and formal

parameters
 Tables are always passed by reference
ABAP Training Program Modularisation 32

Calling Functions

EXCEPTIONS f1=a1 … fn=an


 Functions are defined with exceptions

 The calling program determines whether and which exceptions it is

to handle itself
 The OTHERS clause covers all exceptions not explicitly specified

 If an error occurs in the function execution it is either handled in

the function or control immediately returns to the calling program (if


the exception is specified in the EXCEPTIONS parameter of the call)
ABAP Training Program Modularisation 33

ABAP Function Module: FILL_SEATTAB

Exceptions
NO_ENTRY REPORT ZXXXXXX.
CALL FUNCTION ‘FILL_SEATTAB’
EXPORTING
YEAR = YEAR
Exception defined in TABLES
SEATTAB = ITAB
function definition EXCEPTIONS
NO_ENTRY = 01
OTHERS = 02.

CASE SY-SUBRC.
Exception condition WHEN 1. WRITE ‘No Entry’.
WHEN 2. WRITE ‘Other Error’.
checked on return ENDCASE.
from function
ABAP Training Program Modularisation 34

Editpattern (Insert Statement)


ABAP Training Program Modularisation 36

Function Modules

MAINTAINING FUNCTION
MAINTAINING FUNCTION MODULES .
MODULES .
FUNCTION LIBRARY
PROGRAM ....
FM_02
INETRFACE FM GROUP : FIBU . CALL FUNCTION ‘FM_02 ‘
IMPORT EXPORTING
EXPORT
FM_01 . .
IMPORTING
TABLES FM_02 . . :
EXCEPTIONS FM GROUP : ZIBU .
PROGRAM FM_03 . .
DOCUMENTATION
ADMINISTRATION FM_04 . .
ABAP Training Program Modularisation 37

Creating Function Module

FUNCTION LIBRARY

FUNCTION MODULE Z_FREESEAT

CREATE

FUNCTION GROUP
APPLICATION ZDEM F4

FUNCTION GROUP SHORT DESC RESPONSIBLE


ZDEM TRAINING LOTZ
ABAP Training Program Modularisation 38

Calling Function Modules


Call function ‘calculate_revenue_loss’
Exporting
PAYMENTSUM = SFLIGHT-PAYMENTSUM
SEATSOCC = SFLIGHT-SEATSOCC
Price = SFLIGHT-price
Importing
Loss = loss
Revenue = revenue
Changing
Values = value1
Tables
ITAB = itab1.
Exception
Parameter_error = 1
Address_not_exist = 2
ABAP Training Program Modularisation 39

Function Group
 Group of logically related function modules which share the same
program context at runtime.
 A function group is the main program for the function modules it
contains. Functions that use the same data are normally assigned to the
same function group.
ABAP Training Program Modularisation 40

Documenting Functions

PROGRAM RSAA10G .
TABLES : SFLIGHT .
DATA : LOSS LIKE SFLIGHT-PAYMENTSUM .
CALL FUNCTION ‘CALCULATE_REVENUE_LOSS’
EXPORTING
PAYMENTSUM = SFLIGHT-PAYMENTSUM
SEATSOCC = SFLIGHT-SEATSOCC
PRICE = SFLIGHT-PRICE
IMPORTING
LOSS = LOSS
REVENUE = REVENUE .

DOCUMENTATION FOR CALCULATE_REVENUE_LOSS


PARAMETER NAME SHORT TEXT PARAMETER TYPE

PAYMENTSUM EARNED REVENUE 1


: : :
ABAP Training Program Modularisation 41

Describing The Interface

PROGRAM RSAA10G .
TABLES : SFLIGHT .
DATA : LOSS LIKE SFLIGHT-PAYMENTSUM .
CALL FUNCTION ‘CALCULATE_REVENUE_LOSS’
EXPORTING
PAYMENTSUM = SFLIGHT-PAYMENTSUM
SEATSOCC = SFLIGHT-SEATSOCC
PRICE = SFLIGHT-PRICE
IMPORTING
LOSS = LOSS
REVENUE = REVENUE .

ABAP/4 FUNCTION MODULE CALCULATE_REVENUE_LOSS


IMPORT PARAMETER REF. FIELD REF. TYPE PROPOSAL OPTIONAL REFERENCE
PAYMENTSUM ..
:
ABAP Training Program Modularisation 42

Changing Parameter

PROGRAM RSAA10G .
TABLES : SFLIGHT .
DATA : LOSS LIKE SFLIGHT-PAYMENTSUM .
CALL FUNCTION ‘CALCULATE_REVENUE_LOSS’
EXPORTING
PAYMENTSUM = SFLIGHT-PAYMENTSUM
SEATSOCC = SFLIGHT-SEATSOCC
PRICE = SFLIGHT-PRICE
IMPORTING

CHANGING

ABAP/4 FUNCTION MODULE CALCULATE_REVENUE_LOSS


IMPORT PARAMETER REF. FIELD REF. TYPE PROPOSAL OPTIONAL REFERENCE
:
IMPORT PARAMETER REF. FIELD REF. TYPE PROPOSAL OPTIONAL REFERENCE
:
CHANGING PARAMETER REF. FIELD REF. TYPE PROPOSAL OPTIONAL REFERENCE
ABAP Training Program Modularisation 43

Passing Internal Tables

PROGRAM RSAA10H .
TABLES : SFLIGHT .
DATA : ITAB LIKE BCAXX OCCURS 10 WITH HEADER LINE .
CALL FUNCTION ‘FILL_SEATTAB’
EXPORTING
YEAR = YEAR
TABLES
SEATTAB = ITAB .

ABAP/4 FUNCTION MODULE FILL_SEATTAB


IMPORT PARAMETER REF. FIELD REF. TYPE PROPOSAL OPTIONAL REFERENCE
:
IMPORT PARAMETER REF. FIELD REF. TYPE PROPOSAL OPTIONAL REFERENCE
:
CHANGING PARAMETER REF. FIELD REF. TYPE PROPOSAL OPTIONAL REFERENCE
ABAP Training Program Modularisation 44

Exception Handling

PROGRAM RSAA10H .
TABLES : SFLIGHT .
DATA : ITAB LIKE BCAXX OCCURS 10 WITH HEADER LINE .
CALL FUNCTION ‘FILL_SEATTAB’
EXPORTING
YEAR = YEAR ABAP/4 FUNCTION MODULE FILL_SEATTAB
TABLES
EXCEPTIONS NO_ENTRY .
ASGH = ITAB
EXCEPTIONS
NO_ENTRY = 01
OTHERS = 02 .

CASE SY-SUB ROUTINERC .


WHEN 1. WRITE ‘NOT ENTRY ‘.
WHEN 2. WRITE ‘OTHER ERROR’ .
ENDCASE .
ABAP Training Program Modularisation 45

Interfaces

Z_FREESEAT
GLOBAL INTERFACE
IMPORT PARAMETER REFERENCE FIELD PROPOSAL
:
EXPORT PARAMETER REFERENCE FIELD PROPOSAL

TABLE PARAMETER REFERENCE STRUCTURE


:
EXCEPTIONS
:

DOCUMENTATION
SHORT TEXT
PARAMETER
SEATSOCC
ABAP Training Program Modularisation 46

Program Editor

FUNCTION Z_FREESEAT

•IMPORTING
•EXPORTING
•CHANGING
•TABLES
•EXCEPTIONS

LOCAL DECLARATIONS

STATEMENTS

ENDFUNCTION .
ABAP Training Program Modularisation 47

Exceptions

CALL
Z_FREESEAT
EXCEPTIONS CALL FUNCTION EXPORTING
PLANE_OVERLOAD. IMPORTING
EXCEPTIONS
PLANE_OVERLOAD = 1.
CASE SY-SUBRC .
PROGRAM WHEN 1.
...
IF SEATSOCC > SEATSMAX . ENDCASE.
RAISE PLANE_OVERLOAD .
ENDIF .
ABAP Training Program Modularisation 48

Test Environment

CALL
FUNCTION MODULE Z_FREESEAT
EXPORT PARAMETER VALUE
:
IMPORT PARAMETER VALUE
:
TABLES No. OF LINES
ABAP Training Program Modularisation 49

Subroutines
MAIN PROGRAM

*SYSTEM DEFINED INCLUDE PROGRAMS


INCLUDE L<gr>TOP . “ GLOBAL DATA
*USER DEFINED INCLUDE PROGRAMS
INCLUDE L<gr>F01 . “ SUB ROUTINES.

CALL
ABAP/4 PROGRAM L<gr>F01.
FUNCTION . . .
:
FORM SUB ROUTINE1 USING . .
:
PERFORM SUB ROUTINE1 USING . .
ENDFORM .
:
ENDFUNCTION .
ABAP Training Program Modularisation 50

GLOBAL DATA
Global Data/ Local Memory
L<gr>TOP .
FUNCTION- POOL <gr>
DATA :
:

SUB ROUTINE
PROGRAM
L<gr>F01 .
FUNCTION . . .
FORM SUB1 USING . . DATA : . . . .
DATA : . . . . MOVE x TO ….
MOVE . . . TO X. PERFORM SUB1
: ENDFUNCTION .
ENDFORM .
ABAP Training Program Modularisation 51

Program Organization
L<gr>TOP .
FUNCTION- POOL <gr>
MESSAGE-ID zz .
DATA : “ Global Data
:
SAPL<gr>

*SYSTEM DEFINED INCLUDE PROGRAMS .


INCLUDE L<gr>TOP .
*USER N DEFINED INCLUDE PROGRAMS
INCLUDE L<gr>UXX . L<gr>UXX .

INCLUDE L<gr>U01 .
INCLUDE L<gr>U02 .

L<gr>U01 .
FUNCTION FA .

L<gr>U01 .
FUNCTION FB .
ABAP Training Program Modularisation 52

Interfaces In Function Modules

 Export parameters
 Import parameters
 Tables
 Changing
 Exceptions
ABAP Training Program Modularisation 53

Remote Function Calls

 Remote function calls offer


 An easy way to implement communication between application

programs via ABAP function calls


 Do not have to worry about incompatibilities between

 Hardware platforms

 Operating systems

 Relieves the programmer from technical considerations of

communication protocols
ABAP Training Program Modularisation 54

Remote Function Calls

 Local function call


 Called function resides on the same machine as the calling

program
 Remote function call
 Called function resides on a different machine to the calling

program
 Machines must be connected via a network

 Useful for instance if there are many branches/offices

where some form of centralised reporting is required


ABAP Training Program Modularisation 55

Remote Function Calls

 Syntax similar to local function call except for the addition of an


extra parameter
 Destination ‘<destination machine name>’
 Inclusion of the destination parameter alerts the system to the
fact that the called function does not run on a system whose
name is destination machine name
ABAP Training Program Modularisation 56

Remote Function Call - Example

Parameters: date1 type d, date2 type d.


Data: l_agency(20),
L_bookings like bookings occurs 0 with header line.
Call function ‘GET_LOCAL_BOOKINGS’
Destination ‘FLORIDA’
Exporting from_date = date1
To_date = date2
Importing agency = l_agency
Tables local_bookings = l_bookings.
ABAP Training Program Modularisation 57

Valid Destinations for Remote Function Calls


ABAP Training Program Modularisation 59

Maintaining RFC Destinations

 Tools…administration, administration…network, RFC destinations


 Transaction SM59
 Add or modify existing destinations
 To add a new destination, specify
 RFC destination name

 Connection type (I, 2, 3, T, L, S, X, M)

 Target machine’s IP address or DNS entry

 Description of connection
ABAP Training Program Modularisation 61

Remotely Callable Functions

 Function must be designated as supporting remote function calls in


the partner system
 ABAP automatically generates a piece of code that serves as the

entry routine (called a stub) for the incoming call


 Stub receives the incoming data and routes it to the desired

function
 Function is carried out and results sent back to the user
ABAP Training Program Modularisation 62

When an RFC Is Issued...

 Runtime system
 Converts data to machine independent representation

 Sends the data over the comms line

 Partner system
 Resolves inconsistencies in terms of

 Internal data representation

 Different code pages

 Begins new session

 Session stays alive as long as the calling program lives

 Invokes called function

 Parameters passed by value only

 Sends data back to the calling system


ABAP Training Program Modularisation 63

Handling Communication Errors

 Exception conditions
 Communication_failure

 Error on the comms line during call execution

 System_failure

 Any runtime error on the partner system

 Function does not exist


 Function not declared as remotely callable
 Can add message <fld name> to receive error message text
associated with the error
ABAP Training Program Modularisation 64

Handling Communication Errors: Example


Parameters: date1 type d, date2 type d.
Data: l_agency(20), sysmsg(80), commsg(80),
L_bookings like bookings occurs 0 with header line.
Call function ‘GETLOCALBOOKINGS’
Destination ‘FLORIDA’
Exporting from_date = date1
To_date = date2
Importing agency = l_agency
Tables local_bookings = l_bookings
Exceptions system_failure = 1 message sysmsg
Communication_failure = 2 message commsg.
Case sy-subrc.
When 1. Write: / ‘system error’, sysmsg. Exit.
When 2. Write: / ‘comms error’, commsg. Exit.
endcase.
ABAP Training Program Modularisation 65

Conclusion

 Covered topics associated with


 Subroutines in ABAP

 Form … ENDFORM, perform

 Parameter passing

 Call function

 Remote function calls (RFC’s)

Potrebbero piacerti anche