Functions in COBOLEdit
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.
*> 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".
Copy and paste the code above into one of the following free online development environments or use your own Functions compiler / interpreter / IDE.
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.
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.