Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subroutines and Functions

Similar presentations


Presentation on theme: "Subroutines and Functions"— Presentation transcript:

1 Subroutines and Functions
MINS298c Fall 1998 Chapters 10 11/29/2018 Subroutines

2 Overview Subroutines Forms Functions What they are
Why to include in program Forms Implementing and using Functions 11/29/2018 Subroutines

3 Scope of the Program Global: Applies to all programs or parts of programs running at the time Not advised if can be avoided Prone to errors Difficult to track Local: Applies to only the subprogram running at the time Increases modularity of code (good thing) Easy to debug and track 11/29/2018 Subroutines

4 Subroutines (Normal Scope)
Line Program DATA A, B, C 1 2 3 4 5 6 According to normal rules of “scope,” which Write statements are legal and which are not? Write A Write F Form DATA X, Y, Z Write A Write F Form DATA D, E, F Write A Write F 11/29/2018 Subroutines

5 Subroutine Self contained blocks of code that can be reused by different parts of a program Advantages: Self contained code = Easy to maintain and debug Changes in subroutine do not affect rest of program Reduces complexity -- more modular Consistent with code quality: Highly Cohesive Loosely Coupled 11/29/2018 Subroutines

6 Mapping ABAP Concepts to Subroutines
Subroutine = Form Explicit to program by being a block of code within the program Implicit to program by using INCLUDE Both are called using PERFORM 11/29/2018 Subroutines

7 Subroutines Defined by FORM and ENDFORM Executed with PERFORM verb
Can call by value or call by reference by reference is implied VALUE clause passes by value ABAP does number and type checking at runtime 11/29/2018 Subroutines

8 PERFORM Syntax PERFORM subroutine_name
[USING p1 p2 p3….] [CHANGING p1 p2 p3 …] [TABLES itab1 itab2 itab3 …..] USING passes p1, p2, … pn single variables by reference USING VALUE p1 p2 …p3 passes values to subroutine but return values do not replace original p1, p2, pn Example of use: depreciation or tax calculation changes original value of asset during calculation 11/29/2018 Subroutines

9 PERFORM Syntax cont. CHANGING nearly the same as USING
Values are changed in the subroutine Passed back to the calling program, but changes are not passed back until the subroutine ends TABLES passes a whole table(s) by reference Uses internal table(s) Can not pass by Value only Reference ?? 11/29/2018 Subroutines

10 Example ASSUME: x, y, and z are integer and x = 4; y = 5; z =10
WRITE x, y, z. PERFORM add_em CHANGING VALUE(x) y CHANGING z. FORM add_em a1 a2 CHANGING a3. a1 = a1 + 1. a2 = a2 + 2. a3 = a1 + a2. ENDFORM.. 11/29/2018 Subroutines

11 Functions Functions are global -- available to all programs from the library: Created outside the program Created with FUNCTION and ENDFUNCTION statement Called from programs with CALL FUNCTION ABAP/4 Development Workbench > Function Group Objects > Single Object Name the function and click on Create Parameters of a function = Import, Export, Change See pages 11/29/2018 Subroutines

12 Programming Assignment 2
Write a program to create a list of the top five companies with the highest sales Use SAP Table TABNA and pick up country, name of company, address, sales, and currency Call the function CONVERT_TO_LOCAL_CURRENCY properly Transfer conversion date, foreign currency amount, foreign currency key, and target currency key Use a subroutine to rank the companies by sales where we can get the top N companies 11/29/2018 Subroutines


Download ppt "Subroutines and Functions"

Similar presentations


Ads by Google