Recursion In COBOL

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.