Recursion is rarely used in Cobol. It is also boring to see this example in IBM book.
Identification Division.
Program-id. recutestRecursive.
**************************************** ***
* Recursive Test
**************************************** **
Environment Division.
Data Division.
Working-storage section.
01 numb PIC 9 (4) value 5.
01 fact PIC 9 (8) value 0.
Local-storage section.
01 num PIC 9 (4 ).
Procedure division.
Move numb to num
If numb = 0 then
Move 1 to fact
Else
Subtract 1 from numb
Compute numb = numb-1
Call 'recutest'
Multiply num by fact
Compute fact = fact * num
End-if
Display num '! = 'Fact
Goback.
End program recutest.
CodeIf there is no highlight, you will be watching it. The Code has passed the test and can be run.
There are two key points:
(1) Add the recursive keyword to the end of the program-ID segment.
(2) Use the local-storage section to save each entryProgramThe value of num.
For example, for each call to recutest, num in local-storage will save their respective values. In working-storage, numb only has one
Value. This is the key to implementing recursion in Cobol.