Baixe GNUCOBOL e outras Manuais, Projetos, Pesquisas em PDF para Informática, somente na Docsity!
GNU COBOL Programmer’s Guide
For Version 2.1 [23NOV2013]
This manual documents GNU COBOL 2.1, 23NOV2013 build.
GNU-COBOL Copyright 2002-2007 Keisuke Nishida Copyright 2007-2012 Roger While Copyright 2013-2013 Ron Norman (RWCS for GNU COBOL) Document Copyright 2009-2014 Gary L. Cutler
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License [FDL], Version 1.3 or any later version published by the Free Software Foundation; with Invariant Section ”Intro- duction”, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled ”GNU Free Documentation License”.
- Introduction Table of Contents
- 1.1. Additional Reference Sources
- 1.2. Introducing COBOL
- 1.2.1. Why YOU Should Learn COBOL
- 1.2.2. Programmer Productivity
- 1.3. So What is GNU COBOL?
- 1.3.1. Language Reserved Words
- 1.3.2. User-Defined Words
- 1.3.3. Case Insensitivity
- 1.3.4. Readability of Programs
- 1.3.5. Divisions Organize Programs
- 1.3.6. Copybooks
- 1.3.7. Structured Data
- 1.3.8. Files
- 1.3.9. Table Handling.
- 1.3.10. Sorting and Merging Data
- 1.3.11. String Manipulation Features.
- 1.3.12. Screen Formatting Features
- 1.3.12.1. A Sample Screen
- 1.3.12.2. Color Palette and Video Attributes
- 1.3.13. Report Writer Features
- 1.3.14. Data Initialization
- 1.3.15. Syntax Diagram Conventions
- 1.3.16. Format of Program Source Lines
- 1.3.17. Program Structure
- 1.3.18. Comments
- 1.3.19. Literals
- 1.3.19.1. Numeric Literals
- 1.3.19.2. Alphanumeric Literals
- 1.3.19.3. Figurative Constants
- 1.3.20. Punctuation
- 1.3.21. LENGTH OF
- 1.3.22. Interfacing to Other Environments
- CDF - Compiler Directing Facility
- 2.1. COPY
- 2.2. REPLACE.
- 2.3. >>DEFINE
- 2.4. >>IF
- 2.5. >>SET
- 2.6. >>SOURCE
- 2.7. >>TURN
- IDENTIFICATION DIVISION
- ENVIRONMENT DIVISION ii GNU COBOL 2.1 [23NOV2013] Programmer’s Guide
- 4.1. CONFIGURATION SECTION
- 4.1.1. SOURCE-COMPUTER
- 4.1.2. OBJECT-COMPUTER
- 4.1.3. REPOSITORY.
- 4.1.4. SPECIAL-NAMES
- 4.1.4.1. Alphabet-Name-Clause
- 4.1.4.2. Class-Definition-Clause
- 4.1.4.3. Switch-Definition-Clause.
- 4.1.4.4. Symbolic-Characters-Clause
- 4.2. INPUT-OUTPUT SECTION
- 4.2.1. SELECT
- 4.2.1.1. ORGANIZATION SEQUENTIAL
- 4.2.1.2. ORGANIZATION LINE SEQUENTIAL
- 4.2.1.3. ORGANIZATION RELATIVE
- 4.2.1.4. ORGANIZATION INDEXED
- 4.2.2. MULTIPLE FILE
- 4.2.3. SAME RECORD AREA
- DATA DIVISION
- 5.1. Data Definition Principles
- 5.2. FILE SECTION
- 5.2.1. File/Sort-Description
- 5.2.2. FILE-SECTION-Data-Item
- 5.3. WORKING-STORAGE SECTION
- 5.4. LOCAL-STORAGE SECTION
- 5.5. LINKAGE SECTION
- 5.6. REPORT SECTION
- 5.6.1. Report Group Definitions
- 5.6.2. REPORT SECTION Data Items
- 5.7. SCREEN SECTION
- 5.8. Special Data Items
- 5.8.1. 01-Level Constants
- 5.8.2. 66-Level Data Items.
- 5.8.3. 77-Level Data Items.
- 5.8.4. 78-Level Data Items.
- 5.8.5. 88-Level Data Items.
- 5.9. Data Description Clauses
- 5.9.1. ANY LENGTH
- 5.9.2. AUTO
- 5.9.3. AUTO-SKIP
- 5.9.4. AUTOTERMINATE
- 5.9.5. BACKGROUND-COLOR
- 5.9.6. BASED
- 5.9.7. BEEP
- 5.9.8. BELL.
- 5.9.9. BLANK
- 5.9.10. BLANK WHEN ZERO
- 5.9.11. BLINK
- 5.9.12. COLUMN
- 5.9.13. CONSTANT
- 5.9.14. EMPTY-CHECK
- Contents 3 June - 5.9.15. ERASE GNU COBOL 2.1 [23NOV2013] Programmer’s Guide iii - 5.9.16. EXTERNAL. - 5.9.17. FALSE - 5.9.18. FOREGROUND-COLOR - 5.9.19. FROM. - 5.9.20. FULL - 5.9.21. GLOBAL - 5.9.22. GROUP INDICATE - 5.9.23. HIGHLIGHT - 5.9.24. JUSTIFIED - 5.9.25. LEFTLINE - 5.9.26. LENGTH-CHECK - 5.9.27. LINE - 5.9.28. LOWLIGHT - 5.9.29. NEXT GROUP - 5.9.30. NO-ECHO - 5.9.31. OCCURS - 5.9.32. OVERLINE - 5.9.33. PICTURE - 5.9.34. PRESENT WHEN - 5.9.35. PROMPT - 5.9.36. REDEFINES - 5.9.37. RENAMES - 5.9.38. REQUIRED - 5.9.39. REVERSE-VIDEO - 5.9.40. SECURE - 5.9.41. SIGN IS - 5.9.42. SOURCE - 5.9.43. SUM OF - 5.9.44. SYNCRONIZED - 5.9.45. TO - 5.9.46. TYPE - 5.9.47. UNDERLINE - 5.9.48. USAGE. - 5.9.49. USING - 5.9.50. VALUE
- PROCEDURE DIVISION
- 6.1. PROCEDURE DIVISION USING
- 6.2. PROCEDURE DIVISION CHAINING
- 6.3. PROCEDURE DIVISION RETURNING.
- 6.4. PROCEDURE DIVISION Sections and Paragraphs
- 6.5. DECLARATIVES
- 6.6. Table References
- 6.7. Qualification of Data Names
- 6.8. Reference Modifiers
- 6.9. Arithmetic Expressions
- 6.10. Conditional Expressions
- 6.10.1. Condition Names
- 6.10.2. Class Conditions
- 6.10.3. Sign Conditions
- 6.10.4. Switch-Status Conditions
- 6.10.5. Relation Conditions
- 6.10.6. Combined Conditions iv GNU COBOL 2.1 [23NOV2013] Programmer’s Guide
- 6.10.7. Negated Conditions
- 6.11. Use of Periods
- 6.12. Use of VERB/END-VERB Constructs
- 6.13. Concurrent Access to Files
- 6.13.1. File Sharing
- 6.13.2. Record Locking
- 6.14. Common Clauses on Executable Statements
- 6.14.1. AT END + NOT AT END
- 6.14.2. CORRESPONDING
- 6.14.3. INVALID KEY + NOT INVALID KEY
- 6.14.4. ON EXCEPTION + NOT ON EXCEPTION.
- 6.14.5. ON OVERFLOW + NOT ON OVERFLOW
- 6.14.6. ON SIZE ERROR + NOT ON SIZE ERROR
- 6.14.7. ROUNDED
- 6.15. Special Registers
- 6.16. Intrinsic Functions
- 6.16.1. ABS
- 6.16.2. ACOS
- 6.16.3. ANNUITY.
- 6.16.4. ASIN
- 6.16.5. ATAN
- 6.16.6. BYTE-LENGTH
- 6.16.7. CHAR
- 6.16.8. COMBINED-DATETIME.
- 6.16.9. CONCATENATE
- 6.16.10. COS
- 6.16.11. CURRENCY-SYMBOL
- 6.16.12. CURRENT-DATE
- 6.16.13. DATE-OF-INTEGER
- 6.16.14. DATE-TO-YYYYMMDD
- 6.16.15. DAY-OF-INTEGER
- 6.16.16. DAY-TO-YYYYDDD
- 6.16.17. E
- 6.16.18. EXCEPTION-FILE.
- 6.16.19. EXCEPTION-LOCATION
- 6.16.20. EXCEPTION-STATEMENT.
- 6.16.21. EXCEPTION-STATUS
- 6.16.22. EXP.
- 6.16.23. EXP10
- 6.16.24. FACTORIAL
- 6.16.25. FRACTION-PART
- 6.16.26. HIGHEST-ALGEBRAIC
- 6.16.27. INTEGER
- 6.16.28. INTEGER-OF-DATE
- 6.16.29. INTEGER-OF-DAY
- 6.16.30. INTEGER-PART
- 6.16.31. LENGTH.
- 6.16.32. LENGTH-AN
- 6.16.33. LOCALE-COMPARE
- 6.16.34. LOCALE-DATE
- 6.16.35. LOCALE-TIME
- 6.16.36. LOCALE-TIME-FROM-SECONDS
- Contents 3 June
- 6.16.37. LOG GNU COBOL 2.1 [23NOV2013] Programmer’s Guide v
- 6.16.38. LOG10
- 6.16.39. LOWER-CASE
- 6.16.40. LOWEST-ALGEBRAIC
- 6.16.41. MAX
- 6.16.42. MEAN
- 6.16.43. MEDIAN
- 6.16.44. MIDRANGE
- 6.16.45. MIN
- 6.16.46. MOD
- 6.16.47. MODULE-CALLER-ID
- 6.16.48. MODULE-DATE
- 6.16.49. MODULE-FORMATTED-DATE
- 6.16.50. MODULE-ID
- 6.16.55. MODULE-PATH
- 6.16.52. MODULE-SOURCE
- 6.16.53. MODULE-TIME
- 6.16.54. MONETARY-DECIMAL-POINT
- 6.16.55. MONETARY-THOUSANDS-SEPARATOR
- 6.16.56. NUMERIC-DECIMAL-POINT
- 6.16.57. NUMERIC-THOUSANDS-SEPARATOR
- 6.16.58. NUMVAL
- 6.16.59. NUMVAL-C
- 6.16.60. NUMVAL-F
- 6.16.61. ORD
- 6.16.62. ORD-MAX
- 6.16.63. ORD-MIN
- 6.16.64. PI
- 6.16.65. PRESENT-VALUE
- 6.16.66. RANDOM
- 6.16.67. RANGE
- 6.16.68. REM
- 6.16.69. REVERSE
- 6.16.70. SECONDS-FROM-FORMATTED-TIME
- 6.16.71. SECONDS-PAST-MIDNIGHT
- 6.16.72. SIGN
- 6.16.73. SIN.
- 6.16.74. SQRT
- 6.16.75. STANDARD-DEVIATION
- 6.16.76. STORED-CHAR-LENGTH
- 6.16.77. SUBSTITUTE.
- 6.16.78. SUBSTITUTE-CASE
- 6.16.79. SUM
- 6.16.80. TAN
- 6.16.81. TEST-DATE-YYYYMMDD
- 6.16.82. TEST-DAY-YYYYDDD
- 6.16.83. TEST-NUMVAL.
- 6.16.84. TEST-NUMVAL-C
- 6.16.85. TEST-NUMVAL-F
- 6.16.86. TRIM
- 6.16.87. UPPER-CASE
- 6.16.88. VARIANCE
- 6.16.89. WHEN-COMPILED - 6.16.90. YEAR-TO-YYYY vi GNU COBOL 2.1 [23NOV2013] Programmer’s Guide
- 6.17. GNU COBOL Statements
- 6.17.1. ACCEPT
- 6.17.1.1. ACCEPT FROM CONSOLE
- 6.17.1.2. ACCEPT FROM COMMAND-LINE
- 6.17.1.3. ACCEPT FROM ENVIRONMENT
- 6.17.1.4. ACCEPT screen-data-item
- 6.17.1.5. ACCEPT FROM DATE/TIME
- 6.17.1.6. ACCEPT FROM Screen-Info
- 6.17.1.7. ACCEPT FROM Runtime-Info.
- 6.17.2. ADD
- 6.17.2.1. ADD TO
- 6.17.2.2. ADD GIVING
- 6.17.2.3. ADD CORRESPONDING
- 6.17.3. ALLOCATE
- 6.17.4. ALTER
- 6.17.5. CALL
- 6.17.6. CANCEL
- 6.17.7. CLOSE
- 6.17.8. COMMIT
- 6.17.9. COMPUTE.
- 6.17.10. CONTINUE
- 6.17.11. DELETE
- 6.17.12. DISPLAY
- 6.17.12.1. DISPLAY UPON device
- 6.17.12.2. DISPLAY UPON COMMAND-LINE.
- 6.17.12.3. DISPLAY UPON ENVIRONMENT-NAME
- 6.17.12.4. DISPLAY screen-data-item
- 6.17.13. DIVIDE
- 6.17.13.1. DIVIDE INTO.
- 6.17.13.2. DIVIDE INTO GIVING
- 6.17.13.3. DIVIDE BY GIVING
- 6.17.14. ENTRY
- 6.17.15. EVALUATE
- 6.17.16. EXIT
- 6.17.17. FREE
- 6.17.18. GENERATE
- 6.17.19. GOBACK
- 6.17.20. GO TO
- 6.17.20.1. Simple GO TO
- 6.17.20.2. GO TO DEPENDING ON
- 6.17.21. IF
- 6.17.22. INITIALIZE
- 6.17.23. INITIATE
- 6.17.24. INSPECT
- 6.17.25. MERGE
- 6.17.26. MOVE
- 6.17.26.1. Simple MOVE
- 6.17.26.2. MOVE CORRESPONDING
- 6.17.27. MULTIPLY
- 6.17.27.1. MULTIPLY BY.
- 6.17.27.2. MULTIPLY GIVING
- 6.17.28. OPEN
- Contents 3 June - 6.17.29. PERFORM GNU COBOL 2.1 [23NOV2013] Programmer’s Guide vii - 6.17.29.1. Procedural PERFORM - 6.17.29.2. Inline PERFORM. - 6.17.29.3. VARYING - 6.17.30. READ - 6.17.30.1. Sequential READ - 6.17.30.2. Random READ - 6.17.31. READY TRACE - 6.17.32. RELEASE - 6.17.33. RESET TRACE - 6.17.34. RETURN - 6.17.35. REWRITE - 6.17.36. ROLLBACK. - 6.17.37. SEARCH - 6.17.38. SEARCH ALL - 6.17.39. SET - 6.17.39.1. SET ENVIRONMENT - 6.17.39.2. SET Program-Pointer - 6.17.39.3. SET ADDRESS - 6.17.39.4. SET Index - 6.17.39.5. SET UP/DOWN. - 6.17.39.6. SET Condition Name - 6.17.39.7. SET Switch - 6.17.39.8. SET ATTRIBUTE - 6.17.40. SORT - 6.17.40.1. File-Based SORT - 6.17.40.2. Table SORT - 6.17.41. START - 6.17.42. STOP - 6.17.43. STRING - 6.17.44. SUBTRACT. - 6.17.44.1. SUBTRACT FROM - 6.17.44.2. SUBTRACT GIVING - 6.17.44.3. SUBTRACT CORRESPONDING. - 6.17.45. SUPPRESS. - 6.17.46. TERMINATE - 6.17.47. TRANSFORM - 6.17.48. UNLOCK - 6.17.49. UNSTRING - 6.17.50. WRITE
- Report Writer Usage Notes
- 7.1. RWCS Lexicon
- 7.2. The Anatomy of a Report
- 7.3. The Anatomy of a Report Page
- 7.4. How RWCS Builds Report Pages
- 7.5. Control Hierarchy
- 7.6. An Example
- 7.6.1. Data
- 7.6.2. Program
- 7.6.3. Generated Report Pages
- 7.7. Control Hierarchy (Revisited)
- 7.8. Turning PHYSICAL Page Formatting Into LOGICAL Formatting
- Interfacing With The OS. viii GNU COBOL 2.1 [23NOV2013] Programmer’s Guide
- 8.1. Compiling Programs
- 8.1.1. cobc - The GNU COBOL Compiler
- 8.1.2. Compilation Time Environment Variables
- 8.1.3. Locating Copybooks
- 8.1.4. Compiler Configuration Files
- 8.2. Running Programs
- 8.2.1. Direct Execution
- 8.2.2. Executing Dynamically-Loadable Libraries
- 8.2.2.1. cobcrun - Command-line Execution
- 8.2.2.2. Dynamically Loaded Subprograms
- 8.2.3. Run Time Environment Variables
- 8.2.4. Program Arguments
- 8.3. Built-In System Subroutines
- 8.3.1. C$CALLEDBY
- 8.3.2. C$CHDIR
- 8.3.3. C$COPY
- 8.3.4. C$DELETE
- 8.3.5. C$FILEINFO
- 8.3.6. C$GETPID
- 8.3.7. C$JUSTIFY
- 8.3.8. C$MAKEDIR.
- 8.3.9. C$NARG
- 8.3.10. C$PARAMSIZE
- 8.3.11. C$PRINTABLE
- 8.3.12. C$SLEEP
- 8.3.13. C$TOLOWER
- 8.3.14. C$TOUPPER
- 8.3.15. CBL AND
- 8.3.16. CBL CHANGE DIR
- 8.3.17. CBL CHECK FILE EXIST
- 8.3.18. CBL CLOSE FILE
- 8.3.19. CBL COPY FILE
- 8.3.20. CBL CREATE DIR
- 8.3.21. CBL CREATE FILE
- 8.3.22. CBL DELETE DIR
- 8.3.23. CBL DELETE FILE
- 8.3.24. CBL EQ
- 8.3.25. CBL ERROR PROC
- 8.3.26. CBL EXIT PROC
- 8.3.27. CBL FLUSH FILE
- 8.3.28. CBL GET CSR POS
- 8.3.29. CBL GET CURRENT DIR
- 8.3.30. CBL GET SCR SIZE
- 8.3.31. CBL IMP
- 8.3.32. CBL NIMP
- 8.3.33. CBL NOR
- 8.3.34. CBL NOT
- 8.3.35. CBL OC NANOSLEEP
- 8.3.36. CBL OPEN FILE
- 8.3.37. CBL OR
- 8.3.38. CBL READ FILE
- 8.3.39. CBL RENAME FILE
- Contents 3 June - 8.3.40. CBL TOLOWER GNU COBOL 2.1 [23NOV2013] Programmer’s Guide ix - 8.3.41. CBL TOUPPER - 8.3.42. CBL WRITE FILE - 8.3.43. CBL XOR - 8.3.44. SYSTEM - 8.3.45. X" 91 " - 8.3.46. X"E4" - 8.3.47. X"E5" - 8.3.48. X"F4" - 8.3.49. X"F5"
- Sub-Programming
- 9.1. Subprogram Types
- 9.2. Independent vs Contained vs Nested Subprograms
- 9.3. Alternate Entry Points
- 9.4. Dynamic vs Static Subprograms.
- 9.5. Subprogram Execution Flow
- 9.5.1. Subroutine Execution Flow
- 9.5.2. User-Defined Function Execution Flow
- 9.6. Sharing Data Between Calling and Called Programs
- 9.5.1. Subprogram Arguments
- 9.6.1.1. Calling Program Considerations
- 9.6.1.2. Called Program Considerations
- 9.6.2. GLOBAL Data Items
- 9.6.3. EXTERNAL Data Items
- 9.7. Recursive Subprograms
- 9.8. Combining GNU COBOL and C Programs
- 9.9.1. GNU COBOL Run-Time Library Requirements
- 9.9.2. String Allocation Differences Between GNU COBOL and C
- 9.9.3. Matching C Data Types with GNU COBOL USAGEs
- 9.9.4. GNU COBOL Main Programs CALLing C Subprograms
- 9.9.5. C Main Programs Calling GNU COBOL Subprograms
- Programming Style Suggestions
- 10.1. Marking Changes in Programs
- 10.2. Data Item Coding and Naming Conventions
- 10.3. Table Subscripting versus Table Indexing.
- 10.4. Copybook Naming Conventions and Usage
- 10.5. PROCEDURE DIVISION Sections Versus Paragraphs.
- 10.6. COMPUTE Versus ADD-SUBTRACT-MULTIPLY-DIVIDE
- Appendix A - Glossary of Terms
- Appendix B - Reserved Word List
- Appendix C - GNU Free Documentation License
- Appendix D - Summary of Document Changes
- Index.
GNU COBOL 2.1 [23NOV2013] Programmer’s Guide 1
1. Introduction
This document describes the syntax, semantics and usage of the COBOL programming language as implemented by the current version of GNU COBOL, formerly known as OpenCOBOL.
The principal developers of GNU COBOL are Keisuke Nishida and Roger While. They may be contacted through the GNU COBOL website – sourceforge.net/projects/open-cobol.
This document was intended to serve as a full-function reference and user’s guide suitable for both those readers learning COBOL for the first time as well as those already familiar with some dialect of the COBOL language.
1.1. Additional Reference Sources
For those wishing to learn COBOL for the first time, I can strongly recommend the following resources.
If you like to hold a book in your hands, I strongly recommend "Murach’s Structured COBOL", by Mike Murach, Anne Prince and Raul Menendez (2000) - ISBN 9781890774059. Mike Murach and his various writing partners have been writing outstanding COBOL textbooks for decades, and this text is no exception. It’s an excellent book for those familiar with the concepts of programming in other languages, but unfamiliar with COBOL.
Would you prefer a web-based tutorial? Try the University of Limerick (Ireland) COBOL web site - ‘http://www.csis.ul.ie/cobol/’.
1.2. Introducing COBOL
If you already know a programming language, and that language isn’t COBOL, chances are that language is Java, C or C++. You will find COBOL a much different programming language than those; sometimes those differences are a good thing and sometimes they aren’t. The thing to remember about COBOL is this — it was designed to solve business problems.
COBOL, first introduced to the programming public in 1959, was the very first programming language to become standardized (in 1960). This meant that a standard-compliant COBOL program written on computer "A" made by company "B" would be able to be compiled and executed on computer "X" made by company "Y" with very few, if any, changes. This may not seem like such a big deal today, but it was a radical departure from all programming languages that came before it and even many that came after it.
The name COBOL actually says it all — COBOL is an acronym that stands for "(CO)mmon (B)usiness (O)riented (L)anguage". Note the fact that the word "common" comes before all others. The word "business" is a close second. Therein lies the key to COBOL’s success.
1.2.1. Why YOU Should Learn COBOL
Despite statements from industry "insiders", the COBOL programming language is not dead, even though newer and so-called "modern" languages like Java, C#, .NET, Ruby on Rails and so on appear to have become the languages of choice in the Information Technology world. These languages have become popular because they address the following desired requirements for "modern" programming:
- They conform to the principles of Object-Oriented Programming (OOP). This is desired for one major reason — it facilitates "code reusability", thus improving the productivity
3 June 2014 Chapter 1 - Introduction
2 GNU COBOL 2.1 [23NOV2013] Programmer’s Guide
of programmers by allowing them to re-use previously written (and debugged) code in new applications. For one reason or another, COBOL is perceived as being weak in this regard. It isn’t (especially today), as we’ll see in the next section, but perception is important.
- Those languages aren’t limited to mainframe computers, as COBOL is perceived to be. Some, like .NET and Ruby, aren’t even available on mainframes. The "modern" program- ming languages were designed and intended for use on the full variety of computer platforms, from shirt-pocket computers (i.e. smartphones) up to the most massive of supercomputers.
There are several excellent commercially available COBOL implementations available for non-mainframe systems (Micro Focus COBOL, AccuCOBOL, NetCOBOL and Elastic COBOL, just to name a few), including Windows and UNIX/Linux systems. These aren’t cheap, however.
- Universities love the "Modern" languages. In the U.S., 73% of colleges lack even one COBOL course on their curricula. COBOL, it appears, is no longer "cool" enough for students to fill a classroom.
Just because COBOL doesn’t traditionally support objects, classes, and the like doesn’t mean that its "procedural" approach to computing isn’t valuable — after all, it runs 70% of the worlds business transactions, and does so:
- Using programs that, for the most part, are much more self-documenting than would be the case with any other programming language.
- Effortlessly providing arithmetic accuracy to 31 digits, with performance approaching that of well-written assembly-language programs. Don’t think this isn’t critically important to banks, investment houses and any business interested in tracking revenues, expenses and profits (duh - like ALL of them).
- Integrating well with non-COBOL infrastructures such as XML, SOA, MQ, almost any DBMS, Transaction Processing platforms, Queue-Management facilities and other program- ming languages.
- By running on almost as many different computing platforms as Java can. You can’t run COBOL programs in your smartphone, but desktops, workstations, midframes/servers, mainframes and supercomputers are all fair game.
Today’s IT managers and business leaders are faced with a challenging dilemma — how do you maintain the enourmous COBOL codebase that is still running their businesses when academia has all but abandoned the language they need their people to use to keep the wheels rolling? The problem is compounded by the fact that those programmers that are skilled in COBOL are retiring and taking their knowledge with them. In some markets, this appears to be having an inflationary effect on the cost of resources (COBOL programmers) whose supply is becoming smaller and smaller. The pressure to update applications to make use of more up-to-date graphical user interfaces is also perceived as a reason to abandon COBOL in favor of GUI- friendly languages such as Java.
Businesses are addressing the COBOL challenge in different ways:
- By undertaking so-called "modernization projects", where existing applications are either rewritten in "modern" languages or replaced outright with purchased packages. Most of these businesses are using such activities as an excuse to abandon "expensive" mainframes in favor of (presumably) less-expensive "open systems" (midframe/server) solutions.
Chapter 1 - Introduction 3 June 2014
4 GNU COBOL 2.1 [23NOV2013] Programmer’s Guide
Sometimes the quest for improved programmer productivity (and therefore reduced program- ming expense) has taken the form of introducing new features in programming languages, or even new languages altogether. Sometimes it has resulted in new ways of using the existing languages.
While many technological and procedural developments have made evolutionary improvements to programmer productivity, each of the following three events has been responsible for revolu- tionary improvements:
- The development of so-called "higher-level" programming languages that enable a program- mer to specify in a single statement of the language an action that would have required many more separate statements in a prior programming language. The standardization of such languages, making them usable on a wide variety of computers and operating systems, was a key aspect of this development. COBOL was a pioneering development in this area, being a direct descendant of the very first higher-level language (FLOW-MATIC, developed by US Naval Lieutenant Grace Hopper) and the first to become standardized.
- The establishment of programming techniques that make programs easier to read and there- fore easier to understand. Not only do such techniques reduce the amount of rework nec- essary simply to make a program work as designed, but they also reduce the amount of time a programmer needs to study an existing program in order how to best adapt it to changing business requirements. The foremost development in this area was structured programming. Introduced in the late 1970s, this approach to programming spawned new programming languages (PASCAL, ALGOL, PL/1 and so forth) designed around it. With the ANSI85 standard, COBOL embraced the principles espoused by structured programming mavens as well as any of the languages designed strictly around it.
- The establishment of programming techniques AND the introduction of programming lan- guage capabilities to facilitate the reusability of program code. Anything that supports code reusability can have a profound impact to the amount of time it takes to develop new applications or to make significant changes to existing ones. In recent years, object-oriented programming (OOP) has been the industry "poster child" for code reusability. By enabling program logic and the data structures that logic manipulates to be encapsulated into easily stored and retrieved (and therefore "reusable") modules called classes, the object-oriented languages such as Java, C++ and C# have become the favorites of academia. Since students are being trained in these languages and only these, by and large, it’s no surprise that — today — object-oriented programming languages are the darlings of the industry.
The reality is, however, that good programmers have been practicing code reusability for more than a half-century. Up until recently, COBOL programmers have had some of the best code reusability tools available — they’ve been doing it with copybooks and subprograms rather than classes, methods and attributes but the net results have been similar. With the COBOL2002 standard and proposed COBOL20xx standard, the COBOL programming language has become just as "object-oriented" as the "modern" languages, while preserving the ability to support, modify, compile and execute "legacy" COBOL programs as well.
While GNU COBOL supports few of the OOP programming constructs defined by the COBOL2002 and COBOL20xx standards, it supports every aspect of the ANSI85 standard and therefore fully meets the needs of points #1 and #2, above. With it’s supported feature set (see [So What is GNU COBOL?], page 5), it provides significant programmer productivity capabilities.
Chapter 1 - Introduction 3 June 2014
GNU COBOL 2.1 [23NOV2013] Programmer’s Guide 5
1.3. So What is GNU COBOL?
GNU COBOL is an open source COBOL compiler and runtime environment, written using the C programming language. GNU COBOL is typically distributed in source-code form, and must then be built for your computer’s operating system using the system’s C compiler and loader. While originally developed for the UNIX and Linux operating systems, GNU COBOL has also been successfully built for OSX computers or Windows computers utilizing the UNIX-emulation features of such tools as Cygwin and MinGW.
The MinGW approach is a personal favorite with the author of this manual because it creates a GNU COBOL compiler and runtime library that require only a single MinGW DLL to be available for the GNU COBOL compiler, runtime library and user programs. That DLL is freely distributable under the terms of the GNU General Public License. A MinGW build of GNU COBOL fits easily on and runs from a 128MB flash drive with no need to install any software onto the Windows computer that will be using it. Some functionality of the language, dealing with the sharing of files between concurrently executing GNU COBOL programs and record locking on certain types of files, is sacrificed however as the underlying operating system routines needed to implement them aren’t available to Windows and aren’t provided by MinGW.
GNU COBOL has also been built as a truly native Windows application utilizing Microsoft’s freely-downloadable Visual Studio Express package to provide the C compiler and linker/loader. This approach does not lend itself well to a "portable" distribution.
The GNU COBOL compiler generates C code from your COBOL programs; that C code is then automatically compiled and linked using your system’s C compiler (typically, but not limited to, "gcc").
GNU COBOL fully supports much of the ANSI85 standard for COBOL (the only major exclusion is the Communications Module) and also supports some of the components of the COBOL standard, such as the "SCREEN SECTION" (see [SCREEN SECTION], page 104), table-based "SORT" (see [Table SORT], page 435) and user-defined functions.
1.3.1. Language Reserved Words
COBOL programs consist of a sequence of words and symbols. Words, which consist of sequences of letters (upper- and/or lower-case), digits, dashes ("-") and/or underscores (" ") may have a pre-defined, specific, meaning to the compiler or may be invented by the programmer for his/her purposes.
The GNU COBOL language specification defines over 700 ’Reserved Words’ — words to which the compiler assigns a special meaning.
Programmers may use a reserved word as part of a word they are creating themselves, but may not create their own word as an exact duplicate (without regard to case) of a COBOL reserved word.
See [Appendix B - Reserved Word List], page 559, for a complete list of GNU COBOL reserved words.
1.3.2. User-Defined Words
When you write GNU COBOL programs, you’ll need to create a variety of words to represent various aspects of the program, the program’s data and the external environment in which the program will run. This will include internal names by which data files will be referenced, data item names and names of executable logic procedures.
3 June 2014 Chapter 1 - Introduction
GNU COBOL 2.1 [23NOV2013] Programmer’s Guide 7
Let’s look at how much more "wordy" COBOL is than Java. Count the characters in the two programs. The Java program has 95 (not counting carriage returns and any indentation). The COBOL program has 89 (again, not counting carriage returns and indentation)! Technically, it could have been only 65 because the "IDENTIFICATION DIVISION." header is actually optional. Clearly, "Hello World" doesn’t look any more concise in Java than it does in COBOL.
Let’s look at a different problem. Surely a program that asks a user to input a positive integer, generates the sum of all positive integers from 1 to that number and then prints the result will be MUCH shorter and MUCH easier to understand when coded in Java than in COBOL, right?
You can be the judge. First, the Java version:
import java.util.Scanner; public class sumofintegers { public static void main(String[] arg) { System.out.println("Enter a positive integer"); Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int sum=0; for (int i=1;i<=n;i++) { sum+=i; } System.out.println("The sum is "+sum); } }
And now for the COBOL version:
IDENTIFICATION DIVISION. PROGRAM-ID. SumOfIntegers. DATA DIVISION. WORKING-STORAGE SECTION. 01 n BINARY-LONG. 01 i BINARY-LONG. 01 sum BINARY-LONG VALUE 0. PROCEDURE DIVISION. DISPLAY "Enter a positive integer" ACCEPT n PERFORM VARYING i FROM 1 BY 1 UNTIL i > n ADD i TO sum END-PERFORM DISPLAY "The sum is " sum.
My familiarity with COBOL may be prejudicing my opinion, but it doesn’t appear to me that the Java code is any simpler than the COBOL code. In case you’re interested in character counts, the Java code comes in at 278 (not counting indentation characters). The COBOL code is 298 (274 without the "IDENTIFICATION DIVISION." header).
Despite what you’ve seen here, the more complex the programming logic being implemented, the more concise the Java code will appear to be, even compared to 2002-standard COBOL. That conciseness comes with a price though — program code readability. Java (or C or C++ or C#) programs are generally intelligible only to trained programmers. COBOL programs can, however, be quite understandable by non-programmers. This is actually a side-effect of the
3 June 2014 Chapter 1 - Introduction
8 GNU COBOL 2.1 [23NOV2013] Programmer’s Guide
"wordiness" of the language, where COBOL statements use natural English words to describe their actions. This inherent readability has come in handy many times throughout my career when I’ve had to learn obscure business (or legal) processes by reading the COBOL program code that supports them.
The "modern" languages, like Java, also have their own "boilerplate" infrastructure overhead that must be coded in order to write the logic that is necessary in the program. Take for ex- ample the "public static void main(String[] arg)" and "import java.util.Scanner;" statements. The critics tend to forget about this when they criticize COBOL for it’s structural "overhead".
When it first was developed, COBOL’s easily-readable syntax made it profoundly different from anything that had been seen before. For the first time, it was possible to specify logic in a manner that was — at least to some extent — comprehensible even to non-programmers. Take for example, the following code written in FORTRAN — a language developed only a year before COBOL:
EXT = PRICE * IQTY INVTOT = INVTOT + EXT
With its original limitation on the length of variable names (one- to six-character names com- prised of a letter followed by up to five letters and/or digits), it’s implicit rule that variable were automatically created as real (floating-point) unless their name started with a letter in the range I-N, and its use of algebraic notation to express actions being taken, FORTRAN wasn’t a par- ticularly readable language, even for programmers. Compare this with the equivalent COBOL code:
MULTIPLY price BY quantity GIVING extended-amount ADD extended-amount TO invoice-total
Clearly, even a non-programmer could at least conceptually understand what was going on! Over time, languages like FORTRAN evolved more robust variable names, and COBOL introduced a more formula-based syntactical capability for arithmetic operations, but FORTRAN was never as readable as COBOL.
Because of its inherent readability, I would MUCH rather be handed an assignment to make significant changes to a COBOL program about which I know nothing than to be asked to do the same with a C, C++, C# or Java program.
Those that argue that it is too boring / wasteful / time-consuming / insulting (pick one) to have to code a COBOL program "from scratch" are clearly ignorant of the following facts:
- Many systems have program-development tools available to ease the task of coding pro- grams; those tools that concentrate on COBOL are capable of providing templates for much of the "overhead" verbiage of any program...
- Good programmers have — for decades — maintained their own skeleton "template" pro- grams for a variety of program types; simply load a template into a text editor and you’ve got a good start to the program...
- Legend has it that there’s actually only been ONE program ever written in COBOL, and all programs ever "written" thereafter were simply derivatives of that one. Although this is clearly intended as a (probably) bad joke, it is nevertheless close to the very simple truth that many programers"reuse" existing COBOL programs when creating new ones. There’s
Chapter 1 - Introduction 3 June 2014