Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 9: Modular Programming Todd A. Boyle, Ph.D. St. Francis Xavier University.

Similar presentations


Presentation on theme: "Lesson 9: Modular Programming Todd A. Boyle, Ph.D. St. Francis Xavier University."— Presentation transcript:

1 Lesson 9: Modular Programming Todd A. Boyle, Ph.D. St. Francis Xavier University

2 Modular Programming So far we have examined code focused on producing professional looking reports, such as creating custom input screens and formatting output data. We will now examine code and issues focused on improving the professionalism of the code you write. These are issues that the end user may not see, but instead fall under good coding practices.

3 Modular Programming Modular programming focuses on dividing our program into logical pieces or units. These logical pieces allow for reusability of code and eases in code maintainability. There are three major types of modular units in ABAP: – Subroutines – Function Modules – Events

4 Subroutines A program-internal modularization unit. Parts of a program are moved into the subroutine to make the program easier to read and segments reusable. Allows for a more function-oriented program. Each subroutine executes a specific function.

5 Subroutines * Call the subroutine PERFORM. … * Subroutine code FORM. … ENDFORM.

6 Subroutines Subroutines may contain an interface. We can pass data to the subroutine and get data back from the subroutine using the interface. This allows us to call the subroutine multiple times and pass different data objects. We declare the interface after the subroutine name.

7 Subroutines * Call the subroutine PERFORM. … * Subroutine code FORM. … ENDFORM.

8 Subroutines We pass parameters from the main program to the subroutine and from the subroutine back to the main program. In the interface, we define how data is passed from the main program (called the actual parameters) to the subroutine (called the formal parameters).

9 Defining the Interface There are three ways in which we pass data from the main program to the subroutine: – Call-by-value – Call-by-reference – Call-by-value-and-result

10 Call-by-value A local copy of the actual parameter is passed to the subroutine. The value assigned to the formal parameter has no effect on the actual parameters.

11 Call-by-reference The dereferenced address of the actual parameter is passed to the subroutine. The value assigned to the formal parameter affects the actual parameter directly. The value of the actual parameter is physically changed by the subroutine through the address.

12 Call-by-value-and-result A local copy of the actual parameter is passed to the subroutine and a value is passed back to the main program. The actual parameter changes only after leaving the subroutine. Prevents updates if the subroutine terminates unexpectedly.

13 ABAP Code: Call-by-value PERFORM tax_calc USING a1. FORM tax_calc USING VALUE(f1). … ENDFORM.

14 ABAP Code: Call-by-reference PERFORM tax_calc CHANGING a2. FORM tax_calc CHANGING f2. … ENDFORM.

15 ABAP Code: Call-by-value-and-result PERFORM tax_calc CHANGING a3. FORM tax_calc CHANGING VALUE(f3). … ENDFORM.

16 Example Program ZWSSUBROUTINE1

17 Function Modules If there is a common activity (e.g., calculate interest) that may span multiple ABAP programs, we can code that logic in a function module. This function module can then be called by a wide variety of programs. This differs from a subroutine, which can only be called by the program it resides in.

18 Function Modules A function module contains the following interface elements: – Export parameters: passed from the calling program to the function module. – Import parameters: passed from the function module to the calling program. – Changing parameters: passed to the function module and can be modified by it. – Exceptions: information about any errors that may have occurred when executing the function module.

19 Function Modules Steps in implementing a Function Module – Create an ABAP program. – Use the PATTERN push button to find the function module. – Remove the comments from the import, export, changing, and exception parameters.

20

21

22

23

24

25

26 Example of a Function Module ZWSFUNMODULE1


Download ppt "Lesson 9: Modular Programming Todd A. Boyle, Ph.D. St. Francis Xavier University."

Similar presentations


Ads by Google