SAP AND ABAP TIPS AND FACTS

Friday, March 21, 2008

SAP ABAP Tutorial on Subroutines. PERFORM FORM ENDFORM

SAP ABAP Tutorial on Subroutines. PERFORM FORM ENDFORM

A subroutine is a block of code introduced by FORM and concluded by ENDFORM.







FORM <subroutine
name> [USING ... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]...
]

[CHANGING... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]...
].


<Subroutine
code>


ENDFORM.




In ABAP a subroutine is defined as shown above. All the subroutines start with FORM followed by the subroutine name.

For example.







FORM write_hello.

    Write:/ 'Hello'.

ENDFORM.




A subroutine is called by a PERFORM statement as follows.

PERFORM write_hello.

In the above example no parameters pased to the subroutine. We will now see one more example of a simple subroutine that passes parameters. Now if you want to add two numbers. we need to write the following code.

Data: d_sum type i,
d_num1 type i,
d_num2 type i.


d_num1 = 5.
d_num2 = 10.

perform addnumbers using d_num1
d_num2 .


write:/ d_sum, d_num1, d_num2.

*---------------------------------------------------------------------*
* FORM addnumbers *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> NUM1 *
* --> NUM2 *
*---------------------------------------------------------------------*
form addnumbers using num1 num2.
d_sum = num1 + num2.
endform.

For the above code the output would be 15, 5, 10 because the values of the parameters would be as follows.

d_sum = 15,
d_num1 = 5,
d_num2 = 10.

This is because we are passing the parameters by REFERENCE and not by VALUE.

Important: For calling by reference, USING and CHANGING are equivalent.

This means that the value of the actual parameters changes if the value of the formal parameters changes. We will write some code
to demonstrate this point.

Data: d_sum  type i,
      d_num1 type i,
      d_num2 type i.


d_num1 = 5.
d_num2 = 10.


perform pass_ref using d_num1
                       d_num2.

write:/ d_sum, d_num1, d_num2.

*&---------------------------------------------------------------------*
*&      Form  pass_ref
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_D_NUM1  text
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_ref USING    P_D_NUM1
                       P_D_NUM2.

   P_D_NUM1 = 100.
   P_D_NUM2 = 200.

ENDFORM.                    " pass_ref


The output of the above program would be 15, 100, 200. This is because the value of actual parameters is changed because the value of the formal parameters also changes.

We will now see how to pass parameters by value. To pass parameters by value we need to explicitly state it in the subroutine. When parameters are passed by VALUE the value of the actual parameters does not change, even if the value of the formal parameters changes.

Sample code.

*&---------------------------------------------------------------------*
*&      Form  pass_val
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*     
*      -->P_D_NUM1  text
*     
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_val USING  VALUE(P_D_NUM1)
                     VALUE(P_D_NUM2).

 P_D_NUM1 = 1000.
 P_D_NUM2 = 2000.

ENDFORM.                    " pass_val

Please find the code below that demonstrates this concept.

Data: d_sum  type i,
      d_num1 type i,
      d_num2 type i.


d_num1 = 5.
d_num2 = 10.


perform pass_val using d_num1
                                     d_num2.

write:/ d_sum, d_num1, d_num2.

*&---------------------------------------------------------------------*
*&      Form  pass_val
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*     
*      -->P_D_NUM1  text
*     
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_val USING  VALUE(P_D_NUM1)
                     VALUE(P_D_NUM2).

 P_D_NUM1 = 1000.
 P_D_NUM2 = 2000.

ENDFORM.                    " pass_val

The output of the above program is 15, 5, 10.

We will now see the effect of CHANGING when the PARAMETERS are passed by VALUE. If CHANGING is specified in PARAMETERS that are passed by value then the value of the ACTUAL Parameters also changes. Please see the sample code below.


Data: d_sum  type i,
      d_num1 type i,
      d_num2 type i.


d_num1 = 5.
d_num2 = 10.

perform pass_val_chcg  using d_num1
                                               d_num2.

write:/ d_sum, d_num1, d_num2.

*&---------------------------------------------------------------------*
*&      Form  pass_val_chcg
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_D_NUM1  text
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_val_chcg USING      VALUE(P_D_NUM1)
                            CHANGING   VALUE(P_D_NUM2).

 P_D_NUM1 = 1001.
 P_D_NUM2 = 2002.

ENDFORM.                    " pass_val_chcg

The output of the above program would be as follows.

15, 5, 2,002

This is because we have specified CHANGING only for the PARAMETER P_D_NUM2 and hence the actual Parameter d_num2 also changes.

