当前位置:网站首页>Modularity in SAP ABAP: macros, subroutines and function modules -04

Modularity in SAP ABAP: macros, subroutines and function modules -04

2022-06-22 16:21:00 Boating in rainy days

ABAP Modularity in : macro 、 Subroutines and functional modules -04

When you modularize the source code , You will ABAP Statements in a module . then , You simply call the module , Instead of putting all the statements in the main program . When the program is generated , The source code in the modular unit is considered to actually exist in the main program .

The need for modularity

  • Improve the structure of the program .
  • Easy to read code
  • Easy to maintain code
  • Avoid redundancy and promote code reuse

Various modular technologies

  • Use of macros
  • Use of include files
  • Subroutines
  • Function module

Let's study each of them in detail :

SAP-ABAP macro

If you want to use the same set of statements multiple times in your program , You can include them in macros .

You can only use macros in the program that defines them , And it can only be called in the program line after its definition .

Macros for long computation or complex WRITE Sentences are useful .

DEFINE <macro_name>

'Macro Statements

END-OF-DEFINITION

Macros can use arguments &N, among N = 1,2,3

DATA: number1 TYPE I VALUE 1.

DEFINE increment.

ADD 1 to &1.

WRITE &1.

END-OF-DEFINITION.

Increment number1.

WRITE number1.

Including procedures

Included programs are for modular source code only , No parameter interface . Included programs allow you to use the same source code in different programs . If you want to use verbose data declarations in different programs , They will be very useful

Include <include program Name>

Point needs attention

The containing program cannot call itself .

The containing program must contain complete statements

INCLUDE ZILX0004.

WRITE: / 'User', SY-UNAME,/ 'Date', SY-DATUM.

================================

PROGRAM ZRPM0001.

INCLUDE ZILX0004.

Subroutines

Subroutines are available in any ABAP A process defined in a program , You can also call... From any program . Subroutines usually call... Internally , in other words , They contain code snippets or algorithms that are often used locally . If you want a feature to be reusable throughout the system , Please use the function module .

FORM <Subroutine> [<pass>].

<Statement block>.

ENDFORM.
< Subroutines > = Subroutine name 
<pass> = Parameters passed 

Type of subroutine

  1. Inside
    • A subroutine defined in the same program being called .
    • You can access the main ABAP/4 All data objects declared in the program .
  2. External
    • A subroutine defined outside the called program .
    • Need to use Option or declare data objects in the public part of memory .

Call subroutine

Internal subroutine

PERFORM <subroutine> [<pass>]
< Subroutines > = Subroutine name 
<pass> = Passed parameters 

The data declared in the main program is automatically available .

External subroutine

PERFORM <subroutine>(<Program>) [<pass>].

PERFORM <subroutine> (<Program>) [<pass>] [IF FOUND].

PERFORM (<subroutine>) IN PROGRAM  (<Program>) [<pass>] [IF FOUND].

PERFORM <index> OF <subroutine1> <subroutine2> <subroutine3> [<pass>].

matters needing attention

  • Nested calls are allowed in subroutines ( That is to say FORM … ENDFORM In the implementation of ).
  • Recursive calls are also possible .
  • To define local data , Please be there. FORM Then use DATA sentence . Each time you enter a subroutine , Will recreate the data ( Use the initial value ) And release at last ( From stack ).
  • To define global data to be used in subroutines , Please be there. FORM Then use LOCAL sentence . Save these values when you enter the subroutine , Then release at the end ( From stack )

Function module

The function module is a general module that anyone can use ABAP/4 routine . in fact , A large number of standard function modules are available .

Functional modules are successfully organized into functional groups : A collection of logically related functions . A functional module always belongs to a functional group .

syntax -

FUNCTION <function module>

<Statements>

ENDFUNCTION.

Important information related to functional modules

  • Administration
  • Import / change / Export parameters .
  • Table parameters / abnormal .
  • file
  • Source code - LU01 . Is a functional group
  • Global data - LTOP . Global data of functional groups - It can be accessed across functional modules in functional groups .
  • The main program - SAPL . Contains a list of all containing files for this function group

Call function module

To call a function module , Please use CALL FUNCTION sentence :

CALL FUNCTION <module>

[EXPORTING  f1 = a 1.... f n = a n]

[IMPORTING  f1 = a 1.... f n = a n]

[CHANGING   f1 = a 1.... f n = a n]

[TABLES     f1 = a 1.... f n = a n]

[EXCEPTIONS e1 = r 1.... e n = r n [ERROR_MESSAGE = r E]    

[OTHERS = ro]].

Functional group

Functional groups are containers for functional modules . in fact , There are a large number of standard functional groups .

All functional modules in a functional group can access the global data of the group .

And executable programs ( type 1) And module pool ( type M) equally , Function groups can contain screens 、 Select screen and list .

matters needing attention

  • Unable to execute function group .
  • Function group names can be up to 26 Characters .
  • When creating a function group or function module , The main program and the containing program will be automatically generated .
  • Function groups encapsulate data .

How to create a functional group

  1. Go to the transaction SE80.
  2. Select program... From the drop-down menu .
  3. Write down the name of the feature group you want to create . Usually, the function groups made by users are represented by “Z” start . for example - <Z_FUNCTION_GROUP_NAME> . press return .
  4. Please note that , If the user selects create TOP The options included , By default TOP contain .

How to create function modules

  1. Create a function group ( such as “ ZCAL ”).
  2. Create a function module , Set properties ( Functional group 、 Applications 、 Short text and process type ) And save .
  3. Include files “ LZCALU01 ” Will contain the source code of the first function module .
  4. Include files “ LZCALTOP ” There will be global data .
  5. The main program “ SAPLZCAL ” contain
      • Global data include file “ LZCALTOP ”
      • Function modules include files “ LZCALUXX ”
      • User defined include file “ LZCALF …”、“ LZCALO …” and “ LZCALI …”
    1. Define interface parameters and exceptions
    2. Write source code
    3. Activate the function module
    4. Test function module —— Single test and commissioning
    5. Recording and publishing function modules
      This is it. ABAP Modularity in .

Reference resources :https://www.guru99.com/sap-macro-include-function-module-group-subroutine.html

原网站

版权声明
本文为[Boating in rainy days]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221500489535.html