Lecture11 abap on line

16
Lecture 11 Subroutines & Function Modules BCO5647 Applications Programming Techniques (ABAP)

description

 

Transcript of Lecture11 abap on line

Page 1: Lecture11 abap on line

Lecture 11Subroutines & Function Modules

BCO5647 Applications Programming Techniques (ABAP)

Page 2: Lecture11 abap on line

2BCO5647

Readings & Objectives

Readings

Keller & Keller Chapter 5Section 5.1 & 5.2

Objectives

This lecture will

Discuss modularisation techniques and how these could be implemented in ABAP

Introduce the concept of internal subroutines and how these are implemented in ABAP

Examine how parameters are passed between the main program and subroutines

Distinguish calling parameters by value and by reference

Introduce the concept of function modules and their characteristics

Demonstrate how function modules can be searched for in the repository

Demonstrate how the features of a function module can be displayed

Demonstrate how a function module can be called

Demonstrate how errors in function modules can be captured and handled.

Page 3: Lecture11 abap on line

3BCO5647

Modularisation Techniques

Modularisation enables us to reduce the complexity of large programs and to re-use code.

Modularisation can be implemented using:Internal subroutines

The subroutine and the call are in the same program.External subroutines

The subroutine is in an external program.Function modules

The subroutine is stored in the Function Library.

Page 4: Lecture11 abap on line

4BCO5647

Internal Subroutines (“Forms”)

Internal subroutines are declared using the FORM and ENDFORM keywords.

All subroutines should be placed at the end of the program.

Subroutines are called using the PERFORM statement.

Nested and recursive subroutine calls are permitted.

Example :

perform calculate_total.……

form calculate_total.….statements

endform.

Page 5: Lecture11 abap on line

5BCO5647

Global and Local Data

Unless declared separately in an internal subroutine, data is global to the whole program.

Data can be defined as being local to the subroutine by using the data keyword :

data flag value 1 type i.write flag.perform write_flag.write flag. ……..form write_flag.

data flag type i.flag = 2.write flag.

endform.

Page 6: Lecture11 abap on line

6BCO5647

Passing Parameters

Data can be passed from the calling program to a subroutine using parameters.

Parameters may be described as: Read only - the parameter is read in the subroutine but is not changed. Changing - the parameter is read and changed by the subroutine.

In general, the parameter passed must be of the same type as the parameter received by the subroutine.

Data structures (eg internal tables) can also be passed as parameters.

Page 7: Lecture11 abap on line

7BCO5647

Passing Parameters - Example

report ptest.data: val1 type p, val2 type p, val3 type p, val4 type p, answer type p, operator type c.val1 = 4. val2 = 2. operator = ‘+’.val3 = 8. val4 = 5.

perform calc_answer using val1 val2 operator changing answer.

operator = ‘-’.perform calc_answer using val3 val4 operator

changing answer.………...form calc_answer using fv1 fv2 op changing ans. case op. when ‘+’. ans = fv1 + fv2. when ‘-’. ans = fv1 - fv2. endcase.endform.

Page 8: Lecture11 abap on line

8BCO5647

Calling by Value and by Reference

Parameters can be called either by reference or by value.

Calling by reference: The address of the actual parameter is called. Changes have an immediate effect. If only the formal parameter name is specified in the subroutine interface, then the parameter is called by reference.

Calling by value: When the subroutine is called, the actual parameter value is copied to the formal parameter. There are two types of call by value.

Page 9: Lecture11 abap on line

9BCO5647

External Subroutines

A program can make an external call to a subroutine in another program.

Example :

report main.data: name(20).

….perform print_name(process) using name.

….

report process.….

form print_name using name1.….

endform.

Page 10: Lecture11 abap on line

10BCO5647

Function Modules

A function module is a separate program that can be called from your ABAP code to perform a specific task.

SAP comes with a library of pre-written function modules. Function modules are pooled together into function groups.

E.g. calendar functions

Function modules have a clearly defined fixed interface for data exchange.

The interface supports exception handling.

Function modules use their own memory area.

Page 11: Lecture11 abap on line

11BCO5647

Searching for a Function Module

Go to the Function Builder: SAP Standard Menu >> Development >> Function Builder. Transaction Code SE37.

Searching can be done using the Repository Information System or SAP’s Application hierarchy.

Click Matchcode Button

Page 12: Lecture11 abap on line

12BCO5647

Displaying a Function Module

You can display information about existing function modules:

Attributes: specifies administrative information like the person responsible for the module and a short description of the module.

Import: contains a list of the formal parameters that are used to pass data to a function module.

Export: contains a list of the formal parameters that are used to receive data from a function module.

Page 13: Lecture11 abap on line

13BCO5647

Displaying a Function Module

You can display information about existing function modules:

Changing: contains a list of the formal parameters that are used both to pass data to and receive data from a function module.

Tables: specifies the tables that are to be passed to a function module. Table parameters are always passed by reference.

Exceptions: shows how the function module reacts to exceptions.

Source code: program code of the function module.

Documentation: provides information about the interface and exceptions.

Page 14: Lecture11 abap on line

14BCO5647

Calling a Function Module

Function modules are called by the ABAP code using a CALL statement.

To insert function into ABAP code click [Pattern] button on toolbar.

Page 15: Lecture11 abap on line

15BCO5647

Calling a Function Module

Page 16: Lecture11 abap on line

16BCO5647

Calling a Function Module