If the subroutine concludes successfully, that is, when the ENDFORM statement occurs, or when the subroutine is terminated through a CHECK or EXIT statement, the current value of the formal parameter is copied into the actual parameter.

If the subroutine terminates prematurely due to an error message, no value is passed.

ABAP TIPS

PREVIOUS                  NEXT                    RANDOM

 ABAP TIPS


Since your web browser does not support JavaScript, here is a non-JavaScript version of the image slideshow:

 ABAP TIPS
Always specify your conditions in the Where-clause instead of checking them yourself with check statements. The database system can then use an index (if possible) and the network load is considerably less.


 ABAP TIPS
For all frequently used Select statements, try to use an index. You always use an index if you specify (a generic part of) the index fields concatenated with logical Ands in the Select statement's Where clause. Note that complex Where clauses are poison for the statement optimizer in any database system.


 ABAP TIPS
If there exists at least one row of a database table or view with a certain condition, use the Select Single statement instead of a Select-Endselect-loop. Select Single requires one communication with the database system, whereas Select-Endselect needs two.


 ABAP TIPS
It is always faster to use the Into Table version of a Select statement than to use Append statements.


 ABAP TIPS
To read data from several logically connected tables use a join instead of nested Select statements. Network load is considerably less.


 ABAP TIPS
If you want to find the maximum, minimum, sum and average value or the count of a database column, use a select list with aggregate functions instead of computing the aggregates yourself. Network load is considerably less.


 ABAP TIPS
If you process your data only once, use a Select-Endselect-loop instead of collecting data in an internal table with Select Into Table. Internal table handling takes up much more space.


 ABAP TIPS
Use a select list or a view instead of Select * , if you are only interested in specific columns of the table. Network load is considerably less.


 ABAP TIPS
For all frequently used, read-only tables, try to use SAP buffering. Network load is considerably less.


 ABAP TIPS
Whenever possible, use array operations instead of single-row operations to modify your database tables. Frequent communication between the application program and database system produces considerable overhead.


 ABAP TIPS
Whenever possible, use column updates instead of single-row updates to update your database tables. Network load is considerably less.


 ABAP TIPS
Instead of using nested Select loops or FOR ALL ENTRIES it is often possible to use subqueries. Network load is considerably less.


 ABAP TIPS
Use the special operators CO, CA, CS, instead of programming the operations yourself. If ABAP/4 statements are executed per character on long strings, CPU consumption can rise substantially.


 ABAP TIPS
Some function modules for string manipulation have become obsolete and should be replaced by ABAP/4 statements or functions: STRING_CONCATENATE... -> CONCATENATE, STRING_SPLIT... -> SPLIT, STRING_LENGTH -> strlen(), STRING_CENTER -> WRITE...TO...CENTERED, STRING_MOVE_RIGHT -> WRITE...TO...RIGHT-JUSTIFIED


 ABAP TIPS
Use the CONCATENATE statement instead of programming a string concatenation of your own.


 ABAP TIPS
If you want to delete the leading spaces in a string, use the ABAP/4 statement SHIFT...LEFT DELETING LEADING... .Other constructions (with CN and SHIFT...BY SY-FDPOS PLACES, with CONDENSE if possible, with CN and ASSIGN CLA+SY-FDPOS(LEN) ...) are not as fast. In any case, avoid using SHIFT inside a WHILE-loop!


 ABAP TIPS
Use the SPLIT statement instead of programming a string split yourself.


 ABAP TIPS
Use the strlen( ) function to restrict the DO loop to the relevant part of the field, e.g. when determinating a check-sum.


 ABAP TIPS
Use "CLEAR f WITH val" whenever you want to initialize a field with a value different from the field's type-specific initial value.


 ABAP TIPS
Try to keep the table ordered and use binary search or used a table of type SORTED TABLE. If TAB has n entries, linear search runs in O( n ) time, whereas binary search takes only O( log2( n ) ).


 ABAP TIPS
A dynamic key access is slower than a static one, since the key specification must be evaluated at runtime. However, for large tables the costs are dominated by number of comparison needed to locate the entry.


 ABAP TIPS
If you need to access an internal table with different keys repeatedly, keep your own secondary indices.With a secondary index, you can replace a linear search with a binary search plus an index access.


 ABAP TIPS
LOOP ... WHERE is faster than LOOP/CHECK because LOOP ... WHERE evaluates the specified condition internally. As with any logical expressions, the performance is better if the operands of a comparison share a common type. The performance can be further enhanced if LOOP ... WHERE is combined with FROM i1 and/or TO i2, if possible.


