COBOL/Functions
Functions in COBOL
[edit | edit source]In computer science a function is a sequence of program instructions that performs a specific task and is packaged as a unit. This unit can then be used in programs wherever that task should be performed.
Functions may be defined within programs, or separately in libraries that can be used by many programs. In different programming languages, a function may be called a procedure, a subroutine, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.
Functions have a variety of forms in COBOL, and a variety of verbs can be used to call—transfer execution to and from—the function.
This example uses the simple form of the PERFORM verb. In this example the PERFORM GET-FAHRENHEIT command references the GET-FARENHEIT function defined later in the listing. When program execution encounters the PERFORM GET-FAHRENHEIT statement, execution is transferred to the GET-FARENHEIT function. When that function completes, execution returns to the next statement after PERFORM GET-FAHRENHEIT statement that called the function. This call and return occurs similarly for the CALCULATE-CELSIUS and DISPLAY-RESULT functions.
Although not demonstrated in this example, the PERFORM verb has several alternative forms. These include
- PERFORM THRU—used to execute a series of paragraph by giving the first and last paragraph names in the sequence. After executing the last paragraph, the control is returned.
- PERFORM UNTIL—a paragraph is executed until the given condition becomes true. The default condition is ‘With test before’ indicating the condition is checked before the execution of statements in a paragraph.
- PERFORM TIMES—a paragraph will be executed the number of times specified.
- PERFORM VARYING—a paragraph will be executed until the condition in UNTIL phrase becomes true.
In addition to user-defined functions, many system-provided functions, known as intrinsic functions, are provided by COBOL systems. COBOL also provides subroutines that are programs that can be completed independently. Subroutines are invoked using the CALL verb.
The Functions lesson in the Wikiversity course Programming Fundamentals provides additional background, readings, multimedia resources, key terms, and activities related to understanding and using functions.
Please study those materials to better understand and extend the following example program.
functions.cbl
[edit | edit source] *> This program asks the user for a Fahrenheit temperature,
*> converts the given temperature to Celsius,
*> and displays the results.
*>
*> References:
*> https://www.mathsisfun.com/temperature-conversion.html
*> https://www.tutorialspoint.com/cobol/index.htm
*> https://open-cobol.sourceforge.io/doc/gnucobol.html
IDENTIFICATION DIVISION.
PROGRAM-ID. functions.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 fahrenheit PIC 999V99.
01 celsius PIC ZZ9.99.
PROCEDURE DIVISION.
main.
PERFORM get-fahrenheit.
PERFORM calculate-celsius.
PERFORM display-result.
STOP RUN.
get-fahrenheit.
DISPLAY "Enter Fahrenheit temperature:".
ACCEPT fahrenheit.
calculate-celsius.
COMPUTE celsius = (fahrenheit - 32) * 5 / 9.
display-result.
DISPLAY fahrenheit "° Fahrenheit is " celsius "° Celsius".
END PROGRAM functions.
Try It
[edit | edit source]Copy and paste the code above into one of the following free online development environments or use your own Functions compiler / interpreter / IDE.
Assignment
[edit | edit source]Part 1:
The kelvin is the base unit of temperature in the International System of Units (SI), having the unit symbol K. It is widely used in scientific work.
Celsius can be converted to Kelvins using the following formula:
K = °C + 273
Modify the example program listed above to convert Celsius temperatures to and from Kelvins and display the result.
Run and test the program in at least one programming environment.
Part 2:
Dutch colonists acquired Manhattan on May 24, 1626 in exchange for traded goods often said to be worth US$24. If that money was invested at 5% interest computed annually, how much would it be worth today?
Modify the example program to perform that calculation and display the result. The formula
New Principle sum = Principle sum × (1 + annual interest rate)number of years invested
can be used for this assignment. This requires using the exponentiation operator "**".
Run and test the modified program.
As a further challenge, modify the program to accept the interest rate as an input.