Page 1 of 2

factorial of a number in rexx using internal procedure

PostPosted: Mon Aug 16, 2010 6:23 pm
by nalinigoli
Hi
/* factorialProgram.rex -- computes the factorial of a number */

/* Usage: exec factorialProgram number */

/* Example: exec factorialProgram 5 */

arg N .

call factorial N

say result

exit 0 /* don't fall through to the PROCEDURE instruction */

/* internal procedure FACTORIAL
* returns factorial of argument N
*/

factorial : PROCEDURE

n = arg( 1 )

if n = 1 then
return 1

return n * factorial( n - 1 )


i have given in my pds member and execcuted using the command in the ispf panel " tso exec rexx.exec(main1) exec"
rexx.exec(main1) is the dataset where i have stored the above rexx code
please help me on how to execute this
if i try this it is showing error in the line exit 0

Re: factorial of a number in rexx using internal procedure

PostPosted: Mon Aug 16, 2010 8:04 pm
by MrSpock
tso exec rexx.exec(main1) exec

Why are you passing the argument 'exec' and not a number?

Also, post some real TRACE command runtime output.

Re: factorial of a number in rexx using internal procedure

PostPosted: Mon Aug 16, 2010 8:30 pm
by NicC
And why are you posting here and not the rexx forum?

Re: factorial of a number in rexx using internal procedure

PostPosted: Mon Aug 16, 2010 8:31 pm
by enrico-sorichetti
And why are you posting here and not the rexx forum?

because he wants suggestions and feedback... what else 8-)

Re: factorial of a number in rexx using internal procedure

PostPosted: Tue Aug 17, 2010 10:46 am
by nalinigoli
[quote="nalinigoli"]Hi

/* main1 rexx*/
/* call factor sub program****/
pull x
say x
arg x
call factor x
say result
exit 0
factor:PROCEDURE
x = arg(1)
if x = 1
then return 1
return x * factor(x-1)

if i try this it is showing error
13 +++ return x * factor(x-1)
6 +++ call factor x
Error running MAIN1, line 13: Bad arithmetic conversion

please help me out

Re: factorial of a number in rexx using internal procedure

PostPosted: Tue Aug 17, 2010 2:50 pm
by NicC
Your trace is not showing the values - only the statements being executed. Try TRACE '?i' for a full interactive trace. SAY the value of x before executing line 13.

Re: factorial of a number in rexx using internal procedure

PostPosted: Tue Aug 17, 2010 3:59 pm
by NicC
What value for x are you testing with? I have tried the code you originally posted and it is giving me good results up to x = 4 so I would sugest that it is ok.

Re: factorial of a number in rexx using internal procedure

PostPosted: Tue Aug 17, 2010 4:07 pm
by NicC
One other thing - the code does not handle invalid input! x < 1 goes into an endless loop!

Re: factorial of a number in rexx using internal procedure

PostPosted: Tue Aug 17, 2010 4:33 pm
by nalinigoli
can u pls give me the exact code u have executed

Re: factorial of a number in rexx using internal procedure

PostPosted: Tue Aug 17, 2010 4:47 pm
by NicC
As I wrote 3 posts before
I have tried the code you originally posted
i.e. I cut and pasted it, loaded it to the mainframe and executed it. Afterwards I prettied it up, added the check for the parm being greater than zero and reformatted it to my own standards but what I executed first is the exact code you originally posted.