130 likes | 351 Views
Subroutines and Functions. MINS298c Fall 1998 Chapters 10. Overview. Subroutines What they are Why to include in program Forms Implementing and using Functions. Scope of the Program. Global: Applies to all programs or parts of programs running at the time
E N D
Subroutines and Functions MINS298c Fall 1998 Chapters 10 Subroutines
Overview • Subroutines • What they are • Why to include in program • Forms • Implementing and using • Functions Subroutines
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 Subroutines
Subroutines (Normal Scope) Line Program DATA A, B, C According to normal rules of “scope,” which Write statements are legal and which are not? 1 2 3 4 5 6 Write A Write F Form DATA X, Y, Z Write A Write F Form DATA D, E, F Write A Write F Subroutines
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 Subroutines
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 Subroutines
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 Subroutines
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 Subroutines
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 ?? Subroutines
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. WRITE x, y, z. FORM add_em a1 a2 CHANGING a3. a1 = a1 + 1. a2 = a2 + 2. a3 = a1 + a2. ENDFORM.. Subroutines
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 216-230 Subroutines
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 Subroutines