Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (50 sloc) 1.15 KB

Local Sample

This sample shows the declaration and use of local variable.

Setup

Install Node.js.

Run

With the command line:

node run factorial.cob

The output:

1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800

factorial.cob declares a variable directly in the implicit procedure division:

local n.

The show-factorial procedure is invoked in a loop, varying the local variable:

procedure division.
perform show-factorial varying n from 1 to 10.

show-factorial procedure calls factorial using using to pass an argument, and giving to save the result. Then, it shows the result using display. Note the declaration and use of a local variable result.

show-factorial.
local result.
perform factorial using n giving result.
display n "! = " result.

factorial procedure invokes itself recursively. It uses a local variable m.

factorial using n.
local m.
if n = 1 then return n.
subtract 1 from n giving m.
perform factorial using m giving m.
multiply n by m.
return m.