当前位置:网站首页>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
- Inside
- A subroutine defined in the same program being called .
- You can access the main ABAP/4 All data objects declared in the program .
- 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
- Go to the transaction SE80.
- Select program... From the drop-down menu .
- 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 .
- Please note that , If the user selects create TOP The options included , By default TOP contain .
How to create function modules
- Create a function group ( such as “ ZCAL ”).
- Create a function module , Set properties ( Functional group 、 Applications 、 Short text and process type ) And save .
- Include files “ LZCALU01 ” Will contain the source code of the first function module .
- Include files “ LZCALTOP ” There will be global data .
- The main program “ SAPLZCAL ” contain
- Global data include file “ LZCALTOP ”
- Function modules include files “ LZCALUXX ”
- User defined include file “ LZCALF …”、“ LZCALO …” and “ LZCALI …”
- Define interface parameters and exceptions
- Write source code
- Activate the function module
- Test function module —— Single test and commissioning
- 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
边栏推荐
猜你喜欢
随机推荐
买网红雪糕的我,成了大冤种
静态断言 static_assert
SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
Luogu p2466 [sdoi2008] Sue's small ball solution
SAP web service 无法使用 SOAMANAGER 登陆到SOA管理页面
直播无顶流:董宇辉这么火,还有人看刘畊宏吗?
School enterprise alliance is on the way! Huawei cloud gaussdb has come to universities again
[Shanda conference] establishment of webrtc tools for multi person video call
信创研究:国产数据库聚焦信创市场,华为Gauss有望成为最强
【山大会议】应用设置模块
84.(cesium篇)cesium模型在地形上运动
Trust level of discover
【小程序项目开发-- 京东商城】uni-app开发之分包配置
3.抽象类(shape)
19、 Xv6 context switching (implementation of context switching; encapsulation and recovery of state machine)
【山大会议】多人视频通话 WebRTC 工具类搭建
Simulation of stack and queue
【华为云至简致远】征文获奖名单出炉!
预约打新债到底安不安全呀?是不是靠谱的?








