COBOL Programming Course 1 Getting Started
COBOL Programming Course 1 Getting Started
1
Preface
Abstract
One computer programming language was designed specifically for business, Common Business-Oriented
Language, COBOL. Today COBOL remains as relevant as ever, handling $3 trillion in commerce every day.
This publication is aimed at beginners looking to build a working understanding of COBOL programming.
It describes how to work with COBOL using modern tools including Visual Studio Code with Zowe and Z
Open Editor extensions. It describes how to write, test, execute, and debug COBOL programs.
Authors
Michael Bauer is a development leader for the Open Mainframe value stream at Broadcom and is a squad
lead for the Zowe open source initiative. Zowe, a popular framework of modern interfaces for z/OS, opens
the mainframe to DevOps tools and practices. Mike leads the Command Line Interface (CLI) squad, which
created and recently spun-off the successful Zowe Explorer extension for Visual Studio Code. A frequent
speaker and blogger, Mike runs interactive workshops around the world for those interested in incorporating
mainframe in their enterprise DevOps initiatives.
Zeibura Kathau is a technical writer for the Mainframe DevOps value stream at Broadcom. He works on
the open-source projects Che4z and Code4z, which are IDE extension packages for mainframe developers. He
has 8 years of experience in the Information Technology field.
Makenzie Manna is an IBM Redbooks Project Leader in the United States. She has 3 years of experience
in the Computer Science Software Development field. She holds a Master’s degree in Computer Science
Software Development from Marist College. Her areas of expertise include mathematics, IBM Z and cloud
computing.
Paul Newton is a Consulting IT Specialist in the United States. He has 40 years of experience in the
Information Technology field. He holds a degree in Information Systems from the University of Arizona. His
areas of expertise include IBM Z, z/OS, and LinuxONE. He has written extensively on implementation of
z/OS based technology.
Jonathan Sayles is a technical educator at IBM, where he conducts presentations, seminars and training
courses, as well as producing educational materials. His more than 40 years in the IT education and computer
industries encompass work within both academic and corporate development organizations. He has also
been engaged as a software developer/designer/consultant, educator, and author, with a focus on relational
database, IDE, and object technologies. In addition to authoring/publishing 16 books, Jon has written and
published more than 150 articles in technical journals, and served as technical editor for several IT magazines.
He is also co-author of IBM Redbook publications Transitioning: Informix 4GL to Enterprise Generation
Language (EGL), SG24-6673 and z/OS Traditional Application Maintenance and Support, SG24-7868.
William Yates is a Software engineer working for IBM UK. For the majority of his career he has working
on the CICS TS product mainly as a software tester and now as Test Architect. He has delivered technical
content for many Redbooks, video courses and at conferences around the world. He is also one of the leaders
of the Galasa project, building an open source integration test framework for hybrid cloud applications
available at https://galasa.dev
Acknowledgements
Special thanks to the following people for participating in the residency to shape the content in this publication.
• Dr. Tak Auyeung, Professor, American River College
• Jeffrey Bisti, Z Ecosystem Architect, IBM
• Ilicena Elliott, IT Specialist II, Employment Development Department
• Martin Keen, Technical Content Services, IBM
2
• Sudharsana Srinivasan, z Influencer Ecosystem Program Coordinator, IBM
• Suzy Wong, Information Technology Specialist, DMV
•
Left-to-right: Ilicena, Suzy, Makenzie, Martin, Paul, and Tak
3
Contents
Part 1 - Getting started 9
1 Why COBOL? 9
1.1 What is COBOL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 How is COBOL being used today? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 Why should I care about COBOL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4
5.1 What is a CLI and why would you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.2 What is Zowe CLI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.3 Zowe CLI interactive use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.3.1 Installing Zowe CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.3.2 Interactive Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.3.3 Zowe Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.3.4 Interacting with z/OS Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.3.5 Interacting with z/OS Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.4 Automating tasks using Zowe CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.4.1 Automated Job Submission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.4.2 Using Other Programming Languages and Continuous Integration . . . . . . . . . . . 36
5.4.3 Additional Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.5 The world of modern open source tooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8 Basic COBOL 46
8.1 COBOL characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.1.1 Enterprise COBOL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.1.2 Chapter objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.2 What must a novice COBOL programmer know to be an experienced COBOL programmer? 47
8.2.1 What are the coding rules and the reference format? . . . . . . . . . . . . . . . . . . . 47
8.2.2 What is the structure of COBOL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.2.3 What are COBOL reserved words? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.2.4 What is a COBOL statement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.2.5 What is the meaning of a scope terminator? . . . . . . . . . . . . . . . . . . . . . . . . 48
8.2.6 What is a COBOL sentence? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.2.7 What is a COBOL paragraph? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.2.8 What is a COBOL section? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.3 COBOL Divisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.3.1 COBOL Divisions structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.3.2 What are the four Divisions of COBOL? . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.4 PROCEDURE DIVISION explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5
8.5 Additional information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.5.1 Professional manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.5.2 Learn more about recent COBOL advancements . . . . . . . . . . . . . . . . . . . . . 51
8.6 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.7 Lab - Zowe CLI & Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.7.1 Zowe CLI - Interactive Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.7.2 Zowe CLI - Programmatic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
9 Data division 67
9.1 Variables / Data-items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
9.1.1 Variable / Data-item name restrictions and data types . . . . . . . . . . . . . . . . . . 67
9.2 PICTURE clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.2.1 PIC clause symbols and data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.2.2 Coding COBOL variable / data-item names . . . . . . . . . . . . . . . . . . . . . . . . 68
9.2.3 PICTURE clause character-string representation . . . . . . . . . . . . . . . . . . . . . 68
9.3 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.3.1 Figurative constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.3.2 Data relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.3.3 Levels of data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
9.4 MOVE and COMPUTE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
9.5 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
10 File handling 74
10.1 COBOL code used for sequential file handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.1.1 COBOL inputs and outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
10.1.2 FILE-CONTROL paragraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
10.1.3 COBOL external data source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.1.4 Data sets, records, and fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.1.5 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.1.6 ASSIGN clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
10.2 PROCEDURE DIVISION sequential file handling . . . . . . . . . . . . . . . . . . . . . . . . 77
10.2.1 Open input and output for read and write . . . . . . . . . . . . . . . . . . . . . . . . . 77
10.2.2 Close input and output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
10.3 COBOL programming techniques to read and write records sequentially . . . . . . . . . . . . 78
10.3.1 READ-NEXT-RECORD paragraph execution . . . . . . . . . . . . . . . . . . . . . . . 79
10.3.2 READ-RECORD paragraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
10.3.3 WRITE-RECORD paragraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
10.3.4 Iterative processing of READ-NEXT-RECORD paragraph . . . . . . . . . . . . . . . 80
10.4 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11 Program structure 84
11.1 Styles of programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.1.1 What is structured programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.1.2 What is Object Orientated Programming . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.1.3 COBOL programming style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.2 Structure of the Procedure Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.2.1 Program control and flow through a basic program . . . . . . . . . . . . . . . . . . . . 85
11.2.2 Inline and out of line perform statements . . . . . . . . . . . . . . . . . . . . . . . . . 86
11.2.3 Using performs to code a loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
11.2.4 Learning bad behavior using the GO TO keyword . . . . . . . . . . . . . . . . . . . . 87
11.3 Paragraphs as blocks of code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
11.3.1 Designing the content of a paragraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.3.2 Order and naming of paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.4 Program control with paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6
11.4.1 PERFORM TIMES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
11.4.2 PERFORM THROUGH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
11.4.3 PERFORM UNTIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
11.4.4 PERFORM VARYING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
11.5 Using subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
11.5.1 Specifying the target program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
11.5.2 Specifying program variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
11.5.3 Specifying the return value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
11.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
11.7 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
12 File output 95
12.1 Review of COBOL write output process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
12.1.1 ENVIRONMENT DIVISION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
12.2 FILE DESCRIPTOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
12.2.1 FILLER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
12.3 Report and column headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
12.3.1 HEADER-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
12.4 PROCEDURE DIVISION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
12.4.1 MOVE sentences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
12.4.2 PRINT-REC FROM sentences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
12.5 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7
15 Data types 115
15.1 Data representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
15.1.1 Numerical value representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
15.1.2 Text representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
15.2 COBOL DISPLAY vs COMPUTATIONAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
15.3 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
8
Part 1 - Getting started
1 Why COBOL?
This chapter introduces COBOL, specifically with reference to its use in enterprise systems.
• What is COBOL?
• How is COBOL being used today?
• Why should I care about COBOL?
9
The point is - whatever you read or hear about COBOL, be very skeptical. If you have the opportunity to
work directly with someone involved in writing or maintaining critical business logic using COBOL, you will
learn about the operation of the core business. Business managers, business analysts, and decision makers
come and go. The sum of all good business decisions can frequently be found in the decades of changes
implemented in COBOL programs. The answer to “How does this business actually work?” can be found in
COBOL programs.
Add the following to your awareness of COBOL. It is an absolute myth that you must be at least 50 years
old to be good with COBOL. COBOL is incredibly easy to learn and understand. One of the many reasons
financial institutions like COBOL, is the fact that it is not necessary to be a programmer to read and
understand the logic. This is important because critical business logic code is subject to audit. Auditors
are not programmers. However, auditors are responsible for ensuring the business financial statements are
presented fairly. It is COBOL processing that frequently result in the business ledger updates and subsequent
financial statements.
Now for a real-world lesson. A comment recently made in a well-known business journal by someone with a
suspect agenda was quoted as saying, “COBOL is a computing language used in business and finance. It was
first designed in 1959 and is pretty old and slow.” A highly experienced business technology person knows
the only true part of that last sentence was that COBOL was first designed in 1959.
It’s no secret that lots of banks still run millions of lines of COBOL on mainframes. They probably want to
replace that at some point. So why haven’t they? Most banks have been around long enough to still feel the
pain from the ~1960’s software crisis. After spending enormous amounts of money, and time, on developing
their computer systems, they finally ended up with a fully functional, well-tested, stable COBOL core system.
Speaking with people that have worked on such systems, nowadays they have Java front ends and wrappers
which add functionality or more modern interfaces, they run the application on virtualized replicated servers,
but in the end, everything runs through that single core logic. And that core logic is rarely touched or
changed, unless necessary.
From a software engineering perspective, that even makes sense. Rewrites are always more expensive than
planned, and always take longer than planned (OK, probably not always. But often.). Never change a
running system etc., unless very good technical and business reasons exist.
10
2 VSCode with Zowe Explorer
Zowe Explorer is an open-source extension for VS Code that lets developers and system administrators
interact with z/OS mainframes.
• Introduction to Zowe Explorer
• Using Zowe Explorer
• Profiles in Zowe Explorer
– Secure Credentials
– Creating a New Profile
– Editing Profiles
– Deleting Profiles
• Summary
11
• View multiple jobs simultaneously
• Download spool content
• Interact with jobs from multiple systems simultaneously
For more information about Zowe Explorer and the different use cases, visit the marketplace
3. Type Zowe-Plugin in the text box. This will trigger the Built-in Secure Credential Store.
Alternatively, to enable this feature by editing settings.json, hover over the gear icon and click “Copy Setting
as JSON”. You can then paste that to settings.json and update the value to Zowe Plugin.
Note: If you are using Zowe CLI and you’ve installed the Secure-Credential-Store Plugin, the steps to activate
it will still be the same.
12
2. Click on the + sign. A dialog box will appear and ask if you want to “Create a New Connection to
z/OS”.
3. Press enter or click on that selection.
5. Enter the URL and Port that you received by email when you registered for the COBOL Course. The
connection information that you need has a title of “IP address for VSCode extension”.
Note: You can leave this blank if you do not want to save your credentials in your machine. You will be
prompted for your username once you start using Zowe Explorer.
13
7. Enter your Password.
Note: You can leave this blank if you do not want to save your credentials in your machine. You will be
prompted for your username once you start using Zowe Explorer.
8. Select True/False if you want to accept or reject Self-Signed Certificates. For this course, please select
false.
14
3. Edit the URL information if changes are required, or enter to confirm the information is still correct.
Note: You can leave this blank if you do not want to save your credentials in your machine. You will be
prompted for your username once you start using Zowe Explorer.
5. Edit your Password.
Note: You can leave this blank if you do not want to save your credentials in your machine. You will be
prompted for your username once you start using Zowe Explorer.
6. Edit your authorized connections
15
3. Select the Profile that you want to delete.
16
2. Confirm that you want to delete your profile.
2.4 Summary
In this section you have learned the basic features of the Zowe Explorer extension and how to create and
work with Zowe compatible zosmf profiles.
17
3 VSCode with Z Open Editor
In this chapter we will explain how to use the IBM Z Open Editor extension for VSCode and how using it
can help you develop COBOL source code in a feature rich environment.
• Introduction to the IBM Z Open Editor
– What is the IBM Z Open Editor?
– The role of the Language Server Protocol
– Installing the IBM Z Open Editor for VS Code
• Basic editing
– Known file extensions
– Margins
– Variable expansion
– Syntax highlighting
• Navigation of code
– Outline view
– Breadcrumb view
– Jump to declaration / reference
• Code-completion
– COBOL reserved word completion
– Variable completion
– CICS, MQ, DB2 API completion
• Refactoring code
– Renaming variables
– Handling errors
• Summary
18
The language server protocol defines six broad capabilities that should be implemented for a language server
to be LSP compliant. These capabilities include code completion, hover, jump to definition, workspace
symbols, find references and diagnostics. The IBM Z Open Editor provides compliant language servers for
both the Enterprise Cobol and Enterprise PL/I for z/OS languages. In addition to being compliant, they
also provide additional capabilities that we will discuss further on.
Note: More information on Language Server Protocol implementations can be found at: https://langserver.org
Will be assumed to be COBOL code. This information is stored in the global VSCode settings.json file that
can be accessed through VSCode preferences. This allows for a user to tailor VSCode’s known extensions to
a particular site naming convention.
3.2.2 Margins
The first thing you will notice when editing COBOL source code is that VSCode will have inserted five
vertical lines down the file. These lines segment each line of code into the areas reserved for sequence numbers,
comment / continuation characters, area A and area B. When coding without this basic aid I cannot recount
the number of times I have made a compilation error because I started coding in the wrong column. This
19
alone is a very useful aid to a new COBOL programmer. Move information about COBOL syntax, and in
particular the columns, will be discussed later
20
3.2.4 Syntax highlighting
The COBOL code that you are editing will also be highlighted to help you understand the different elements
of the COBOL language. Depending on the color theme that you have selected in VSCode, comments,
COBOL reserved words, literal values and variables will be colored differently allowing you to spot obvious
syntax issues early on before even submitting the code to a build.
21
Figure 4. Breadcrumb trail in VSCode
Clicking on any of the items in the breadcrumb trail will highlight that element of the code in the editor,
quickly showing you the location of that element within the code. It will also show a view of the code in a
pop-up window, shown in Figure 5. , similar to the outline view previously discussed.
22
paragraph or variable to all the places within the application that reference that paragraph or variable. To
demonstrate this, navigate to line 62 of CBL0001, which again is the declaration of the READ-RECORD
paragraph. To see all of the places where this paragraph is called, right click and select Go to References
, or hit the key combination SHIFT+F12 . This will summon a new pop up dialog which shows all the
references to this paragraph in the code, shown in Figure 6.
Note: If SHIFT+F12 does not work for your machine, you may need to use the key combination, Fn+F12
instead.
3.4 Code-completion
Code completion isn’t exactly a new concept in most IDEs. For example, the Eclipse editor has provided
auto-completion for Java APIs for a long time. The same key combination, CTRL+SPACE , triggers this
auto-completion function while you are coding and can be used to help guide you through COBOL syntax
and CICS, IMS API calls.
23
Figure 7. Auto-completion in VSCode
You can see that not only is the variable ACCT-BALANCE prompted as a potential candidate, but it also
presents ACCT_BALANCE IN ACCT-FIELDS.
24
rename the variable, we probably need to update this reference as well. To perform the rename, ensure that
the cursor is on the variable and then press SHIFT/Fn+F2 . This will bring up a small pop-up asking you
to provide a new variable name, as shown in Figure 8. Enter ACCT-NO-TEST and press enter .
25
Figure 10. Highlighting error in source code
Now that you see where the error is located, it can now be corrected. As soon as the error has been rectified,
the problem disappears from the problem view.
3.6 Summary
In this chapter you have been able to go through some of the editing features of the Z Open Editor for
VSCode. These capabilities make editing COBOL, PL/Iand JCL a lot friendlier and easier than some of the
other editors in the market.
26
4 VS Code with Code4z Open-Source Extension Package
This section introduces the Code4z extension package, in particular the COBOL Language Support extension.
• What is Code4z?
• Known File Extensions
• Syntax Highlighting and Coloring
• Syntax and Semantic Check
• Navigation of Code
– Go To Definition
– Find All References
• Copybook Support
• Autocomplete
• Summary
27
Figure 1. The syntax and semantic check feature highlights an error.
4.5.1 Go To Definition
While your cursor is placed on a variable or paragraph name, you can press F12 or CTRL+click to use the
Go To Definition functionality to display the point in the code where the variable or paragraph is defined.
Figure 2. Go To Definition shows the point at which the USER-STREET variable is first defined.
Figure 3. Find All References lists all references to the USER-STREET variable in the code.
28
configure a Zowe CLI zosmf profile. You can also enable support for copybooks stored locally in folders in
your workspace. This is useful when working with a COBOL project stored in a Github repository.
The COBOL Language Support extension helps to ensure that copybooks called in the code remain compatible
through semantic analysis of keywords, variables, and paragraphs within copybooks, and ensures the
consistency of code by defining variables and paragraphs across copybooks. The extension also helps to
protect against unwanted errors caused by recursive or missing copybooks.
The Go To Definition and Find All References functionalities are extended to work for occurrences of
variables and paragraphs from copybooks called in the program as well as from the program itself. You can
also use the Go To Definition feature on a copybook name in order to open it.
4.7 Autocomplete
The COBOL Language Support extension provides live suggestions while you type for COBOL keywords,
as well as variables and paragraphs which are already referenced in the code or in copybooks used by the
program.
Figure 4. Autocomplete lists possible variables and keywords beginning with the typed string in a list.
4.8 Summary
In this chapter you have been introduced to all the COBOL language support features of the Code4z package
of open-source extensions for VS Code.
29
5 Zowe CLI and Zowe CLI Plug-ins
In this chapter we will explain what a CLI is and why you would use it, how to use Zowe CLI interactively,
how to abstract CLI commands into useful scripts, and how Zowe CLI enables the use of familiar open source
tooling while developing COBOL applications on the mainframe.
• What is a CLI and why would you use it?
• What is Zowe CLI?
• Zowe CLI interactive use
– Installing Zowe CLI
– Interactive Help
– Zowe Profiles
– Interacting with z/OS Data Sets
– Interacting with z/OS Jobs
• Automating tasks using Zowe CLI
– Automated Job Submission
– Using Other Programming Languages and Continuous Integration
– Additional Examples
• The world of modern open source tooling
• Summary
30
CLIs are useful for automating repeated tasks. For mainframe COBOL apps, Zowe CLI can help you automate
your build, deployment, and testing processes. Check out this blog for more info and the sample code that
made it possible! Zowe CLI can also help you to automate administrative tasks.
Most IDEs have integrated terminals as well so the CLI can be leveraged from your favorite distributed
development environment, including VS Code!
31
In the example above, multiple extensions are installed. The structure of commands is zowe <group>
<action> <object> followed by various parameters and options specific to the command. For example,
a valid command is zowe files list data-set "HLQ.*". This command will list data-sets matching a
pattern of "HLQ.*". You can append -h to any command to find out more information. Frequently referring
to the help can be difficult and time consuming so if your environment has access to a web browser, simply
append --help-web or --hw to any command to launch interactive web help.
32
5.3.4 Interacting with z/OS Data Sets
Zowe CLI provides a significant suite of z/OS data set interaction functionality. See the following figures for
details on available actions and a sample list command.
33
Figure 7. Sample Zowe CLI zos-jobs submit ds command
34
Figure 10. Sample Zowe CLI response format JSON output
Now, instead of issuing this command and reviewing it to see if the retcode is less than or equal to 4, we
want to automate it. See the implementation in a node script below.
Figure 11. Sample code to submit job and verify output is less than or equal to a maximum allowable RC
I had to make the investment to write this automation but for future job submissions I can simply issue
npm run submitJob. IDEs like VS Code can visualize these tasks making my commonly repeated tasks as
easy as clicking a button :). This job could compile, link, and/or run a COBOL program.
35
Figure 12. Vizualization of npm script and sample run
More advanced code automating the compilation, deployment to test environment, and testing of a COBOL
CICS application is described in this blog.
36
You can then level-up this process by leveraging a CI/CD pipeline. What is a CI/CD pipeline? It is an
automated way of building, testing, and deploying your application and you can do the same with your
COBOL development. The figure below shows the pipeline for the same automated tasks that we did earlier.
Figure 14. CI/CD pipeline of the “one click” COBOL build process
To know more about this topic, check this out.
5.6 Summary
As both a user and programmatic interface, command line interfaces offer significant value in simplifying
complex repeatable processes into single tasks. CLIs are commonly used when developing on popular cloud
platforms like Amazon Web Services. The Zowe CLI is the CLI for the mainframe that has been extended via
numerous plug-ins. Zowe CLI acts as a bridge tool enabling the use of distributed development tooling while
working with mainframe applications. Numerous resources and articles are available for using Zowe CLI to
create custom automation, build CI pipelines, and incorporate static analysis into your COBOL development
processes. Development tooling created by the distributed open source community can now be effectively
leveraged for mainframe development.
37
6 Installation of VSCode and extensions
This chapter covers all aspects of download and installation of Visual Studio (VS) Code and any prerequisites
that are needed. It includes:
• Install prerequisites
– Install node.js
– Install Java SDK
• Install VSCode
• Install VSCode extensions
– Zowe Explorer
– IBM Z Open Editor
– Code4z
• Summary
V12 .16.1
38
Note : The version numbers in our examples are provided purely for reference and may not reflect the latest
versions of the software.
39
Figure 1. VSCode download site
Note : Be sure to select the correct installation file for your workstations respective OS, shown in Figure 1.
6.3.1.1 Install Zowe Explorer Open VSCode and in the left side tool menu select Extensions. From
there, in the “Search Extensions in Marketplace” search field, type Zowe Explorer. Search results will begin
populating, select “Zowe Explorer” and click install, depicted in Figure 3.
40
Figure 3. Install Zowe Explorer in VSCode
The Zowe communinity have a number of on-line video that walk through the steps required to install,
configure and operate the Zowe Explorer, see Zowe Explorer VSC Extension (part 1).
6.3.2.1 Install IBM Z Open Editor Open VSCode and in the left side tool menu select Extensions.
From there, in the “Search Extensions in Marketplace” search field, type IBM Z Open Editor. Search results
will begin populating, select " IBM Z Open Editor " and click install, depicted in Figure 4.
41
Figure 4. Install IBM Z Open Editor in VSCode
Note : There may be some limitations with IBM Z Open Editor if running a 32-bit Java version on Windows.
6.3.3 Code4z
Code4z is an all-in-one, open-source mainframe extension package for developers working with z/OS appli-
cations, suitable for all levels of mainframe experience, even beginners. Mainframe application developers
can use the Code4z package for a modern, familiar, and seamless experience, which helps to overcome some
developers’ reservations or concerns about the traditional mainframe user experience. To find out more about
Code4z, please visit https://github.com/BroadcomMFD/code4z.
6.3.3.1 Install Code4z Open VSCode and in the left side tool menu select Extensions. From there, in
the “Search Extensions in Marketplace” search field, type Code4z. Search results will begin populating, select
" Code4z " and click install.
The extension pack contains a number of extensions that can be leveraged when working with the mainframe,
including the COBOL Language Support extension which provides similar functionality to the Z Open Editor
extension. Therefore, ensure only one of these extensions is enabled. Extensions can be disabled within VS
Code by locating the extension in the Extensions menu, clicking the settings gear, and selecting Disable.
Other extensions included in the Code4z pack will work with either COBOL Language Support or Z Open
Editor.
6.4 Summary
In this chapter you have been introduced to VSCode and some of the extension tools available to it. We have
walked through the process of installing the pre-requisite software, Node.js and Java SDK, as well as VSCode,
Zowe Explorer, IBM Z Open Editor and Code4z. You have also been briefly introduced to the utility of these
extensions in VSCode. In the subsequent chapters we will delve deeper into how and when to use them and
get some practice through lab assignments.
42
7 Installation of Zowe CLI and Plug-ins
This chapter covers all aspects of the download and installation of Zowe CLI and Zowe CLI plug-ins.
• Install prerequisites - Node.js
• Install Zowe CLI
– Public npm Registry
– Package from Zowe.org
• Install Zowe CLI Plug-ins
– Public npm Registry
– Package from Zowe.org
• Summary
If the command returns an EACCESS error, refer to Resolving EACCESS permissions errors when installing
packages globally in the npm documentation. If other issues are encountered in your environment, please
review known Zowe CLI issues for solutions.
We also highly recommend installing the Secure Credential Store plug-in before using the CLI. The Secure
Credential Store Plug-in for Zowe CLI lets you store your credentials securely in the default credential
manager in your computer’s operating system. On Linux, libsecret will need to be installed.
If running Linux, please run the following command for your Linux distribution:
• Debian/Ubuntu: sudo apt-get install libsecret-1-dev
• Red Hat-based: sudo yum install libsecret-devel
• Arch Linux: sudo pacman -S libsecret
To install the Secure Credential Store Plug-in for Zowe CLI, issue the following command:
zowe plugins install
@zowe / secure - credential - store - for - zowe - cli@zowe - v1 - lts
User profiles, which contain connection information for interacting with various z/OS services, created after
installing the plug-in will automatically store your credentials securely.
43
To securely store credentials in existing user profiles (profiles that you created prior to installing the SCS
plug-in), issue the following command:
zowe scs update
If the command returns an EACCESS error, refer to Resolving EACCESS permissions errors when installing
packages globally in the npm documentation. If other issues are encountered in your environment, please
review known Zowe CLI issues for solutions.
The highly recommended Secure Credential Store Plug-in for Zowe CLI lets you store your credentials securely
in the default credential manager in your computer’s operating system. On Linux, libsecret will need to be
installed.
If running Linux, please run the following command for your Linux distribution:
• Debian/Ubuntu: sudo apt-get install libsecret-1-dev
• Red Hat-based: sudo yum install libsecret-devel
• Arch Linux: sudo pacman -S libsecret
To install the Secure Credential Store Plug-in for Zowe CLI, issue the following command from where you
unzipped the core CLI package contents:
zowe plugins install secure - credential - store - for - zowe - cli . tgz
User profiles, which contain connection information for interacting with various z/OS services, created after
installing the plug-in will automatically store your credentials securely.
To securely store credentials in existing user profiles (profiles that you created prior to installing the SCS
plug-in), issue the following command:
zowe scs update
44
For example,
zowe plugins install @zowe / cics - for - zowe - cli@zowe - v1 - lts
Multiple plug-ins can be installed in a single command. For example, to install all Zowe CLI plug-ins available
from the Zowe organization, you could issue:
zowe plugins install @zowe / cics - for - zowe - cli@zowe - v1 - lts
@zowe / ims - for - zowe - cli@zowe - v1 - lts @zowe / mq - for - zowe - cli@zowe - v1 - lts
@zowe / zos - ftp - for - zowe - cli@zowe - v1 - lts
@zowe / db2 - for - zowe - cli@zowe - v1 - lts
Vendor plug-ins on the registry are installed in the same way. For example, to install the CA Endevor plug-in,
you would issue
zowe plugins install @broadcom / endevor - for - zowe - cli@zowe - v1 - lts
For offline installation of vendor plug-ins, please reach out to the specific vendor for details.
7.4 Summary
In this chapter we walked through the process of installing the prerequisite software, Node.js and npm, as
well as Zowe CLI and various plug-ins.
45
Part 2 - Learning COBOL
8 Basic COBOL
This chapter introduces the basics of COBOL syntax. It then demonstrates how to view and run a basic
COBOL program in VSCode.
• COBOL characteristics
– Enterprise COBOL
– Chapter objectives
• What must a novice COBOL programmer know to be an experienced COBOL program-
mer?
– What are the coding rules and the reference format?
– What is the structure of COBOL?
– What are COBOL reserved words?
– What is a COBOL statement?
– What is the meaning of a scope terminator?
– What is a COBOL sentence?
– What is a COBOL paragraph?
– What is a COBOL section?
• COBOL Divisions
– COBOL Divisions structure
– What are the four Divisions of COBOL?
• PROCEDURE DIVISION explained
• Additional information
– Professional manuals
– Learn more about recent COBOL advancements
• Lab
• Lab - Zowe CLI & Automation
– Zowe CLI - Interactive Usage
– Zowe CLI - Programmatic Usage
46
8.1.2 Chapter objectives
The object of the chapter is to expose the reader to COBOL terminology, coding rules, and syntax while the
remaining chapters include greater detail with labs for practicing what is introduced in this chapter.
8.2.1 What are the coding rules and the reference format?
COBOL source code is column dependent, meaning column rules are strictly enforced. Each COBOL source
code line has five areas, where each of these areas has a beginning and ending column.
COBOL source text must be written in COBOL reference format. Reference format consists of the areas
depicted in Figure 1. in a 72-character line.
47
8.2.1.4 Area B (columns 12 - 72)
• Certain items must begin in Area B, they are:
– Entries, sentences, statements, and clauses
– Continuation lines
• Column 12 is referred to as the B Margin
48
8.2.6 What is a COBOL sentence?
A COBOL “Sentence” is one or more “Statements” followed by a period (.), where the period serves as a
scope terminator.
8.3.2.3 DATA DIVISION The DATA DIVISION is where characteristics of data are defined in one of
the following sections:
• FILE SECTION:
Defines data used in input-output operations.
• LINKAGE SECTION:
Describes data from another program. When defining data developed for internal processing.
49
• WORKING-STORAGE SECTION:
Storage allocated and remaining for the life of the program.
• LOCAL-STORAGE SECTION:
Storage allocated each time a program is called and de-allocated when the program ends.
50
3. Messages and Codes - To better understand certain COBOL compiler messages and return codes to
diagnose problems.
http://publibfp.boulder.ibm.com/epubs/pdf/c2746481.pdf
8.6 Lab
In this lab exercise you will connect to an IBM Z system, view a simple COBOL hello world program in
VSCode, submit JCL to compile the COBOL program, and view the output. Refer to “Installation of VSCode
and extensions” to configure VSCode if you have not already done so. You can either use Z Open Editor and
Zowe Explorer, or Code4z.
1. The lab assumes installation of VSCode with either Z Open Editor and Zowe Explorer extensions, as
shown in Figure 2a, or the Code4z extension pack, as shown in Figure 2b.
Click the Extensions icon. If you installed Z Open Editor and Zowe Explorer, the list should include:
1. IBM Z Open Editor
2. Zowe Explorer
Figure 2a. The Z Open Editor and Zowe Explorer VSCode extensions
If you installed Code4z, the list should include:
1. COBOL
2. COBOL Language Support
3. Code4z
4. Debugger for Mainframe
5. Explorer for Endevor
6. HLASM Language Support
7. Zowe Explorer
51
In these exercises, you will only use the COBOL Language Support and Zowe Explorer extensions.
52
Figure 4. Zowe Explorer
4. A box appears to define a new profile. Click + to the left of Create a New Connection to z/OS as
shown in Figure 5.
53
Figure 8. Specified z/OSMF URL
8. The connection prompts for Username as shown in Figure 9.
54
Figure 13. Accept connections with self-signed certifications
13. Result is Favorites in the Data Sets, Unix System Services, and Jobs sections as shown in Figure 14.
55
16. A prompt to “Select a filter” appears for your username. Select the + to “Create a new filter” as shown
in Figure 17.
56
Figure 21. <USERNAME>.CBL
21. Expand <USERNAME>.JCL to view JCL and select member HELLO which is the JCL to compile and
execute simple ‘Hello World!’ COBOL source code as shown in Figure 22.
57
Figure 25. + Select LearnCOBOL connection
25. As a result, the JCL jobs owned by your username appears. HELLOCBL is the JCL job name previously
submitted. Expand HELLOCBL output to view sections of the output as shown in Figure 26.
58
Figure 27. COBOL compiler output
27. View the COBOL program execution by selecting COBRUN:SYSOUT(105) from the LearnCOBOL
in the Jobs section of Zowe Explorer as shown in Figure 28.
59
and Plug-ins” to install Zowe CLI if you have not already done so. Before developing the automation, we will
first leverage the Zowe CLI interactively.
Figure 29. zowe --version command in VS Code Integrated Terminal (Shell selection outlined in red)
2. In order for Zowe CLI to interact with z/OSMF the CLI must know the connection details such as
host, port, username, password, etc. While you could enter this information on each command, Zowe
provides the ability to store this information in configurations commonly known as profiles. Zowe CLI
and the Zowe VS Code Extension share profiles. So if you created a connection profile in the first lab,
you could naturally leverage it here.
To create a LearnCOBOL profile (and overwrite it if it already exists), issue the following command with
your system details (using prompt* will prompt you for certain fields and not show input):
zowe profiles create zosmf LearnCOBOL -- host 192.86.32.250 -- port 10443
-- ru false -- user prompt * -- pass prompt * -- ow
Many profiles can be created for interacting with different z/OSMF instances. If this was not your first
profile, you will want to set it as the default for the following lab exercises. Issue the following command:
zowe profiles set zosmf LearnCOBOL
60
Figure 30. Create and set z/OSMF profile (secure credential store plug-in is in use)
3. Confirm you can connect to z/OSMF by issuing the following command:
zowe zosmf check status
4. List data sets under your ID by issuing a command similar to (see sample output in the following
figure):
zowe files list ds " Z80462 .*"
You can also list all members in a partitioned data set by issuing a command similar to (see sample output in
the following figure):
zowe files list am " Z80462 . CBL "
61
5. Next, we will download our COBOL and JCL data set members to our local machine. First, create
and open a new folder in your file explorer. Note that you could also create a workspace to manage
multiple projects. See the following figure for help:
Then open hello.cbl in your file explorer. A completed example is shown in the following figure:
Figure 33. Download and view data set members using the CLI
6. Next, we will submit the job in member Z80462.JCL(HELLO). To submit the job, wait for it to complete,
and view all spool content, issue:
zowe jobs submit ds " Z80462 . JCL ( HELLO ) " -- vasc
We could also perform this step in piecemeal to get the output from a specific spool file. See the next figure
for an example of the upcoming commands. To submit the job and wait for it to enter OUTPUT status,
issue:
62
zowe jobs submit ds " Z80462 . JCL ( HELLO ) " -- wfo
where JOB00906 and 105 are obtained from the previous commands.
Figure 34. Submit a job, wait for it to complete, then list spool files for the job, and view a specific spool file
If desired, you can also easily submit a job, wait for it to complete, and download the spool content using the
following command (see the following figure for the completed state):
zowe jobs submit ds " Z80462 . JCL ( HELLO ) " -d .
63
Figure 35. Submit a job, wait for it to complete, download and view spool files
The Zowe CLI was built with scripting in mind. For example, you can use the --rfj flag to receive output in
JSON format for easy parsing. See the next figure for an example.
Figure 36. The --rfj flag allows for easy programmatic usage
64
1. Since we already have Node and npm installed, let’s just create a node project for our automation. To
initialize a project, issue npm init in your project’s folder and follow the prompts. You can accept
the defaults by just pressing enter. Only the description and author fields should be changed. See the
following figure.
Figure 37. Use of npm init to create package.json for the project
2. Now that we have our package.json simply replace the test script with a clg script that runs the
following zowe command (replace Z80462 with your high level qualifier):
zowe jobs submit ds ' Z80462 . JCL ( HELLO ) ' -d .
You can name the script whatever you want. I only suggested clg because the CLG in the IGYWCLG proc
(which is what the JCL leverages) stands for compile, link, go. Now, simply issue npm run clg in your
terminal to leverage the automation to compile, link and run the COBOL program and download the output
for review. An example of the completed package.json and command execution are shown in the following
figure.
65
Figure 38. Final package.json and npm run clg execution
3. If you prefer a graphical trigger, you can leverage VS Code as shown in the following figure. Essentially,
the CLI enables you to quickly build your own buttons for your custom z/OS tasks. You could also
invoke a script rather than a single command to accomodate more complex scenarios.
66
9 Data division
Understanding COBOL variables and program processing of variables are essential to effectively learning the
COBOL language. An experienced COBOL programmer must master characteristics of COBOL variables
and the program processing using the variables introduced in this chapter. The objective is to introduce the
reader to the basics of COBOL variables while exposing the reader to the many advanced COBOL variable
options.
Following this chapter is a lab available to compile and execute the COBOL source code provided later in the
chapter. Following the successful compile and execution of one provided program, a second provided COBOL
program with a minor change is available to compile. The second program has an embedded error and on
compile will fail. The failed compilation is an opportunity to identify the error associated with the significance
of PICTURE clause data types associated with the operation of the COMPUTE statement (discussed in this
chapter) and how to solve the error.
• Variables / Data-items
– Variable / Data-item name restrictions and data types
• PICTURE clause
– PIC clause symbols and data types
– Coding COBOL variable / data-item names
– PICTURE clause character-string representation
• Literals
– Figurative constants
– Data relationships
– Levels of data
• MOVE and COMPUTE
• Lab
67
When COBOL source code is compiled into an executable program, the COBOL compiler is expecting a
named COBOL variable to possess attributes such as a length and data type. During program execution, the
variable represents a defined area of processing memory where the memory location has a maximum length
and designated data type.
A list of the most common COBOL data types are:
• Numeric (0-9)
• Alphabetic (A-Z), (a-z), or a space
• Alphanumeric Numeric and Alphabetic Combination
68
• PIC clause for a value such as $1,123.45 is coded as follows:
PIC $9,999V99
9.3 Literals
A COBOL literal is constant data value, meaning the value will not change like a variable can. The
COBOL statement, DISPLAY "HELLO WORLD!", is a COBOL reserved word, DISPLAY , followed by a literal,
HELLO WORLD!
9.3.2.1 Level numbers A structured level number hierarchic relationship is available to all DATA
DIVISION sections. Figure 1. shows the level number hierarchic relationship with programmer chosen level
numbers, variable names and PIC clauses in the File Section where “01 PRINT-REC” references the following
“05”-level group of variables and the “01 ACCT-FIELDS” references the following “05”-level group of variables.
Observe 05-level CLIENT-ADDR is further subdivided into several 10-level names. COBOL code referencing
the name CLIENT-ADDR includes the 10-level names.
69
Figure 1. Level number hierarchic relationship
70
71
Figure 2. MOVE and COMPUTE example
9.5 Lab
Note : It may take a few seconds to load in all segments of this lab. If files are not loading, hit the refresh
button on the list that appears when hovering over the section bar.
1. View the PAYROL00 COBOL source code member in the ‘id’.CBL data set.
2. Submit the JCL member, PAYROL00, from the id.JCL, where id is your id,dropdown. This is where
id.JCL(PAYROL00) compiles and successfully executes the PAYROL00. program.
Note : If you receive this error message after submitting the job:
That is because you submitted the job from the .CBL data set and not the .JCL data set.
3. View both compile and execution of PAYROL00 job output, referenced in Figure 4.
72
##. A return code of 12 means there was an error, but how do we know what that error was? Continue to
find out!
7. Observe the text associated with IGYPA3146-S on line 137 within the job output (compile), illustrated
in Figure 6.
73
10 File handling
The previous chapter and lab focused on variables and moving literals into variables, then writing variable
content using the COBOL DISPLAY statement. This section introduces reading records from files into
variables, moving the variables to output variables, and writing the output variables to a different file. A
simple COBOL program to read each record from a file and write each record to a different file is used to
illustrate COBOL code necessary to read records from an input external data source and write records to an
output external data source.
An experienced COBOL programmer can answer the question, “How does an Enterprise COBOL program
read data from an input external data source and write data to an output external data source?” The
objective of this chapter is to provide enough comprehensive information for the reader to be able to answer
that question.
• COBOL code used for sequential file handling
– COBOL inputs and outputs
– FILE-CONTROL paragraph
– COBOL external data source
– Data sets, records, and fields
– Blocks
– ASSIGN clause
• PROCEDURE DIVISION sequential file handling
– Open input and output for read and write
– Close input and output
• COBOL programming techniques to read and write records sequentially
– READ-NEXT-RECORD paragraph execution
– READ-RECORD paragraph
– WRITE-RECORD paragraph
– Iterative processing of READ-NEXT-RECORD paragraph
• Lab
74
– WRITE FROM statement
Figure 1. FILE-CONTROL
While SELECT gives a name to an internal file and ASSIGN gives a name to the external dataset name,
a COBOL program needs more information about both. The COBOL compiler is given more information
about both in the DATA DIVISION, FILE SECTION.
The COBOL reserved word ‘FD’ is used to give the COBOL compiler more information about internal file
names in the FILE-SECTION. The code below the FD statement is the record layout. The record layout
consists of level numbers, variable names, data types, and lengths as shown in Figure 2.
75
Figure 2. FILE-SECTION
10.1.5 Blocks
Each record read by the program can result in disk storage access. A program typically reads 1 record at a
time in sequential order until all records are read. When a record is read, the record retrieved from disk is
stored in memory for program access. When each next record read requires the need to retrieve the record
from disk, system performance is impacted negatively. Records can be blocked where a block is a group
of records. The result is when the first record is read, then an entire block of records is read into memory
assuming the program will be reading the second, third, etc. records avoiding unnecessary disk retrievals and
negative system performance. The memory holding a record or block of records to be read by the program is
known as a buffer. COBOL BLOCK CONTAINS clause is available to specify the size of the block in the
buffer. Observe Figure 3.
76
10.1.6 ASSIGN clause
While the SELECT clause name is an internal file name, the ASSIGN clause name is describing a data source
external to the program. z/OS uses Job Control Language, JCL, operations to tell the system what program
to load and execute followed by input and output names needed by the program. The JCL input and output
names are call DDNAMEs. The JCL DDNAME statement includes a JCL DD operation where DD is an
abbreviation for Data Definition. On the same DDNAME statement is the system-controlled data set name.
COBOL code “SELECT ACCT-REC ASSIGN TO ACCTREC” requires a JCL DDNAME ACCTREC with
a DD redirecting ACCTREC to a z/OS controlled dataset name, MY.DATA. The COBOL program is shown
in Example 1.
The purpose of the redirection of ACCT-REC, via ASSIGN TO, to JCL DDNAME, ACCTREC is flexibility.
ACCT-REC is used in the program itself, ACCTREC is a bridge to JCL, shown in Example 1. , and a DD
JCL statement links ACCTREC to an actual dataset, shown in Example 2. This flexibility allows the same
COBOL program to access a different data source with a simple JCL modification avoiding requirement to
change the source code to reference the alternate data source.
SELECT ACCT - REC ASSIGN TO ** ACCTREC **
77
Figure 4. OPEN-FILES
Figure 5. CLOSE-STOP
78
Figure 6. Reading and writing records
Note: COBOL is English-like and COBOL reserved words are English-like. The programmer is free to
use English-like variable names to help remember the purpose of the variable names. The PROCEDURE
DIVISION structure is English-like. A paragraph contains one or more sentences. A sentence contains one or
more statements. The implicit scope terminator, a period (.), terminates a sentence or terminates several
consecutive statements which would be analogous to a compounded sentence where ‘and’ joins potentially
independent sentences together. ###
79
END-READ. The paragraph is terminated by an implicit scope terminator, (.) period. Control is returned to
READ-NEXT-RECORD paragraph to execute the next statement, PERFORM WRITE-RECORD.
80
Figure 7. Iterative processing
10.4 Lab
The lab associated with this chapter demonstrates the ‘end-of-file’ COBOL coding technique for reading all
data records from a sequential file. If a step has an asterisk (*) next to it, it will have a hint associated at
the end of the lab content.
1. If not already, open VSCode and select Zowe Explorer from the left sidebar.
Note : If you are opening a new instance of VSCode (i.e. you closed out of it after the previous usage),
you may need to ‘Select a filter’ again. You can do so by selecting the search icon next to your named
connection in the DATA SETS section and then reselecting the filter previously used. It should be in the
listed filters after you have selected the search symbol.
2. View these COBOL source code members listed in the id.CBL data set:
• CBL0001
• CBL0002
3. View these three JCL members in the id.JCL data set:
• CBL0001J
• CBL0002J
• CBL0033J
Figure 8. Id.JCL(CBL0001J).jcl
4. Submit job, JCL(CBL0001J), within the DATA SET section.
5. View that job output using the JOBS section.
• COBRUN:SYSPRINT(101) - COBOL program compiler output
• RUN:PRTLINE(103) - COBOL program execution output, shown in Figure 9.
81
Figure 9. RUN:PRTLINE(103) for JCL(CBL0001J)
6. Submit job, JCL(CBL0002J), within the DATA SET section.
7. View that job output using the JOBS section.
• COBRUN:SYSPRINT(101) - COBOL program compiler output
Locate COBOL compiler severe message IGYPS2121-S within the output file referred to in step 7,
shown in Figure 10.
82
Figure 11. RUN:SYSOUT(104) message
12. Fix this error by editing JCL(CBL0033J):
• Determine the DDNAME needed, but missing or misspelled.
• Correct it within the code and save
13. Re-submit job, JCL(CBL0033J), using the DATA SET section.
14. View CBL0033J output using the JOBS section, your output should look like Figure 12.
• RUN:PRTLINE - COBOL program execution output (if correction is successful)
83
11 Program structure
In this chapter we discuss the concept of structured programming and how it relates to COBOL. We highlight
the key techniques within the COBOL language that allow you to write good well-structured programs.
• Styles of programming
– What is structured programming
– What is Object Orientated Programming
– COBOL programming style
• Structure of the Procedure Division
– Program control and flow through a basic program
– Inline and out of line perform statements
– Using performs to code a loop
– Learning bad behavior using the GO TO keyword
• Paragraphs as blocks of code
– Designing the content of a paragraph
– Order and naming of paragraphs
• Program control with paragraphs
– PERFORM TIMES
– PERFORM THROUGH
– PERFORM UNTIL
– PERFORM VARYING
• Using subprograms
– Specifying the target program
– Specifying program variables
– Specifying the return value
• Summary
• Lab
84
analyze and read. Although COBOL does contain these structures, it is important to use them sparingly and
not as the backbone of well-structured code.
Well-structured code is both easy to understand and to maintain. It is highly likely that at some point in
your career you will be required to read and work from someone else’s code, often a decade after it was
originally written. It would be extremely helpful to you if the original author structured their code well and
likewise if it is your code someone else is reading.
...
85
STOP RUN .
86
PERFORM VARYING COUNTER FROM 01 BY 1 UNTIL COUNTER EQUAL 11
MOVE COUNTER TO MSG - TO - WRITE
WRITE PRINT - REC
END - PERFORM .
Example 5. GO TO example
If we were to compile and run the program, you would see that although the job ABENDS (abnormally ends)
with a 4038-abend code, it did execute some of the code and wrote the first two lines of the output. If you
were to look at the output in more detail, you would see a message like the following:
IGZ0037S The flow of control in program TOTEN1 proceeded beyond the last
line of the program .
87
0 01 FLAG PIC 9(1) VALUE 1.
88
Note: A paragraph can also be ended by END-PROGRAM, END-METHOD, END FACTORY OR END-
OBJECT. Most of these are used within Object Orientated COBOL which is not discussed here.
Considering that a program can be made up of multiple paragraphs and that the PERFORM keyword can
be used to call the paragraph, either conditionally or as part of a loop, it is easy to see that good paragraph
design really helps makes your COBOL more structured and readable.
89
PERFORM 4000 - READ - RECORD
END - PERFORM .
*
3000 - CLOSE - STOP .
CLOSE ACCT - REC .
CLOSE PRINT - LINE .
STOP RUN .
*
4000 - READ - RECORD .
READ ACCT - REC
AT END MOVE 'Y ' TO LASTREC
END - READ .
*
5000 - WRITE - RECORD .
MOVE ACCT - N - TO ACCT - NO - O .
MOVE ACCT - LIMIT TO ACCT - LIMIT - O .
MOVE ACCT - BALANCE TO ACCT - BALANCE - O .
MOVE LAST - NAME TO LAST - NAME - O .
MOVE FIRST - NAME TO FIRST - NAME - O .
MOVE COMMENTS TO COMMENTS - O .
WRITE PRINT - REC .
90
11.4.1 PERFORM TIMES
Perhaps the simplest way of repeating a perform statement is to use the TIMES keyword to perform a
paragraph or sections of code a static number of times, shown in Example 10.
PERFORM 10 TIMES
MOVE FIELD - A TO FIELD - B
WRITE RECORD
END - PERFORM .
91
Example 13. PERFORM UNTIL
This would be equivalent to the Java code:
while ( counter != 10) {
// counter ++
// move counter to msg - to - write
// write print - rec
}
92
// write print - rec
}
}
93
Note: When passing variables either BY REFERENCE or BY CONTENT, note you can send data items of
any level. Which means you can pass entire data structures, handy for dealing with common records.
You might also see the phrase, BY VALUE, being used in a CALL sentence. BY VALUE is similar to BY
CONTENT, as a copy of the content of the variable is passed. The difference is that only a subset of COBOL
datatypes are supported and you can only specify elementary data-items. This is because BY VALUE is
primarily used when COBOL is calling a program of another language (such as C).
11.6 Summary
In summary, this chapter should provide the necessary foundation to understand structured programming
and how it relates to COBOL and its importance to understanding and maintaining code. Many examples of
how, when and why to implement key techniques have been provided and explained for further understanding.
You should be able to identify the basic differences between structured programming (COBOL) and OO
programming (Java). You should also understand the general concept of the best practices in the structure of
the Procedure Division with reference to the design and content of paragraphs, program control options and
ways to call other programs within the same system.
11.7 Lab
This lab utilizes COBOL program CBL0003, located within your id.CBL data set, as well as JCL job
CBL0003J, located within your id.JCL data set. The JCL jobs are used to compile and execute the COBOL
programs, as discussed in previous chapters.
94
12 File output
Designing a structured layout that is easy to read and understand is required to format output. Designing a
structured layout involves column headings and variable alignment using spaces, numeric format, currency
format, etc. This chapter aims to explain this concept utilizing example COBOL code to design column
headings and align data names under the such headings. At the end of the chapter you are asked to complete
a lab that practices implementation of the components covered.
A capability of COBOL data output formatting that is worth noting but not covered in this chapter is that
COBOL is a web enabled computer language. COBOL includes easy and quick transformation of existing
COBOL code to write JSON (JavaScript Object Notation) where the output is subsequently formatted for a
browser, a smartphone, etc. Frequently, the critical data accessed by a smart phone, such as a bank balance,
is stored and controlled by z/OS where a COBOL program is responsible for retrieving and returning the
bank balance to the smart phone.
• Review of COBOL write output process
– ENVIRONMENT DIVISION
• FILE DESCRIPTOR
– FILLER
• Report and column headers
– HEADER-2
• PROCEDURE DIVISION
– MOVE sentence
– PRINT-REC FROM sentences
• Lab
95
Figure 1. SELECT and ASSIGN
12.2.1 FILLER
Observe the data name FILLER. While most data fields have unique names, FILLER is a COBOL reserved
word data name, that is useful for output formatting. This is in part because FILLER allocates memory
space without the need for a name. Also, FILLER allocated memory has a defined length in the output line
and may contain spaces or any literal. Figure 2. shows multiple VALUE SPACES for FILLER. SPACES
create white space between data-items in the output which is valuable in keeping the code readable. More
specifically in Figure 2. FILLER PIC X(02) VALUE SPACES, represents the output line containing two
spaces.
Figure 2. FILLER
96
DIVISION and includes the headers listed and defined below.
• HEADER-1:
– Writes a literal
– Example: ‘Financial Report for’
• HEADER-2:
– Writes literals
– Examples:
∗ ‘Year’ followed by a variable name
∗ ‘Month’ followed by a variable name
∗ ‘Day’ followed by a variable name
• HEADER-3:
– Writes literals
– Examples:
∗ ‘Account’ followed by FILLER spacing
∗ ‘Last Name’ followed by FILLER spacing
∗ ‘Limit’ followed by FILLER spacing
∗ ‘Balance; followed by FILLER spacing
• HEADER-4:
– Writes dashes followed by FILLER spacing
97
Figure 3. Designed output structure layout
12.3.1 HEADER-2
HEADER-2 includes the year, month, day of the report together with FILLER area, creating blank spaces
between the year, month, and day, as you can see in Figure 3. Figure 4. is an example of the data name
layout used to store the values of CURRENT-DATE. The information COBOL provides in CURRENT-DATE
is used to populate the output file in HEADER-2.
98
Figure 4. CURRENT-DATE intrinsic function
99
PRINT-REC FROM defined header data names, HEADER-1 and HEADER-2, from Figure 3. The PRINT-
REC file descriptor data names in Figure 2. are effectively replaced with the content of the header data
names in Figure 3. written to output. The sentences on lines 7 and 8 result in a blank line written between
headers. The sentences on lines 9 and 10 write the PRINT-REC FROM defined HEADER-3 and HEADER-4
data names from Figure 3. The PRINT-REC file descriptor data names in Figure 2. are effectively replaced
with the content of the header data names in Figure 3.
12.5 Lab
This lab utilizes two COBOL programs, CBL0004 and CBL0005, located within your id.CBL data set, as
well as two JCL jobs, CBL0004J and CBL0005J, located within your id.JCL data set. The JCL jobs are used
to compile and execute the COBOL programs, as discussed in previous chapters.
100
13 Conditional expressions
This chapter dives into how programs make decisions based upon the programmer written logic. Specifically,
programs make these decisions within the PROCEDURE DIVISION of the source code. We will expand on
several topics regarding conditional expressions written in COBOL through useful explanations, examples
and eventually practicing implementation through a lab.
• Boolean logic, operators, operands, and identifiers
– COBOL conditional expressions and operators
– Examples of conditional expressions using Boolean operators
• Conditional expression reserved words and terminology
– IF, EVALUATE, PERFORM and SEARCH
– Conditional states
– Conditional names
• Conditional operators
• Conditional expressions
– IF ELSE (THEN) statements
– EVALUATE statements
– PERFORM statements
– SEARCH statements
• Conditions
– Relation conditions
– Class conditions
– Sign conditions
• Lab
101
A list of COBOL Boolean relational operators for each of the common type of COBOL conditional expressions
are represented in Figures 1, 2 and 3 below.
102
This conditional expression evaluates to false because while 5 > 1 is true, 1 > 2 is false. The AND operation
requires both expressions to be true to return true for the compounded condition expression. Let’s show
another implementation, this time using the OR Boolean operator.
IF (5 > 1 OR 1 > 2)THEN .... ELSE ....
This conditional expression evaluates to true because while 1 > 2 is false, 5 > 1 is true. The OR operation
requires only one of the expressions to be true to return true for the entire compounded condition expression.
More conditional operators used for relation, class, and sign conditions are discussed further on in the chapter.
103
MOVE 'TX ' TO USA - STATE .
....
....
IF STATE DISPLAY ' The State is Texas '
ELSE DISPLAY ' The State is not Texas '
END - IF .
104
Relational operator Can be written Meaning
IS LESS THAN OR EQUAL TO IS <= Is less than or equal to
105
13.4.3 PERFORM statements
A PERFORM with UNTIL phrase is a conditional expression. In the UNTIL phrase format, the procedures
referred to are performed until the condition specified by the UNTIL phrase evaluates to true. Using the
FACIAL-EXP conditional-name, the SAY-SOMETHING-DIFFERENT paragraph is executed continuously
UNTIL FACIAL-EXP contains ‘HAPPY’, observe Example 5.
PERFORM SAY - SOMETHING - DIFFERENT BY FACIAL - EXP UNTIL ' HAPPY '
END - PERFORM .
13.5 Conditions
A conditional expression can be specified in either simple conditions or complex conditions. Both simple
and complex conditions can be enclosed within any number of paired parentheses; the parentheses, however,
do not change whether the condition is simple or complex. This section will cover three of the five simple
conditions:
• Relation
• Class
• Sign
106
• Numeric
– IS NUMERIC or IS NOT NUMERIC
• Alphabetic
– IS ALPHABETIC or IS NOT ALPHABETIC
– IS ALPHABETIC-LOWER / ALPHABETIC-UPPER
– IS NOT ALPHABETIC-LOWER / ALPHABETIC-UPPER
• DBCS
– IS DBCS or IS NOT DBCS
– IS KANJI or IS NOT KANJI
13.6 Lab
This lab requires two COBOL programs, CBL0006 and CBL0007 and two respective JCL Jobs, CBL0006J
and CBL0007J, to compile and execute the COBOL programs. All of which are provided to you in your
VSCode - Zowe Explorer.
107
Figure 4. Id.JCL(CBL0006J) output
5. Submit CBL0007J
6. Find the compile error, IGYPS2113-E, in the job output.
7. Go ahead and modify id.CBL(CBL0007) to correct the syntax error outlined by the IGYPS2113-E
message.*
8. Re-submit CBL0007J
9. Validate that the syntax error was corrected by getting an error free output file.
108
14 Arithmetic expressions
This chapter aims to introduce the concept of implementing arithmetic expressions in COBOL programs.
We will review the basic concept of arithmetic expressions, operators, statements, limitations, statement
operands, as well as precedence of operation within the expressions. You will be able to follow along with a
comprehensive example exhibiting the usage of arithmetic expressions in a COBOL program that you have
seen in previous chapters and labs. Following the chapter is a lab to practice the implementation of what you
have learned.
• What is an arithmetic expression?
– Arithmetic operators
– Arithmetic statements
• Arithmetic expression precedence rules
– Parentheses
• Arithmetic expression limitations
• Arithmetic statement operands
– Size of operands
• Examples of COBOL arithmetic statements
• Lab
109
Binary operator Meaning Unary operator Meaning
- Subtraction - Multiplication by -1
Multiplication
/ Division
Exponentiation
14.2.1 Parentheses
Parentheses are used to denote modifications to normal order of operations (precedence rules). An arithmetic
expression within the parentheses is evaluated first and result is used in the rest of the expression. When
expressions are contained within nested parentheses, evaluation proceeds from the least inclusive to the most
inclusive set. That means you work from the inner most expression within parentheses to the outer most.
The precedence for how to solve an arithmetic expression in Enterprise COBOL with parentheses is:
1. Parentheses (simplify the expression inside them)
2. Unary operator
3. Exponents
4. Multiplication and division (from left to right)
5. Addition and subtraction (from left to right)
Parentheses either eliminate ambiguities in logic where consecutive operations appear at the same hierarchic
level or modify the normal hierarchic sequence of execution when necessary. When the order of consecutive
110
operations at the same hierarchic level is not completely specified by parentheses, the order is from left to
right.
An arithmetic expression can begin only with a left parenthesis, a unary operator, or an operand (that is, an
identifier or a literal). It can end only with a right parenthesis or an operand. An arithmetic expression must
contain at least one reference to an identifier or a literal.
There must be a one-to-one correspondence between left and right parentheses in an arithmetic expression,
with each left parenthesis placed to the left of its corresponding right parenthesis. If the first operator in
an arithmetic expression is a unary operator, it must be immediately preceded by a left parenthesis if that
arithmetic expression immediately follows an identifier or another arithmetic expression.
111
ROUNDING and ON SIZE ERROR handling.
112
Figure 3. READ-NEXT-RECORD.
Figure 4. is an example of two COMPUTE statements in the paragraph, LIMIT-BALANCE-TOTAL. Notice
that the results of the COMPUTE statements are to add client ACCT-LIMIT to the current TLIMIT and
add client ACCT-BALANCE to TBALANCE totals each time the paragraph is executed, which is one time
for each client record read in our example.
Figure 5. WRITE-TLIMIT-TBALANCE
113
14.6 Lab
This lab requires two COBOL programs, CBL0008 and CBL0009 and two respective JCL Jobs, CBL0008J
and CBL0009J, to compile and execute the COBOL programs. All of which are provided to you in your
VSCode - Zowe Explorer.
114
15 Data types
A COBOL programmer must be aware that the computer stored internal data representation and formatting
can differ, where the difference must be defined in the COBOL source code. Understanding the computer’s
internal data representation requires familiarity with binary, hexadecimal, ASCII, and EBCDIC. Packed-
Decimal is needed to explain COBOL Computational and Display data format. This chapter aims to
familiarize the reader with these different “types” of data representation.
• Data representation
– Numerical value representation
– Text representation
• COBOL DISPLAY vs COMPUTATIONAL
• Lab
15.1.1.1 COMP-1 This is also known as a single-precision floating point number representation. Due to
the floating-point nature, a COMP-1 value can be very small and close to zero, or it can be very large (about
10 to the power of 38). However, a COMP-1 value has limited precision. This means that even though a
COMP-1 value can be up to 10 to the power of 38, it can only maintain about seven significant decimal digits.
Any value that has more than seven significant digits are rounded. This means that a COMP-1 value cannot
exactly represent a bank balance like $1,234,567.89 because this value has nine significant digits. Instead,
the amount is rounded. The main application of COMP-1 is for scientific numerical value storage as well as
computation.
15.1.1.2 COMP-2 This is also known as a double-precision floating point number representation. COMP-
2 extends the range of value that can be represented compared to COMP-1. COMP-2 can represent values
up to about 10 to the power of 307. Like COMP-1, COMP-2 values also have a limited precision. Due to the
expanded format, COMP-2 has more significant digits, approximately 15 decimal digits. This means that
once a value reaches certain quadrillions (with no decimal places), it can no longer be exactly represented in
COMP-2.
COMP-2 supersedes COMP-1 for more precise scientific data storage as well as computation. Note that
COMP-1 and COMP-2 have limited applications in financial data representation or computation.
15.1.1.3 COMP-3 This is also known as packed BCD (binary coded decimal) representation. This is,
by far, the most utilized numerical value representation in COBOL programs. Packed BCD is also somewhat
unique and native to mainframe computers such as the IBM z architecture.
115
Unlike COMP-1 or COMP-2, packed BCD has no inherent precision limitation that is independent to the
range of values. This is because COMP-3 is a variable width format that depends on the actual value format.
COMP-3 exactly represents values with decimal places. A COMP-3 value can have up to 31 decimal digits.
15.1.1.4 COMP-4 COMP-4 is only capable of representing integers. Compared to COMP-1 and COMP-
2, COMP-4 can store and compute with integer values exactly (unless a division is involved). Although
COMP-3 can also be used to represent integer values, COMP-4 is more compact.
15.1.1.5 COMP-5 COMP-5 is based on COMP-4, but with the flexibility of specifying the position of a
decimal point. COMP-5 has the space efficiency of COMP-4, and the exactness of COMP-3. Unlike COMP-3,
however, a COMP-5 value cannot exceed 18 decimal digits..
15.1.2.1 EBCDIC Extended Binary Coded Decimal Interchange Code (EBCDIC) is an eight binary
digits character encoding standard, where the eight digital positions are divided into two pieces. EBCDIC
was devised in the early 1960’s for IBM computers. EBCDIC is used to encode text data so that text can be
printed or displayed correctly on devices that also understand EBCDIC.
15.1.2.2 ASCII American Standard Code for Information Interchange, ASCII, is another binary digit
character encoding standard.
15.1.2.3 EBCDIC vs ASCII Why are these two standards when they seemingly perform the same
function?
EBCDIC is a standard that traces its root to punch cards designed in 1931. ASCII, on the other hand, is a
standard that was created, unrelated to IBM punch cards, in 1967. A COBOL program natively understands
EBCDIC, and it can comfortably process data originally captured in punch cards as early as 1931.
ASCII is mostly utilized by non-IBM computers.
COBOL can encode and process text data in EBCDIC or ASCII. This means a COBOL program can
simultaneously process data captured in a census many decades ago while exporting data to a cloud service
utilizing ASCII or Unicode. It is important to point out, however, that the programmer must have the
awareness and choose the appropriate encoding.
15.3 Lab
Many of the previous COBOL lab programs you have worked with thus far are reading records containing two
packed decimal fields, the client account limit and the client account balance. In the Arithmetic expressions
lab, the total of all client account limits and balances used a COMPUTE statement, where the COMP-3
fields contained the packed decimal internal data.
What happens when an internal packed decimal field is not described using COMP-3? Without using COMP-3
to describe the field, the COBOL program treats the data as DISPLAY data (EBCDIC format). This lab
demonstrates what happens during program execution without using COMP-3.
116
15.3.0.1 Using VSCode and Zowe Explorer
1. Submit the job, id.JCL(CBL0010J)
2. Observe that the compile of the COBOL source was successful, however, also observe that the execution
of the job failed. How can you tell?
There’s no CC code next to CBL0010J(JOB#), instead there is an ABENDU4038 message. U4038 is a
common user code error typically involving a mismatch between the external data and the COBOL
representation of the data.
3. Read the execution SYSOUT message carefully. The SYSOUT message mistakenly believes the records
are 174 characters in length while the program believes the records are 170 characters in length.
Explanation: Packed decimal (COMP-3) expands into two numbers where only one number would
typically exist. If the program reads a packed decimal field without describing the field as COMP-3, then
program execution becomes confused about the size of the record because the PIC clause, S9(7)V99, is
expecting to store seven numbers plus a sign digit when only three word positions are read. Therefore,
execution reports a four-record length position discrepancy.
4. Edit id.CBL(CBL0010) to identify and correct the source code problem.*
5. Submit id.JCL(CBL0010J) and verify correction is successful with a CC 0000 code.
Lab Hints:
The ACCT-LIMIT PIC clause in the ACCT-FIELDS paragraph should be the same as the PIC clause for
ACCT-BALANCE.
117
16 Intrinsic functions
Today’s COBOL is not your parents COBOL. Today’s COBOL includes decades of feature/function rich
advancements and performance improvements. Decades of industry specifications are applied to COBOL
to address the growing needs of businesses. What Enterprise COBOL for z/OS promised and delivered, is
decades of upward compatibility with new releases of hardware and operating system software. The original
DNA of COBOL evolved into a powerful, maintainable, trusted, and time-tested computer language with no
end in sight.
Among the new COBOL capabilities is JSON GENERATE and JSON PARSE, providing an easy to use
coding mechanism to transform DATA DIVISION defined data-items into JSON for a browser, a smart phone,
or any IoT (Internet of Things) device to format in addition to transforming JSON received from a browser,
a smart phone, or any IoT device into DATA DIVISION defined data-items for processing. Frequently, the
critical data accessed by a smart phone, such as a bank balance, is stored and controlled by z/OS where a
COBOL program is responsible for retrieving and returning the bank balance to the smart phone. COBOL
has become a web enabled computer language.
Previous COBOL industry specifications included intrinsic functions, which remain largely relevant today.
An experienced COBOL programmer needs to be familiar with intrinsic functions and stay aware of any new
intrinsic functions introduced. This chapter aims to cover the foundation of intrinsic functions and their
usage in COBOL.
• What is an intrinsic function?
– Intrinsic function syntax
– Categories of intrinsic functions
• Intrinsic functions in Enterprise COBOL for z/OS V6.3
– Mathematical example
– Statistical example
– Date/time example
– Financial example
– Character-handling example
• Use of intrinsic functions with reference modifiers
• Lab
118
01 Item -1 Pic x (30) Value " Hello World !".
01 Item -2 Pic x (30) .
. . .
Display Item -1
Display Function Upper - case ( Item -1)
Display Function Lower - case ( Item -1)
Move Function Upper - case ( Item -1) to Item -2
Display Item -2
119
Example 2. Mathematical intrinsic function
120
16.2.5 Character-handling example
Example 6. shows used of COBOL function UPPER-CASE where an string or alphabetic variable processed
by UPPER-CASE will translate any lower case characters to upper case.
MOVE FUNCTION UPPER - CASE (" This is shouting !") TO SOME - FIELD
DISPLAY SOME - FIELD
Output - THIS IS SHOUTING !
LNAME (1:1)
LNAME (4:2)
16.4 Lab
This lab contains data that includes a last name, where last name is all upper-case. It demonstrates the use
of intrinsic functions together with reference modification to lower-case the last name characters, except the
first character of the last name.
This lab requires two COBOL programs, CBL0011 and CBL0012 and two respective JCL Jobs, CBL0011J
and CBL0012J, to compile and execute the COBOL programs. All of which are provided to you in your
VSCode - Zowe Explorer.
121
Figure 1. Current lab vs. Data types lab output
3. Observe the PROCEDURE DIVISION intrinsic function, lower-case, within the WRITE-RECORD
paragraph. This intrinsic function is paired with a reference modification resulting in output of last
name with upper-case first character and the remainder in lower-case.
4. Submit CBL0012J
5. Observe the compile error.
Previous lab programs made use of a date/time intrinsic function. The date/time intrinsic function in this
lab has a syntax error that needs to be identified and corrected.
6. Modify id.CBL(CBL0012) correcting compile error.*
7. Re-submit CBL0012J
8. Corrected CBL0012 source code should compile and execute the program successfully. A successful
compile will result in the same output as CBL0011J.
Lab Hints
Refer to CBL0011 line 120 for the proper formatting of the function-name causing the compile error.
122