Lab 8 (Function Module) Ans

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

Hong Kong Institute of Vocational Education

IT114105 HD in Software Engineering


ITP4512 Enterprise Software
Topic 7: Function Module
Lab 8 – Function Module

Intended Learning Outcomes


Upon completion of this tutorial/lab, you should be able to:
o Identify how to handle function modules in an SAP R/3 system.
o Create simple ABAP programs to use function modules for specific functions.

Exercise 1

1. Use SE80/SE37 to create a function group called Z_08_FG.


2. Save it as local object:

Exercise 2

1. Use SE37 to create a function module called Z_FM_DIVIDE to import 2 numbers and export the
result which number 1 divided by number 2. Export result as zero if number 2 is equal to zero.
2. Copy the program Z_07_SB_DIVIDE to Z_08_FM_DIVIDE which calls Z_FM_DIVIDE to
handle the calculation instead of using subroutine:

1
Answer:

2
*&---------------------------------------------------------------------*
*& Report Z_08_SB_DIVIDE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT Z_08_FM_DIVIDE MESSAGE-ID Z_MESSAGE .

DATA f_result TYPE p DECIMALS 2.


DATA result TYPE f.
PARAMETERS Number1 TYPE i.
PARAMETERS Number2 TYPE i.

CALL FUNCTION 'Z_FM_DIVIDE'


EXPORTING
NUM1 = number1
NUM2 = number2
IMPORTING
RES = result
.

IF sy-subrc EQ 0.
MOVE result TO f_result.
WRITE: / 'Result:', f_result.
ENDIF.

Exercise 3

1. Copy the program Z_08_FM_DIVIDE to Z_08_DIVIDE_CHECK. Copy the function module


Z_FM_DIVIDE to Z_DIVIDE_CHECK. Change the logic in Z_DIVIDE_CHECK such that it will
check whether number 2 is zero and send this fact out using Exceptions.

3
2. Revise the program Z_08_DIVIDE_CHECK so that it can show error message (use Z_MESSAGE
created in Lab 7) while user inputs 0 as the value of number 2.

Answer:

4
*&---------------------------------------------------------------------*
*& Report Z_07_SB_DIVIDE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT Z_08_ DIVIDE_CHECK MESSAGE-ID Z_MESSAGE .

DATA f_result TYPE p DECIMALS 2.


DATA result TYPE f.
PARAMETERS Number1 TYPE i.
PARAMETERS Number2 TYPE i.

CALL FUNCTION 'Z_DIVIDE_CHECK'


EXPORTING
NUM1 = number1
NUM2 = number2
IMPORTING
RES = result
EXCEPTIONS
ZERO_NUMBER = 1
OTHERS = 2.

IF SY-SUBRC <> 0.
IF sy-subrc = 1.
MESSAGE i003.
ELSE.
MOVE result TO f_result.
WRITE: / 'Result:', f_result.
ENDIF.

5
Exercise 4

1. Use SE37 to create a function module called Z_FM_GS to import two number a , r and n, then it
exports s which is equal to sum of the geometric series : a, ar, ar2, …. , arn-1.
2. If r = 1, exception is raised by the function.
3. Sum of GS is given as: a * ( 1 – rn ) / ( 1 – r ).
4. Write a program named Z_08_GS which calls Z_FM_GS to handle the calculation and will
display an error message when input ratio is equal to 1.
5.

Answer:
Function Z_FM_GS:
Import: A TYPE I, R TYPE I, N TYPE I.
Export: S TYPE I.
Exceptions: ONE …. one.
Source Code:
FUNCTION z_08_gp.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(A) TYPE I
*" REFERENCE(R) TYPE I
*" REFERENCE(N) TYPE I
*" EXPORTING
*" REFERENCE(S) TYPE I
*" EXCEPTIONS
*" ONE
*"----------------------------------------------------------------------
IF r NE 1.
s = a * ( 1 - r ** n ) / ( 1 - r ).
ELSE.
RAISE one.
ENDIF.
ENDFUNCTION.

