🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# ABAP 中的模块化:宏,子例程&功能模块 > 原文: [https://www.guru99.com/sap-macro-include-function-module-group-subroutine.html](https://www.guru99.com/sap-macro-include-function-module-group-subroutine.html) 在对源代码进行模块化时,需要在模块中放置一系列 ABAP 语句。 然后,您无需调用所有语句即可将其放置在主程序中,而只需调用模块即可。在生成程序时,将模块化单元中的源代码视为实际上实际存在于主程序中。 在本教程中,您将学习: * [SAP- ABAP 宏](#1) * [包含程序](#2) * [子例程](#3) * [功能模块](#4) * [功能组](#5) **Need of Modularization** * 改进程序的结构。 * 易于阅读的代码 * 易于维护的代码 * 避免冗余并促进代码重用 **Various Modularization Techniques** * 宏的使用 * 包含文件的使用 * 子程序 * 功能模块 Lets look into each of them in detail : ## SAP- ABAP 宏 If you want to reuse the same set of statements more than once in a program, you can include them in a macro.You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition. 宏对于长时间计算或复杂的 WRITE 语句很有用。 句法 ``` DEFINE <macro_name> 'Macro Statements END-OF-DEFINITION ``` Macros can use Parameters ***&N*** where 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. ``` Output: 2 ## 包含程序 Include Programs are solely for modularizing source code, and have no parameter interface. Include programs allow you to use the same source code in different programs. They  can be useful if you have lengthy data declarations that you want to use in different programs. Syntax ``` Include <include program Name> ``` **Points to Note** * 包含程序无法自行调用。 * 包含程序必须包含完整的语句。 Example: ``` INCLUDE ZILX0004. WRITE: / 'User', SY-UNAME,/ 'Date', SY-DATUM. ================================ PROGRAM ZRPM0001. INCLUDE ZILX0004. ``` ## 子例程 Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module. 句法- ``` FORM <Subroutine> [<pass>]. <Statement block>. ENDFORM. ``` <Subroutine> = Name of the subroutine <通过> =正在传递参数 **子例程的类型** 1. 内部 * 在同一程序中定义的子例程被调用。 * 可以访问在 ABAP / 4 主程序中声明的所有数据对象。 2. 外部 * 在正在调用的程序之外定义的子例程。 * 需要使用<传递>选项或在内存的公共部分声明数据对象。 **Calling a Subroutine** ***Internal Subroutines*** ``` PERFORM <subroutine> [<pass>] ``` <subroutine> = Name of the subroutine <pass> = Parameters being passed 主程序中声明的数据将自动可用。 ***外部子程序*** ``` 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>]. ``` ***Points to Note*** * 子程序中允许嵌套调用(即 FORM ... ENDFORM 中的 PERFORM)。 * 递归调用也是可能的。 * 要定义本地数据,请在 FORM 之后使用 DATA 语句。 每次您进入子例程时,都会重新创建数据(使用初始值)并在最后(从堆栈中)释放数据。 * 要定义子例程中使用的全局数据,请在 FORM 之后使用 LOCAL 语句。 当您输入子例程时,将保存这些值,然后最后(从堆栈中)释放这些值 ## 功能模块 Function Modules are general purpose ABAP/4 routines that anyone can use. Infact , there are a large number of standard function Modules available.Function Modules are organized into Function Groups: Collections of logically related functions. A Function module always belongs to a Function Group. 句法- ``` FUNCTION <function module> <Statements> ENDFUNCTION. ``` **Important information Associated with Function Module** * 行政 * 导入/更改/导出参数。 * 表参数/异常。 * 文献资料 * 源代码- *L < fgrp > U01。* < fgrp >是功能组 * 全局数据- *L < fgrp > TOP* 。功能组的全局数据-可在功能组中的各个功能模块之间访问。 * 主程序- *SAPL < fgrp >* 。 包含该功能组的所有包含文件的列表 **Call a Function Module** 要调用功能模块,请使用 CALL FUNCTION 语句: ``` 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]]. ``` ## 功能组 Function groups are containers for function modules. Infact, there are a large number of standard Function Groups. All of the function modules in a function group can access the global data of the group. 像可执行程序(类型 1)和模块池(类型 M)一样,功能组可以包含屏幕,选择屏幕和列表。 **注意点** * 功能组无法执行。 * 功能组的名称最多可以包含 26 个字符。 * 创建功能组或功能模块时,将自动生成主程序和包含程序。 * 功能组封装数据。 **How to create a Function Group** 1. 转到事务 SE80。 2. 在下拉列表中选择程序。 3. 输入要创建的功能组的名称。 通常,用户创建的功能组以“ Z”开头。 例如 -< Z_FUNCTION_GROUP_NAME >。 按下 Enter 键。 4. 请注意,如果用户选中创建 TOP 包含的选项,则默认情况下将创建 TOP 包含。 **How to create a Function Module** 1. 创建一个功能组(例如“ *ZCAL* ”)。 2. 创建功能模块,设置属性(功能组,应用程序,短文本和过程类型)和保存。 3. 包含文件“ *LZCALU01* ”将具有第一个功能模块的源代码。 4. 包含文件“ *LZCALTOP* ”将具有全局数据。 5. 主程序“ *SAPLZCAL* ”包含 * 全局数据包含文件“ *LZCALTOP* ” * 功能模块包括文件“ *LZCALUXX* ” * 用户定义的包含文件“ *LZCALF* ..”,“ *LZCALO* ..”和“ *LZCALI* ..” 6. 定义接口参数和异常 7. 编写源代码 8. 激活功能模块 9. 测试功能模块-单一测试&调试 10. 记录和发布功能模块 That's all to Modularity in ABAP.