Skip to content
Fetching latest commit…
Cannot retrieve the latest commit at this time.
..
Failed to load latest commit information.
README.md
factorial.cob
run.js

README.md

Factorial Sample

This sample shows the use of user defined functions.

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 in working storage section (without using picture, yet):

data division.
working-storage section.
01 n.

The procedure division has a loop:

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

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.
Something went wrong with that request. Please try again.