6
Program Z_08_GS Source Code:

REPORT z_08_gs.
PARAMETERS: n_a TYPE i,
n_r TYPE i,
n_n TYPE i.
DATA sum TYPE i.

CALL FUNCTION 'Z_08_GP'


EXPORTING
a = n_a
r = n_r
n = n_n
IMPORTING
s = sum
EXCEPTIONS
one = 1
OTHERS = 2.

IF sy-subrc = 0.
WRITE: / 'Sum of GP is ', sum.
ELSEIF sy-subrc = 1.
WRITE: / 'Ratio cannot be 1'.
ENDIF.

Exercise 5

1. Create a program called Z_09_FLIGHT.


2. In the selection screen of the program, show 3 inputs as follows:

3. Create a variant called VARI_01 for the program Z_09_FLIGHT with below selection screen:

7
4. Create a function module called Z_SUM_PASSENGER, which will sum up 3 input numbers and
then classify the sum as FULL, GOOD or LOW.
( LOW: <=100; FULL: >=300; otherwise ‘GOOD’)
5. Search table SFLIGHT with the selection criteria and call the function module
Z_SUM_PASSENGER to identify the booking status of each flight. Output is similar to below:

6. The total number of passengers of each flight can be calculated by:


SEATSOCC + SEATSOCC _B + SEATSOCC _F
Some sample code for your reference:

REPORT z_09_flight .
TABLES sflight.
TYPES: BEGIN OF i_rec,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
planetype LIKE sflight-planetype,
seatsocc LIKE sflight-seatsocc,
seatsocc_b LIKE sflight-seatsocc_b,
seatsocc_f LIKE sflight-seatsocc_f,
END OF i_rec.
DATA: tab TYPE TABLE OF i_rec,
wa TYPE i_rec.
DATA: total_pass TYPE i,
book_s TYPE c LENGTH 10.

WRITE: 'Airline', 'Connection No', 'Date ', 'Plane Type',


'Booking Status'.

*** Complete the program ***

LOOP AT tab INTO wa.

*** Call Z_SUM_PASSENGER ***

8
WRITE: / wa-carrid UNDER 'Airline',
wa-connid UNDER 'Connection No',
wa-fldate UNDER 'Date ',
wa-planetype UNDER 'Plane Type'.
book_s RIGHT-JUSTIFIED UNDER 'Booking Status'.
ENDLOOP.

Answer:

9
REPORT z_09_flight .
TABLES sflight.
TYPES: BEGIN OF i_rec,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
planetype LIKE sflight-planetype,
seatsocc LIKE sflight-seatsocc,
seatsocc_b LIKE sflight-seatsocc_b,
seatsocc_f LIKE sflight-seatsocc_f,
END OF i_rec.
DATA: tab TYPE TABLE OF i_rec,
wa TYPE i_rec.
DATA: total_pass TYPE i,
book_s TYPE c LENGTH 10.

SELECT-OPTIONS: s_carrid FOR sflight-carrid.


SELECT-OPTIONS: s_connid FOR sflight-connid.
SELECT-OPTIONS: s_date FOR sflight-fldate.
WRITE: 'Airline', 'Connection No', 'Date ', 'Plane Type',
'Booking Status'.

SELECT carrid connid fldate planetype seatsocc seatsocc_b seatsocc_f


FROM sflight
INTO TABLE tab
WHERE carrid IN s_carrid
AND connid IN s_connid
AND fldate IN s_date.
LOOP AT tab INTO wa.

10
CALL FUNCTION 'Z_SUM_PASSENGER'
EXPORTING
num1 = wa-seatsocc
num2 = wa-seatsocc_b
num3 = wa-seatsocc_f
IMPORTING
total = total_pass
booking = book_s.

WRITE: / wa-carrid UNDER 'Airline',


wa-connid UNDER 'Connection No',
wa-fldate UNDER 'Date ',
wa-planetype UNDER 'Plane Type',
book_s RIGHT-JUSTIFIED UNDER 'Booking Status'.
ENDLOOP.

END.

11

You might also like