1 / 12

Subroutines and Functions

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

sharne
Télécharger la présentation

Subroutines and Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Subroutines and Functions MINS298c Fall 1998 Chapters 10 Subroutines

  2. Overview • Subroutines • What they are • Why to include in program • Forms • Implementing and using • Functions 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 Subroutines

  4. 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

  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 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 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 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 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 ?? 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. WRITE x, y, z. FORM add_em a1 a2 CHANGING a3. a1 = a1 + 1. a2 = a2 + 2. a3 = a1 + a2. ENDFORM.. 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 216-230 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 Subroutines

More Related