Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
COBOL
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Features== ===Syntax=== COBOL has an English-like syntax, which is used to describe nearly everything in COBOL programs. For example, a condition can be expressed as {{code|x IS GREATER THAN y|lang=cobolfree}} or more concisely as {{code|x GREATER y|lang=cobolfree}} or {{code|x > y|lang=cobolfree}}. More complex conditions can be abbreviated by removing repeated conditions and variables. For example, {{code|1=a > b AND a > c OR a = d|lang=cobolfree}} can be shortened to {{code|1=a > b AND c OR = d|lang=cobolfree}}. To support this syntax, COBOL has over 300 [[keyword (computer programming)|keywords]].{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 8.9}}{{efn|Vendor-specific extensions cause many implementations to have far more: one implementation recognizes over 1,100 keywords.<ref>{{cite web | url=http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.visualcobol.vs2013/HRLHLHARES01U005.html | title=Reserved Words Table | publisher=[[Micro Focus]] | work=Micro Focus Visual COBOL 2.2 COBOL Language Reference | access-date=3 March 2014}}</ref>}} Some of the keywords are simple alternative or pluralized spellings of the same word, which provides for more grammatically appropriate statements and clauses; e.g., the {{code|IN}} and {{code|OF}} keywords can be used interchangeably, as can {{code|TIME}} and {{code|TIMES}}, and {{code|VALUE}} and {{code|VALUES}}. Each COBOL program is made up of four basic [[lexical items]]: words, literals, picture character-strings (see {{slink||PICTURE clause}}) and separators. Words include reserved words and user-defined identifiers. They are up to 31 characters long and may include letters, digits, hyphens and underscores. Literals include numerals (e.g. {{code|12}}) and strings (e.g. {{code|'Hello!'}}).{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 8.3.1.2}} Separators include the space character and commas and semi-colons followed by a space.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 8.3.2}} A COBOL program is split into four divisions: the identification division, the environment division, the data division and the procedure division. The identification division specifies the name and type of the source element and is where classes and interfaces are specified. The environment division specifies any program features that depend on the system running it, such as [[computer files|files]] and [[character sets]]. The data division is used to declare [[variable (computer science)|variable]]s and [[parameter (computer programming)|parameter]]s. The procedure division contains the program's [[statement (computer science)|statement]]s. Each division is sub-divided into sections, which are made up of paragraphs. ====Metalanguage==== COBOL's syntax is usually described with a unique [[metalanguage]] using braces, brackets, bars and underlining.<ref>{{cite web | url=https://users.cs.jmu.edu/adamses/Web/CS%20430%20Fall%202004/Student%20Project%20Work/Akanni%20-%20COBOL/COBOL%20Tutorial%20HTML-Pages/Introduction%20to%20COBOL.htm | title=Introduction to COBOL | publisher=[[James Madison University]] | access-date=10 December 2024}}</ref> The metalanguage was developed for the original COBOL specifications. {| class="wikitable" |+ Elements of COBOL's metalanguage |- ! Element ! Appearance ! Function |- |- | All capitals | EXAMPLE | Reserved word |- | Underlining | {{underline|EXAMPLE}} | The reserved word is compulsory |- | Braces | { } | Only one option may be selected |- | Brackets | [] | Zero or one options may be selected |- | Ellipsis | ... | The preceding element may be repeated |- | rowspan="2" | Bars | {<nowiki/>{{pipe}} {{pipe}}<nowiki/>}<!--nowiki tags prevent page from showing up on error lists--> | One or more options may be selected. Any option may only be selected once. |- | [{{pipe}} {{pipe}}] | Zero or more options may be selected. Any option may only be selected once. |} As an example, consider the following description of an <code>ADD</code> statement: <div class="exemple-add-statement" style="max-width: 100%; overflow-x: auto; padding: 0; font-size: 85%; line-height: 0;"> <math> \begin{array}{l} \underline{\text{ADD}}\, \begin{Bmatrix} \text{identifier-1} \\ \text{literal-1} \end{Bmatrix}\dots \;\underline{\text{TO}}\,\left\{\text{identifier-2}\,\left[\,\underline{\text{ROUNDED}}\,\right]\right\}\dots \\[1em] \quad \left[\left|\begin{array}{l} \text{ON}\,\underline{\text{SIZE}}\,\underline{\text{ERROR}}\,\text{imperative-statement-1} \\ \underline{\text{NOT}}\,\text{ON}\,\underline{\text{SIZE}}\,\underline{\text{ERROR}}\,\text{imperative-statement-2} \end{array}\right|\right] \\[1em] \quad \left[\,\underline{\text{END-ADD}}\,\right] \end{array} </math> </div> This description permits the following variants: <syntaxhighlight lang="cobolfree"> ADD 1 TO x ADD 1, a, b TO x ROUNDED, y, z ROUNDED ADD a, b TO c ON SIZE ERROR DISPLAY "Error" END-ADD ADD a TO b NOT SIZE ERROR DISPLAY "No error" ON SIZE ERROR DISPLAY "Error" </syntaxhighlight> ===Code format=== [[Image:IBM keypunch deck for Cobol student program at New York University 1979.jpg|thumb|right|COBOL program deck of punched cards, from the 1970s]] [[File:Original XEDIT.png|thumb|3270 session]] The height of COBOL's popularity coincided with the era of [[keypunch machine]]s and [[punched card]]s. The program itself was written onto punched cards, then read in and compiled, and the data fed into the program was sometimes on cards as well.{{sfn|McCracken|1976|pp=2, 6–9}} COBOL can be written in two formats: fixed (the default) or free. In fixed-format, code must be aligned to fit in certain areas (a hold-over from using punched cards). Until COBOL 2002, these were: {| class="wikitable" |- ! Name ! Column(s) ! Usage |- | Sequence number area | 1–6 | Originally used for card/line numbers (facilitating mechanical punched card sorting to assure intended program code sequence after manual editing/handling), this area is ignored by the compiler |- | Indicator area | 7 | The following characters are allowed here: * <code>*</code> – Comment line * <code>/</code> – Comment line that will be printed on a new page of a source listing * <code>-</code> – Continuation line, where words or literals from the previous line are continued * <code>D</code> – Line enabled in debugging mode, which is otherwise ignored |- | Area A | 8–11 | This contains: <code>DIVISION</code>, <code>SECTION</code> and procedure headers; 01 and 77 level numbers and file/report descriptors |- | Area B | 12–72 | Any other code not allowed in Area A |- | Program name area | 73– | Historically up to column 80 for punched cards, it is used to identify the program or sequence the card belongs to |} In COBOL 2002, Areas A and B were merged to form the program-text area, which now ends at an implementor-defined column.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2001|loc=§ F.2}} COBOL 2002 also introduced free-format code. Free-format code can be placed in any column of the file, as in newer programming languages. Comments are specified using <code>*></code>, which can be placed anywhere and can also be used in fixed-format source code. Continuation lines are not present, and the <code>>>PAGE</code> directive replaces the <code>/</code> indicator.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2001|loc=§ F.2}} ===Identification division=== The identification division identifies the following code entity and contains the definition of a class or interface. ====Object-oriented programming==== [[class (computer programming)|Classes]] and [[Interface (object-oriented programming)|interfaces]] have been in COBOL since 2002. Classes have factory objects, containing class [[method (computer programming)|methods]] and variables, and instance objects, containing instance methods and variables.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ D.18.2}} Inheritance and interfaces provide [[polymorphism (computer science)|polymorphism]]. Support for [[generic programming]] is provided through parameterized classes, which can be instantiated to use any class or interface. Objects are stored as references which may be restricted to a certain type. There are two ways of calling a method: the {{code|INVOKE}} statement, which acts similarly to {{code|CALL}}, or through inline method invocation, which is analogous to using functions.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ D.18}} <syntaxhighlight lang="cobolfree"> *> These are equivalent. INVOKE my-class "foo" RETURNING var MOVE my-class::"foo" TO var *> Inline method invocation </syntaxhighlight> COBOL does not provide a way to [[Member visibility|hide]] methods. Class data can be hidden, however, by declaring it without a [[property (programming)|{{code|PROPERTY}}]] clause, which leaves external code no way to access it.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|p=108}} [[Method overloading]] was added in COBOL 2014.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|p=896}} ===Environment division=== The environment division contains the configuration section and the input-output section. The configuration section is used to specify variable features such as currency signs, locales and character sets. The input-output section contains file-related information. ====Files==== COBOL supports three file formats, or ''{{dfn|organizations}}'': sequential, indexed and relative. In sequential files, records are contiguous and must be traversed [[sequential access|sequentially]], similarly to a [[linked list]]. Indexed files have one or more indexes which allow records to be [[random access|randomly accessed]] and which can be sorted on them. Each record must have a [[unique key]], but other, ''{{dfn|alternate}}'', record keys need not be unique. Implementations of indexed files vary between vendors, although common implementations, such as [[C-ISAM]] and [[VSAM]], are based on IBM's [[ISAM]]. Other implementations are [[Record Management Services]] on [[OpenVMS]] and [[Enscribe]] on [[NonStop (server computers)|HPE NonStop]] (Tandem). Relative files, like indexed files, have a unique record key, but they do not have alternate keys. A relative record's key is its ordinal position; for example, the 10th record has a key of 10. This means that creating a record with a key of 5 may require the creation of (empty) preceding records. Relative files also allow for both sequential and random access.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ D.2.1}} A common non-standard extension is the ''{{dfn|line sequential}}'' organization, used to process text files. Records in a file are terminated by a [[newline]] and may be of varying length.<ref>{{cite web | url=http://supportline.microfocus.com/documentation/books/nx30books/fhorgs.htm | title=File Organizations | publisher=Micro Focus | work=File Handling | date=1998 | access-date=27 June 2014 | archive-date=4 March 2016 | archive-url=https://web.archive.org/web/20160304091459/http://supportline.microfocus.com/documentation/books/nx30books/fhorgs.htm | url-status=dead }}</ref> ===Data division=== The data division is split into six sections which declare different items: the file section, for file records; the working-storage section, for [[static variable]]s; the local-storage section, for [[automatic variable]]s; the linkage section, for parameters and the return value; the report section and the screen section, for [[text-based user interface]]s. ====Aggregated data==== Data items in COBOL are declared hierarchically through the use of level-numbers which indicate if a data item is part of another. An item with a higher level-number is subordinate to an item with a lower one. Top-level data items, with a level-number of 1, are called ''{{dfn|records}}''. Items that have subordinate aggregate data are called ''{{dfn|group items}}''; those that do not are called ''{{dfn|elementary items}}''. Level-numbers used to describe standard data items are between 1 and 49.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 8.5.1.2}}{{sfn|Cutler|2014|loc=Appendix A}} <syntaxhighlight lang="cobol"> 01 some-record. *> Aggregate group record item 05 num PIC 9(10). *> Elementary item 05 the-date. *> Aggregate (sub)group record item 10 the-year PIC 9(4). *> Elementary item 10 the-month PIC 99. *> Elementary item 10 the-day PIC 99. *> Elementary item </syntaxhighlight> In the above example, elementary item {{code|num}} and group item {{code|the-date}} are subordinate to the record {{code|some-record}}, while elementary items {{code|the-year}}, {{code|the-month}}, and {{code|the-day}} are part of the group item {{code|the-date}}. Subordinate items can be disambiguated with the {{code|IN}} (or {{code|OF}}) keyword. For example, consider the example code above along with the following example: <syntaxhighlight lang="cobol"> 01 sale-date. 05 the-year PIC 9(4). 05 the-month PIC 99. 05 the-day PIC 99. </syntaxhighlight> The names {{code|the-year}}, {{code|the-month}}, and {{code|the-day}} are ambiguous by themselves, since more than one data item is defined with those names. To specify a particular data item, for instance one of the items contained within the {{code|sale-date}} group, the programmer would use {{code|the-year IN sale-date}} (or the equivalent {{code|the-year OF sale-date}}). This syntax is similar to the "dot notation" supported by most contemporary languages. ====Other data levels==== A level-number of 66 is used to declare a re-grouping of previously defined items, irrespective of how those items are structured. This data level, also referred to by the associated {{dfn|{{code|RENAMES}} clause}}, is rarely used<ref>{{cite book | title=Sams Teach Yourself COBOL in 24 hours | publisher=[[Sams|SAMS Publishing]] | year=1999 | pages=40 | isbn=978-0672314537 | first=Thane | last=Hubbell | lccn=98087215}}</ref> and, circa 1988, was usually found in old programs. Its ability to ignore the hierarchical and logical structure data meant its use was not recommended and many installations forbade its use.{{sfn|McCracken|Golden|1988|loc=§ 19.9}} <syntaxhighlight lang="cobol"> 01 customer-record. 05 cust-key PIC X(10). 05 cust-name. 10 cust-first-name PIC X(30). 10 cust-last-name PIC X(30). 05 cust-dob PIC 9(8). 05 cust-balance PIC 9(7)V99. 66 cust-personal-details RENAMES cust-name THRU cust-dob. 66 cust-all-details RENAMES cust-name THRU cust-balance. </syntaxhighlight> A 77 level-number indicates the item is stand-alone, and in such situations is equivalent to the level-number 01. For example, the following code declares two 77-level data items, {{code|property-name}} and {{code|sales-region}}, which are non-group data items that are independent of (not subordinate to) any other data items: <syntaxhighlight lang="cobol"> 77 property-name PIC X(80). 77 sales-region PIC 9(5). </syntaxhighlight> An 88 level-number declares a ''{{dfn|condition name}}'' (a so-called 88-level) which is true when its parent data item contains one of the values specified in its {{code|VALUE}} clause.{{sfn|Cutler|2014|loc=§ 5.8.5}} For example, the following code defines two 88-level condition-name items that are true or false depending on the current character data value of the {{code|wage-type}} data item. When the data item contains a value of {{code|'H'}}, the condition-name {{code|wage-is-hourly}} is true, whereas when it contains a value of {{code|'S'}} or {{code|'Y'}}, the condition-name {{code|wage-is-yearly}} is true. If the data item contains some other value, both of the condition-names are false. <syntaxhighlight lang="cobol"> 01 wage-type PIC X. 88 wage-is-hourly VALUE "H". 88 wage-is-yearly VALUE "S", "Y". </syntaxhighlight> ====Data types==== Standard COBOL provides the following data types:{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 8.5.2}} {| class="wikitable" |- ! Data type ! Sample declaration ! Notes |- | Alphabetic | {{code|PIC A(30)|lang=cobolfree}} | May contain only letters or spaces. |- | Alphanumeric | {{code|PIC X(30)|lang=cobolfree}} | May contain any characters. |- | Boolean | {{code|PIC 1 USAGE BIT|lang=cobolfree}} | Data stored in the form of 0s and 1s, as a binary number. |- | Index | {{code|USAGE INDEX|lang=cobolfree}} | Used to reference table elements. |- | National | {{code|PIC N(30)|lang=cobolfree}} | Similar to alphanumeric, but using an extended character set, e.g. [[UTF-8]]. |- | Numeric | {{code|PIC 9(5)V9(2)|lang=cobolfree}} | Contains exactly 7 digits (7=5+2). 'V' locates the implicit decimal in a fixed point number. |- | Object | {{code|USAGE OBJECT REFERENCE|lang=cobolfree}} | May reference either an object or <code>NULL</code>. |- | Pointer | {{code|USAGE POINTER|lang=cobolfree}} | |} Type safety is variable in COBOL. Numeric data is converted between different representations and sizes silently and alphanumeric data can be placed in any data item that can be stored as a string, including numeric and group data.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9.24}} In contrast, object references and pointers may only be assigned from items of the same type and their values may be restricted to a certain type.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9.35}} =====PICTURE clause===== A {{code|PICTURE}} (or {{code|PIC}}) clause is a string of characters, each of which represents a portion of the data item and what it may contain. Some picture characters specify the type of the item and how many characters or digits it occupies in memory. For example, a {{code|9}} indicates a decimal digit, and an {{code|S}} indicates that the item is [[signedness|signed]]. Other picture characters (called ''{{dfn|insertion}}'' and ''{{dfn|editing}}'' characters) specify how an item should be formatted. For example, a series of {{code|+}} characters define character positions as well as how a leading sign character is to be positioned within the final character data; the rightmost non-numeric character will contain the item's sign, while other character positions corresponding to a {{code|+}} to the left of this position will contain a space. Repeated characters can be specified more concisely by specifying a number in parentheses after a picture character; for example, {{code|9(7)}} is equivalent to {{code|9999999}}. Picture specifications containing only digit ({{code|9}}) and sign ({{code|S}}) characters define purely ''{{dfn|numeric}}'' data items, while picture specifications containing alphabetic ({{code|A}}) or alphanumeric ({{code|X}}) characters define ''{{dfn|alphanumeric}}'' data items. The presence of other formatting characters define ''{{dfn|edited numeric}}'' or ''{{dfn|edited alphanumeric}}'' data items.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 13.18.40}} {| class="wikitable" |+ Examples |- ! {{code|PICTURE}} clause ! Value in ! Value out |- | rowspan="2" | {{code|PIC 9(5)|lang=cobolfree}} | {{code|100}} | {{code|00100}} |- | {{code|"Hello"}} | {{code|"Hello"}} (this is legal, but results in [[undefined behavior]]){{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9.24}} |- | {{code|PIC +++++|lang=cobolfree}} | {{code|-10}} | <code>" -10"</code><!-- {{code| -10}} deletes one of the spaces --> (note leading spaces) |- | {{code|PIC 99/99/9(4)|lang=cobolfree}} | {{code|30042003}} | {{code|"30/04/2003"}} |- | rowspan="2" | {{code|PIC *(4)9.99|lang=cobolfree}} | {{code|100.50}} | {{code|"**100.50"}} |- | <code>0</code> <!-- {{code|0}} produces a space for some reason --> | {{code|"****0.00"}} |- | {{code|PIC X(3)BX(3)BX(3)|lang=cobolfree}} | {{code|"ABCDEFGHI"}} | {{code|"ABC DEF GHI"}} |} =====USAGE clause===== {{missing information|section|COMPUTATIONAL-5|date=April 2021}} The {{code|USAGE}} clause declares the format in which data is stored. Depending on the data type, it can either complement or be used instead of a {{code|PICTURE}} clause. While it can be used to declare pointers and object references, it is mostly geared towards specifying numeric types. These numeric formats are:{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 13.18.60.3}} * Binary, where a minimum size is either specified by the <code>PICTURE</code> clause or by a <code>USAGE</code> clause such as <code>BINARY-LONG</code> * {{code|USAGE COMPUTATIONAL|lang=cobolfree}}, where data may be stored in whatever format the implementation provides; often equivalent to {{code|USAGE BINARY|lang=cobolfree}} * {{code|USAGE DISPLAY|lang=cobolfree}}, the default format, where data is stored as a string * Floating-point, in either an implementation-dependent format or according to IEEE 754 * {{code|USAGE NATIONAL|lang=cobolfree}}, where data is stored as a string using an extended character set * {{code|USAGE PACKED-DECIMAL|lang=cobolfree}}, where data is stored in the smallest possible decimal format (typically [[packed binary-coded decimal]]) ====Report writer==== The report writer is a [[declarative programming|declarative facility]] for creating reports. The programmer need only specify the report layout and the data required to produce it, freeing them from having to write code to handle things like page breaks, data formatting, and headings and footings.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|p=855}} Reports are associated with report files, which are files which may only be written to through report writer statements. <syntaxhighlight lang="cobol"> FD report-out REPORT sales-report. </syntaxhighlight> Each report is defined in the report section of the data division. A report is split into report groups which define the report's headings, footings and details. Reports work around hierarchical ''{{dfn|control breaks}}''. Control breaks occur when a key variable changes it value; for example, when creating a report detailing customers' orders, a control break could occur when the program reaches a different customer's orders. Here is an example report description for a report which gives a salesperson's sales and which warns of any invalid records: <syntaxhighlight lang="cobol"> RD sales-report PAGE LIMITS 60 LINES FIRST DETAIL 3 CONTROLS seller-name. 01 TYPE PAGE HEADING. 03 COL 1 VALUE "Sales Report". 03 COL 74 VALUE "Page". 03 COL 79 PIC Z9 SOURCE PAGE-COUNTER. 01 sales-on-day TYPE DETAIL, LINE + 1. 03 COL 3 VALUE "Sales on". 03 COL 12 PIC 99/99/9999 SOURCE sales-date. 03 COL 21 VALUE "were". 03 COL 26 PIC $$$$9.99 SOURCE sales-amount. 01 invalid-sales TYPE DETAIL, LINE + 1. 03 COL 3 VALUE "INVALID RECORD:". 03 COL 19 PIC X(34) SOURCE sales-record. 01 TYPE CONTROL HEADING seller-name, LINE + 2. 03 COL 1 VALUE "Seller:". 03 COL 9 PIC X(30) SOURCE seller-name. </syntaxhighlight> The above report description describes the following layout: <pre> Sales Report Page 1 Seller: Howard Bromberg Sales on 10/12/2008 were $1000.00 Sales on 12/12/2008 were $0.00 Sales on 13/12/2008 were $31.47 INVALID RECORD: Howard Bromberg XXXXYY Seller: Howard Discount ... Sales Report Page 12 Sales on 08/05/2014 were $543.98 INVALID RECORD: William Selden 12052014FOOFOO Sales on 30/05/2014 were $0.00 </pre> Four statements control the report writer: {{code|INITIATE}}, which prepares the report writer for printing; {{code|GENERATE}}, which prints a report group; {{code|SUPPRESS}}, which suppresses the printing of a report group; and {{code|TERMINATE}}, which terminates report processing. For the above sales report example, the procedure division might look like this: <syntaxhighlight lang="cobol"> OPEN INPUT sales, OUTPUT report-out INITIATE sales-report PERFORM UNTIL 1 <> 1 READ sales AT END EXIT PERFORM END-READ VALIDATE sales-record IF valid-record GENERATE sales-on-day ELSE GENERATE invalid-sales END-IF END-PERFORM TERMINATE sales-report CLOSE sales, report-out . </syntaxhighlight> Use of the Report Writer facility tends to vary considerably; some organizations use it extensively and some not at all.{{sfn|McCracken|1976|p=338}} In addition, implementations of Report Writer ranged in quality, with those at the lower end sometimes using excessive amounts of memory at runtime.{{sfn|McCracken|1976|p=338}} ===Procedure division=== ====Procedures==== The sections and paragraphs in the procedure division (collectively called procedures) can be used as [[label (programming)|labels]] and as simple [[subroutines]]. Unlike in other divisions, paragraphs do not need to be in sections.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.4}} Execution goes down through the procedures of a program until it is terminated.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.6.3}} To use procedures as subroutines, the {{code|PERFORM}} verb is used. A {{code|PERFORM}} statement somewhat resembles a procedure call in a newer languages in the sense that execution returns to the code following the {{code|PERFORM}} statement at the end of the called code; however, it does not provide a mechanism for [[Parameter (computer programming)|parameter passing]] or for returning a result value. If a subroutine is invoked using a simple statement like {{code|PERFORM subroutine|lang=cobolfree}}, then control returns at the end of the called procedure. However, {{code|PERFORM}} is unusual in that it may be used to call a range spanning a sequence of several adjacent procedures. This is done with the {{code|PERFORM sub-1 THRU sub-n|lang=cobolfree}} construct: <syntaxhighlight lang="cobolfree"> PROCEDURE so-and-so. PERFORM ALPHA PERFORM ALPHA THRU GAMMA STOP RUN. ALPHA. DISPLAY 'A'. BETA. DISPLAY 'B'. GAMMA. DISPLAY 'C'. </syntaxhighlight> The output of this program will be: "A A B C". {{code|PERFORM}} also differs from conventional procedure calls in that there is, at least traditionally, no notion of a call stack. As a consequence, nested invocations are possible (a sequence of code being {{code|PERFORM}}'ed may execute a {{code|PERFORM}} statement itself), but require extra care if parts of the same code are executed by both invocations. The problem arises when the code in the inner invocation reaches the exit point of the outer invocation. More formally, if control passes through the exit point of a {{code|PERFORM}} invocation that was called earlier but has not yet completed, the COBOL 2002 standard stipulates that the behavior is [[Undefined behavior|undefined]]. The reason is that COBOL, rather than a "return address", operates with what may be called a continuation address. When control flow reaches the end of any procedure, the continuation address is looked up and control is transferred to that address. Before the program runs, the continuation address for every procedure is initialized to the start address of the procedure that comes next in the program text so that, if no {{code|PERFORM}} statements happen, control flows from top to bottom through the program. But when a {{code|PERFORM}} statement executes, it modifies the continuation address of the called procedure (or the last procedure of the called range, if {{code|PERFORM THRU}} was used), so that control will return to the call site at the end. The original value is saved and is restored afterwards, but there is only one storage position. If two nested invocations operate on overlapping code, they may interfere which each other's management of the continuation address in several ways.<ref name="FR99">{{cite conference | url=http://pages.cs.wisc.edu/~ramali/Papers/paste99.pdf |archive-url=https://web.archive.org/web/20101224060615/http://pages.cs.wisc.edu/~ramali/Papers/paste99.pdf |archive-date=24 December 2010 |url-status=live | title=Identifying Procedural Structure in Cobol Programs | doi=10.1145/381788.316163 | first1=John | last1=Field | first2=G. | last2=Ramalingam | conference=PASTE '99 | isbn=1581131372 | conference-url=http://cseweb.ucsd.edu/~wgg/paste99.html | date=September 1999}}</ref><ref name="VV07">{{cite journal | url=http://www.cs.vu.nl/~nveerman/research/minefield/minefield.pdf | title=Cobol minefield detection | journal=Software: Practice and Experience |date=November 2006 | volume=36 | issue=14 | doi=10.1002/spe.v36:14 | archive-url=https://web.archive.org/web/20070306135410/http://www.cs.vu.nl/~nveerman/research/minefield/minefield.pdf | url-status=dead | archive-date=6 March 2007 | last1=Veerman | first1=Niels | last2=Verhoeven | first2=Ernst-Jan | s2cid=18619757 }}</ref> The following example (taken from {{harvnb|Veerman|Verhoeven|2006}}) illustrates the problem: <syntaxhighlight lang="cobolfree"> LABEL1. DISPLAY '1' PERFORM LABEL2 THRU LABEL3 STOP RUN. LABEL2. DISPLAY '2' PERFORM LABEL3 THRU LABEL4. LABEL3. DISPLAY '3'. LABEL4. DISPLAY '4'. </syntaxhighlight> One might expect that the output of this program would be "1 2 3 4 3": After displaying "2", the second {{code|PERFORM}} causes "3" and "4" to be displayed, and then the first invocation continues on with "3". In traditional COBOL implementations, this is not the case. Rather, the first {{code|PERFORM}} statement sets the continuation address at the end of {{code|LABEL3}} so that it will jump back to the call site inside {{code|LABEL1}}. The second {{code|PERFORM}} statement sets the return at the end of {{code|LABEL4}} but does not modify the continuation address of {{code|LABEL3}}, expecting it to be the default continuation. Thus, when the inner invocation arrives at the end of {{code|LABEL3}}, it jumps back to the outer {{code|PERFORM}} statement, and the program stops having printed just "1 2 3". On the other hand, in some COBOL implementations like the open-source TinyCOBOL compiler, the two {{code|PERFORM}} statements do not interfere with each other and the output is indeed "1 2 3 4 3". Therefore, the behavior in such cases is not only (perhaps) surprising, it is also not portable.<ref name="VV07"/> A special consequence of this limitation is that {{code|PERFORM}} cannot be used to write recursive code. Another simple example to illustrate this (slightly simplified from {{harvnb|Veerman|Verhoeven|2006}}): <syntaxhighlight lang="cobolfree"> MOVE 1 TO A PERFORM LABEL STOP RUN. LABEL. DISPLAY A IF A < 3 ADD 1 TO A PERFORM LABEL END-IF DISPLAY 'END'. </syntaxhighlight> One might expect that the output is "1 2 3 END END END", and in fact that is what some COBOL compilers will produce. But other compilers, like IBM COBOL, will produce code that prints "1 2 3 END END END END ..." and so on, printing "END" over and over in an endless loop. Since there is limited space to store backup continuation addresses, the backups get overwritten in the course of recursive invocations, and all that can be restored is the jump back to {{code|DISPLAY 'END'}}.<ref name="VV07"/> ====Statements==== COBOL 2014 has 47 statements (also called ''{{dfn|verbs}}''),{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9}} which can be grouped into the following broad categories: control flow, I/O, data manipulation and the report writer. The report writer statements are covered in the [[#Report writer|report writer section]]. =====Control flow===== COBOL's [[conditional (computer programming)|conditional statements]] are {{code|IF}} and {{code|EVALUATE}}. {{code|EVALUATE}} is a [[switch statement|switch-like statement]] with the added capability of evaluating multiple values and conditions. This can be used to implement [[decision table]]s. For example, the following might be used to control a [[CNC lathe]]: <!-- a more common appliance would be more suitable. --> <syntaxhighlight lang="cobolfree"> EVALUATE TRUE ALSO desired-speed ALSO current-speed WHEN lid-closed ALSO min-speed THRU max-speed ALSO LESS THAN desired-speed PERFORM speed-up-machine WHEN lid-closed ALSO min-speed THRU max-speed ALSO GREATER THAN desired-speed PERFORM slow-down-machine WHEN lid-open ALSO ANY ALSO NOT ZERO PERFORM emergency-stop WHEN OTHER CONTINUE END-EVALUATE </syntaxhighlight> The {{code|PERFORM}} statement is used to define loops which are executed {{em|until}} a condition is true (not {{em|while}} true, which is more common in other languages). It is also used to call procedures or ranges of procedures (see the [[#Procedures|procedures section]] for more details). {{code|CALL}} and {{code|INVOKE}} call subprograms and methods, respectively. The name of the subprogram/method is contained in a string which may be a literal or a data item.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§§ 14.9.4, 14.9.22}} Parameters can be passed [[call by reference|by reference]], by content (where a copy is passed by reference) or [[call by value|by value]] (but only if a [[function prototype|prototype]] is available).{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ D.6.5.2.2}} {{code|CANCEL}} unloads subprograms from memory. {{code|GO TO}} causes the program to jump to a specified procedure. The {{code|GOBACK}} statement is a [[return statement]] and the {{code|STOP}} statement stops the program. The {{code|EXIT}} statement has six different formats: it can be used as a return statement, a [[break statement]], a [[continue statement]], an end marker or to leave a procedure.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9.13.1}} [[Exception handling|Exceptions]] are raised by a {{code|RAISE}} statement and caught with a handler, or ''{{dfn|declarative}}'', defined in the {{code|DECLARATIVES}} portion of the procedure division. Declaratives are sections beginning with a {{code|USE}} statement which specify the errors to handle. Exceptions can be names or objects. {{code|RESUME}} is used in a declarative to jump to the statement after the one that raised the exception or to a procedure outside the {{code|DECLARATIVES}}. Unlike other languages, uncaught exceptions may not terminate the program and the program can proceed unaffected. =====I/O===== File I/O is handled by the self-describing {{code|OPEN}}, {{code|CLOSE}}, {{code|READ}}, and {{code|WRITE}} statements along with a further three: {{code|REWRITE}}, which updates a record; {{code|START}}, which selects subsequent records to access by finding a record with a certain key; and {{code|UNLOCK}}, which releases a [[lock (database)|lock]] on the last record accessed. User interaction is done using {{code|ACCEPT}} and {{code|DISPLAY}}. =====Data manipulation===== The following verbs manipulate data: * {{code|INITIALIZE}}, which sets data items to their default values. * {{code|MOVE}}, which [[assignment (computer science)|assigns]] values to data items ; ''MOVE CORRESPONDING'' assigns corresponding like-named [[Record (computer science)#COBOL|fields]]. * {{code|SET}}, which has 15 formats: it can modify indices, assign object references and alter table capacities, among other functions.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§14.9.35.1}} * {{code|ADD}}, {{code|SUBTRACT}}, {{code|MULTIPLY}}, {{code|DIVIDE}}, and {{code|COMPUTE}}, which handle arithmetic (with {{code|COMPUTE}} assigning the result of a formula to a variable). * {{code|ALLOCATE}} and {{code|FREE}}, which handle [[dynamic memory]]. * {{code|VALIDATE}}, which validates and distributes data as specified in an item's description in the data division. * {{code|STRING}} and {{code|UNSTRING}}, which [[Concatenation|concatenate]] and split [[string (computer science)|string]]s, respectively. * {{code|INSPECT}}, which tallies or replaces instances of specified [[substring]]s within a string. * {{code|SEARCH}}, which searches a table for the first entry satisfying a condition. Files and tables are sorted using {{code|SORT}} and the {{code|MERGE}} verb merges and sorts files. The {{code|RELEASE}} verb provides records to sort and {{code|RETURN}} retrieves sorted records in order. ====Scope termination==== Some statements, such as {{code|IF}} and {{code|READ}}, may themselves contain statements. Such statements may be terminated in two ways: by a period (''{{dfn|implicit termination}}''), which terminates ''all'' unterminated statements contained, or by a scope terminator, which terminates the nearest matching open statement. <syntaxhighlight lang="cobolfree"> *> Terminator period ("implicit termination") IF invalid-record IF no-more-records NEXT SENTENCE ELSE READ record-file AT END SET no-more-records TO TRUE. *> Scope terminators ("explicit termination") IF invalid-record IF no-more-records CONTINUE ELSE READ record-file AT END SET no-more-records TO TRUE END-READ END-IF END-IF </syntaxhighlight> Nested statements terminated with a period are a common source of bugs.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|p=899}}{{sfn|McCracken|Golden|1988|loc=§ 8.4}} For example, examine the following code: <syntaxhighlight lang="cobolfree"> IF x DISPLAY y. DISPLAY z. </syntaxhighlight> Here, the intent is to display <code>y</code> and <code>z</code> if condition <code>x</code> is true. However, <code>z</code> will be displayed whatever the value of <code>x</code> because the <code>IF</code> statement is terminated by an erroneous period after {{code|DISPLAY y|lang=cobolfree}}. Another bug is a result of the [[dangling else problem]], when two <code>IF</code> statements can associate with an <code>ELSE</code>. <syntaxhighlight lang="cobolfree"> IF x IF y DISPLAY a ELSE DISPLAY b. </syntaxhighlight> In the above fragment, the <code>ELSE</code> associates with the {{code|IF y|lang=cobolfree}} statement instead of the {{code|IF x|lang=cobolfree}} statement, causing a bug. Prior to the introduction of explicit scope terminators, preventing it would require {{code|ELSE NEXT SENTENCE|lang=cobolfree}} to be placed after the inner <code>IF</code>.{{sfn|McCracken|Golden|1988|loc=§ 8.4}} ====Self-modifying code==== The original (1959) COBOL specification supported the infamous {{code|ALTER X TO PROCEED TO Y|lang=cobolfree}} statement, for which many compilers generated [[self-modifying code]]. <code>X</code> and <code>Y</code> are procedure labels, and the single {{code|GO TO|lang=cobolfree}} statement in procedure <code>X</code> executed after such an {{code|ALTER}} statement means {{code|GO TO Y|lang=cobolfree}} instead. Many compilers still support it,<ref>Examples of compiler support for {{code|ALTER}} can be seen in the following: * {{cite web|ref=none |last=Tiffin |first=Brian |title=September 2013 |url=http://sourceforge.net/p/open-cobol/discussion/cobol/thread/7dc2941f/#5ee9 |work=GNU Cobol |date=18 September 2013 |access-date=5 January 2014 |archive-url=https://web.archive.org/web/20140505181734/http://sourceforge.net/p/open-cobol/discussion/cobol/thread/7dc2941f/ |archive-date=5 May 2014 |url-status=dead }} * {{cite web|ref=none |url=http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.visualcobol.vs2013/HRLHLHPDF803.html |title=The ALTER Statement |publisher=Micro Focus |work=Micro Focus Visual COBOL 2.2 for Visual Studio 2013 COBOL Language Reference |access-date=5 January 2014 }} * {{cite web|ref=none |url=http://www.csim.scu.edu.tw/~kuo/COBOL/COBOLCompiler/COBOL%E6%89%8B%E5%86%8A/cob_lrf.pdf |title=ALTER Statement (Nucleus) |publisher=Fujitsu |work=COBOL85 Reference Manual |date=November 1996 |access-date=5 January 2014 |page=555 |archive-url=https://web.archive.org/web/20140106031540/http://www.csim.scu.edu.tw/~kuo/COBOL/COBOLCompiler/COBOL%E6%89%8B%E5%86%8A/cob_lrf.pdf |archive-date=6 January 2014 |url-status=dead }} * {{cite web|ref=none |url=http://pic.dhe.ibm.com/infocenter/pdthelp/v1r1/topic/com.ibm.entcobol.doc_5.1/PGandLR/ref/rlpsalte.html |title=ALTER Statement |publisher=IBM |work=Enterprise COBOL for z/OS Language Reference |date=June 2013 |access-date=5 January 2014 }}</ref> but it was deemed [[deprecation|obsolete]] in the COBOL 1985 standard and deleted in 2002.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2001|loc=§ F.1}} The {{code|ALTER}} statement was poorly regarded because it undermined "locality of context" and made a program's overall logic difficult to comprehend.{{sfn|McCracken|1976|p=355}} As textbook author [[Daniel D. McCracken]] wrote in 1976, when "someone who has never seen the program before must become familiar with it as quickly as possible, sometimes under critical time pressure because the program has failed ... the sight of a GO TO statement in a paragraph by itself, signaling as it does the existence of an unknown number of ALTER statements at unknown locations throughout the program, strikes fear in the heart of the bravest programmer."{{sfn|McCracken|1976|p=355}} ===Hello, world=== A [["Hello, World!" program]] in COBOL: <syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. PROGRAM-ID. hello-world. PROCEDURE DIVISION. DISPLAY "Hello, world!" . </syntaxhighlight> When the now famous "Hello, World!" program example in ''[[The C Programming Language]]'' was first published in 1978 a similar mainframe COBOL program sample would have been submitted through [[Job Control Language|JCL]], very likely using a punch card reader, and 80 column punch cards. The listing below, ''with an empty DATA DIVISION'', was tested using Linux and the System/370 [[Hercules emulator]] running [[MVS]] 3.8J. The JCL, written in July 2015, is derived from the Hercules tutorials and samples hosted by Jay Moseley.<ref>{{cite web | url=http://www.jaymoseley.com/hercules/compilers/cobol.htm | title=COBOL Compiler from MVT | date=17 January 2015 | access-date=19 July 2015 | first=Jay | last=Moseley}}</ref> In keeping with COBOL programming of that era, HELLO, WORLD is displayed in all capital letters. <syntaxhighlight lang="cobolfree"> //COBUCLG JOB (001),'COBOL BASE TEST', 00010000 // CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1) 00020000 //BASETEST EXEC COBUCLG 00030000 //COB.SYSIN DD * 00040000 00000* VALIDATION OF BASE COBOL INSTALL 00050000 01000 IDENTIFICATION DIVISION. 00060000 01100 PROGRAM-ID. 'HELLO'. 00070000 02000 ENVIRONMENT DIVISION. 00080000 02100 CONFIGURATION SECTION. 00090000 02110 SOURCE-COMPUTER. GNULINUX. 00100000 02120 OBJECT-COMPUTER. HERCULES. 00110000 02200 SPECIAL-NAMES. 00120000 02210 CONSOLE IS CONSL. 00130000 03000 DATA DIVISION. 00140000 04000 PROCEDURE DIVISION. 00150000 04100 00-MAIN. 00160000 04110 DISPLAY 'HELLO, WORLD' UPON CONSL. 00170000 04900 STOP RUN. 00180000 //LKED.SYSLIB DD DSNAME=SYS1.COBLIB,DISP=SHR 00190000 // DD DSNAME=SYS1.LINKLIB,DISP=SHR 00200000 //GO.SYSPRINT DD SYSOUT=A 00210000 // 00220000 </syntaxhighlight> After submitting the JCL, the MVS console displayed: <syntaxhighlight lang="text" highlight="10"> 19.52.48 JOB 3 $HASP100 COBUCLG ON READER1 COBOL BASE TEST 19.52.48 JOB 3 IEF677I WARNING MESSAGE(S) FOR JOB COBUCLG ISSUED 19.52.48 JOB 3 $HASP373 COBUCLG STARTED - INIT 1 - CLASS A - SYS BSP1 19.52.48 JOB 3 IEC130I SYSPUNCH DD STATEMENT MISSING 19.52.48 JOB 3 IEC130I SYSLIB DD STATEMENT MISSING 19.52.48 JOB 3 IEC130I SYSPUNCH DD STATEMENT MISSING 19.52.48 JOB 3 IEFACTRT - Stepname Procstep Program Retcode 19.52.48 JOB 3 COBUCLG BASETEST COB IKFCBL00 RC= 0000 19.52.48 JOB 3 COBUCLG BASETEST LKED IEWL RC= 0000 19.52.48 JOB 3 +HELLO, WORLD 19.52.48 JOB 3 COBUCLG BASETEST GO PGM=*.DD RC= 0000 19.52.48 JOB 3 $HASP395 COBUCLG ENDED </syntaxhighlight> ''Line 10 of the console listing above is highlighted for effect, the highlighting is not part of the actual console output''. The associated compiler listing generated over four pages of technical detail and job run information, for the single line of output from the 14 lines of COBOL.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
COBOL
(section)
Add topic