Always use Pretty Printer and Extended Program Check before releasing the code. Do not leave unused code in the program. Comment the code thoroughly. Align the comments and the Code. Follow the SAP Standards and SAP Best Practices guidelines. It’s a good practice to take a dump of the code on your local drive.

Ole Automation Part1
Ole Automation Part 2
Processing Blocks in ABAP
Simple ABAP Report
ALV Grid - Changing Colors
ALV Report Example
Creating Variants For ABAP Reports
Recording BDC using Transaction
Sales Document Flow in ABAP
User Exits in SAP SD
SAP ABAP Naming Standards
SAP SD Tables
SAP ABAP Data Dictionary Tables
MM Important Transaction Codes in SAP
Passing g Data From One ABAP Program to Another
ABAP Compute Add Collect and Append
SAP ABAP Determining Attributes of Data
SAP ABAP Editor Icons
BAPI for Displaying Material Data
BAPI to get customer bank details
EDI Outbound Process
SAP EDI Process Overview
Function Module for Vendor Bank details
SAP IDOC
Creating a Valid Password in SAP
SAP BADIs Introduction
SAP ABAP MACROS
POP UP function Module to Confirm and Save Data
Select Options
BAPI for availability check
String to Numerical
SAP Goods Movement Process
Getting a List of Plants for a Material
SAP R3 Clients Concept
ABAP Adobe Forms
Authorization Object Tables
SAP Industry Specific Solutions

Sap Scripts and SmartForms Bar Codes
Standard Reports and Sap Scripts
Important Standard Reports in SAP
Abap Tricks and Tips
Bapi Sales Order
BAPI Purchase Order
Creating Function Modules in SAP
Creating Tables in SAP
Finding User Exits in SAP
Function Module Create Text and Read Text
Important Transaction Codes in SAP
ABAP Function Module for Submitting a Program
ABAP Game Tic Tac Toe
ABAP Internal Table To Excel Sheet
ABAP Function Module to create Directory
Different Types of Menus in SAP
Function Modules in SAP to check Loged in Users
ABAP Function Module for Adding Days to Dates
Call a Transaction From a Remote System
SAP MM simple Procurement Cycle
BAPI Material EDIT
Finding Decimal Places in Currency
Getting negative sign before a number in ABAP
Program Editor Lock Unlock
Restricting Select Options
List of BAPIs in the system
SAP Function Module Scramble a String
LSMW
POP up table contents on the screen
SAP R3 Bookmarking Websites
Stock Requirements List Function Module
Retail Transaction Codes
ABAP Debugger Break Points
ABAP Debugger WatchPoints
Drill Down Reports Concept
Creating a HOT SPOT
Interactive Programs and Hide Technique
String Concatenate
Get Week of the Year
SAP ABAP to Add Days to a Date
Add Months to a Date
Get Month in the Year
Display Clock

ABAP Code For Progress BAR
ABAP Function Module For Caluclator
ABAP Function Module For Calender
Displaying Messages in ABAP
Function Module Pop Up To Confirm
Conversion Routines in SAP
SAP ABAP Authorization
SAP ABAP Module Pool Tutorial
SAP ABAP RFC

Finding Path to SAP Transaction in Menu
SAP Purchasing Documents
SAP and ABAP Shortcuts
Logical Databases
Advantages of Logical Databases
Copy to Clipboard
BAPI Create Material
Finding and Running Programs in ABAP
Program Syntax Check and Extended Syntax Check
Select Options upper lower case
BAPI Sales Order Simulate
Get PLANT and Description for a Material
MRP List Function Module
Production Planning and Controlling
Applications in SAP R3
Tool Based Reports
Important Transaction Codes in SAP
SAP Stock per Bin
Pop Up a Calender
Module to Read a File
Module to Reverse A String
Run an Executable Program with Parameters
Program for POP up Screen
Printing Selection Parameters for a Report
Uploading and DownLoading a Report
SAP ABAP Version Management
SAP ABAP Short Cuts
List of Important System Variables

ABAP MACROS
ABAP Calling a File Selector
Some Important Function Modules
ABAP String Operations

ABAP Function Module to Check Validity of Date
Transfer Internal Table Contents to a File

SAP ABAP Program Types
BAPI to Read Customer Data
Checking Validity of Date
Download to Application Server
ABAP Debugger Breakpoint and Watchpoint
BAPI to get Company Code Details
Creating Material Using BAPI part 2
Generating a Valid Password
Logical Databases Structure
Making Fields OBligatory in Selection Screen
ABAP Views
Getting a Company Code for a Plant
Importing contents of Clipboard in SAP
Getting a Plant for a Material
Plant Material and Storage Location
SAP Production Planning Standard Reports
NetWeaver Components
Supported Databases and Operating Systems