Professional Documents
Culture Documents
Objectives
This lecture aims to introduce the reader to
the ABAP WRITE Statement methods for formatting output
positioning output on the screen formatting options lines and blank lines on the screen
' ----+----1----+----2----+', ' 1234567890123456789012345', 'char: ', char, /, / 'numc: ', numc, /, 'int: ', integer, /, / 'pack: ', packed, /, 'float:', float, /, / 'time: ', time, /, 'date: ', date, /, / 'hex: ', hex.
Type c n t d x i p f
Format pad right with spaces pad left with zeros HHMMSS user defaults (DDMMYYYY) pad right with zeros pad left with zeros (suppressed) pad left with zeros (suppressed) pad left with zeros (suppressed)
Positioning Output
WRITE [AT] [/] [pos] [(len)] f .
/ denotes new line pos
horizontal position always output at that position regardless of whether there is enough space or other fields overwritten
(len)
output length if len is too short
numeric fields truncated (left), asterisk displayed all others truncated (right) with no indication
write /5(12) word. write at col(len) word. write /(3) 12345. write: /5(8) word, 25(2) word.
or a digit from 0 to 7:
WRITE var COLOR 3.
Available Colours
Digit 0 1 2 3 4 5 6 7 Colour Constant COL_BACKGROUND COL_HEADING COL_NORMAL COL_TOTAL COL_KEY COL_POSITIVE COL_NEGATIVE COL_GROUP Recommended Actual for Colour Background (default) Headings List Bodies Totals Key Columns Positive Thresholds Negative Thresholds Grouping Light Grey Blue Blue Grey Yellow Cyan Green Red Orange
FORMAT
FORMAT option1 [ON|OFF] option2 [ON| OFF] ...
formatting ON applies to all subsequent output until turned off using the OFF option FORMAT option1 = var1 option2 = var2 ... var interpreted as a number should be of type i var = 0 has same effect as OFF option var > 0 has same effect as ON option with COLOR option acts like corresponding colour number
FORMAT
formatting options used in a WRITE statement overwrite the corresponding settings of a previously issued FORMAT statement for the current output for each new event the system resets all formatting options to their default values
all options have a default value of OFF except the INTENSIFIED option
FORMAT RESET
sets all formatting options to OFF in one go
tables sflight. data sum type i. top-of-page. FORMAT COLOR COL_HEADING. write: 'DATE', 20 'SEATS OCCUPIED', 50 'SEATS AVAILABLE'. start-of-selection. select * from sflight where carrid = 'AA'. if sflight-seatsocc = sflight-seatsmax. "if fully booked FORMAT COLOR COL_NEGATIVE. else. FORMAT COLOR COL_NORMAL. endif. write: / sflight-fldate UNDER 'DATE', sflight-seatsocc UNDER 'SEATS OCCUPIED', sflight-seatsmax UNDER 'SEATS AVAILABLE'. sum = sum + sflight-seatsocc. endselect. end-of-selection. uline. write: 'Total Bookings:' INTENSIFIED OFF, sum UNDER sflight-seatsocc COLOR COL_TOTAL.
Vertical Lines
SKIP [n]
if n greater than lines remaining on page
produces page footer, throws to new page
data blank_line(50). write: / blank_line. write sy-uline. set blank lines on. write: / blank_line. write sy-uline. skip 2. write sy-uline. skip to line 10. write 'line 10'. skip to line 20. write 'line 20'. skip to line 15. write 'line 15'.
NEW-LINE.
SY-SROWS SY-SCOLS
Current number of lines in display window Current number of columns in display window
Program Modularisation
Why Modularise?
improve program structure make the program easier to maintain and update improve readability reduce redundancy allow for component reuse event processing
ABAP is an event driven language. The processing associated with each event is written in a program module
Modularisation in ABAP
types of modules in an ABAP program
subroutines or forms
internal subroutines source code is in the same ABAP program as the calling procedure external subroutines source code of external subroutines is in an ABAP program other than the calling procedure
functions
stored in central library offer a defined interface
Subroutine Syntax
FORM subr [TABLES formal table list] [USING formal input list] [CHANGING formal output list] ENDFORM. PERFORM subr [TABLES actual table list] [USING actual input list] [CHANGING actual output list]
formal parameters - defined within FORM actual parameters - specified with PERFORM parameter types
input - used to pass data to subroutines output - used to pass data from subroutines input/output - pass data to & from subroutines
call by reference call by value normal scoping rules apply to calling & called data items
passing by reference
USING or CHANGING
USING for input parameters that are not changed CHANGING for output parameters that are changed
passing by value
FORM subr USING VALUE(f )
report zpass_by_ref. parameters: number type i default 10. data: factorial type i value 0. PERFORM fact USING number CHANGING factorial. write: / 'factorial of ', (5) number, 'is ', (8) factorial. FORM fact USING p_number CHANGING p_factorial. p_factorial = 1. while p_number ge 1. p_factorial = p_factorial * p_number. p_number = p_number - 1. endwhile. endform. " fact
report zpass_by_ref. parameters: number type i default 10. data: factorial type i value 0. PERFORM fact USING number CHANGING factorial. write: / 'factorial of ', (5) number, 'is ', (8) factorial. FORM fact USING value(p_number) CHANGING p_factorial. p_factorial = 1. while p_number ge 1. p_factorial = p_factorial * p_number. p_number = p_number - 1. endwhile. endform. " fact
Terminating Subroutines
Unconditional termination
EXIT in the subroutine
control leaves the subroutine and processing continues immediately after the calling PERFORM statement
Conditional termination
CHECK cond in the subroutine
if cond is not satisfied, same as above
Function Modules
classified in function groups
group of functions that serve similar purposes
eg, calendar functions
stored in the function library main difference between subroutines and functions is a clearly defined interface for passing data between program and function
Function Builder
FM Group: FIBU FM_01 ... FM_02 ... FM Group: ZIBU FM_03 FM_04 ...
Building a Function
44
45
46
48
49
Calling Functions
CALL FUNCTION function_name [EXPORTING f1=a1 fn=an] [IMPORTING [CHANGING [TABLES f1=a1 fn=an] f1=a1 fn=an] f1=a1 fn=an]
Calling Functions
EXPORTING f1=a1 fn=an
enables the passing of actual parameters to formal input parameters of the function
Calling Functions
CHANGING f1=a1 fn=an
enables the passing of actual parameters to the formal parameters and, after processing the system passes the (changed) formal parameters back to the actual parameters
p3 = p1 / p2.
2 /
3 =
0.67
Calling Functions
EXCEPTIONS f1=a1 fn=an
functions are defined with exceptions the calling program determines whether and which exceptions it is to handle itself the OTHERS clause covers all exceptions not explicitly specified if an error occurs in the function execution it is either handled in the function or control immediately returns to the calling program (if the exception is specified in the EXCEPTIONS parameter of the call)
Exception defined in function definition Exception condition checked on return from function
call function 'fill_seattab' EXPORTING year = TABLES seattab = EXCEPTIONS no_entry = others =
case sy-subrc. when 1. write No entry. when 2. write Other error. endcase.
Using raise
parameters: num1 type i default 2, num2 type i default 3. data result type p decimals 2. CALL FUNCTION 'Z_DIVIDE' EXPORTING p1 = num1 p2 = num2 IMPORTING p3 = result EXCEPTIONS div_by_zero = 01. if sy-subrc = 1. write 'Error: Divide by Zero'. else write: / num1, '/', num2, '=', result. endif.
EditPattern
58
inclusion of the destination parameter alerts the system to the fact that the called function does not reside locally, but on a system whose name is destination machine name
64
system_failure
any runtime error on the partner system
function does not exist function not declared as remotely callable
can add message fld_name to the EXCEPTION clause to receive error message text associated with the error
parameters: date1 type d, date2 type d. data: local_agency(20), sys_msg(80), comms_msg(80), local_bookings like sbook occurs 0 with header line.
CALL FUNCTION 'GET_LOCAL_BOOKINGS' DESTINATION 'PARIS' EXPORTING from_date = date1 to_date = date2 IMPORTING agency = local_agency TABLES bookings = local_bookings EXCEPTIONS system_failure = 1 message sys_msg communication_failure = 2 message comms_msg. case sy-subrc. when 1. write: / System Error:, sys_msg. when 2. write: / Comms Error:, comms_msg. endcase.
Conclusion
This lecture covered topics associated with
formatting output using the WRITE statement
colours position and length lines and blank lines
subroutines in ABAP
FORM ENDFORM, PERFORM Parameter passing CALL FUNCTION Remote Function Calls (RFCs)
Associated Reading
Textbook Ch 3.4.4 Output Formatting Ch 3.2.12 Subroutines Ch 3.9 Function Modules On Line Help
BC ABAP Users Guide
Pre-Reading
Textbook
Ch 3.4.2 (p 211): The Events Concept Ch 3.4.3 (p 213): Selections and Parameters
Selection Screens Using Variants