Newbie ? about Factorial's
Author Message
Newbie ? about Factorial's

While learning Rexx I have run up against this code that I sure could
use some help in understanding how it works.
/* FROM PAGE 234 */
TRACE ?I
SAY 'Enter a number'
PULL number
SAY number || ' ! is' Factorial(number)
EXIT

Factorial: PROCEDURE
num = ARG(1)
IF num = 1 THEN
RETURN num
RETURN Factorial(num - 1) * num

When I look at the output of the trace instruction I get all messed
up.
The line.....RETURN Factorial(num - 1) * num......if I enter
lets say 4, on the first time through should produce
(4  - 1) * 4 and I come up with 12.But when the return to
.....SAY number || ' ! is' Factorial(number)....is completed
the trace output shows....
>>>               "1"
>F>           "1"
>V>           "2"
>O>           "2"
>>>           "2"
>F>         "2"
>V>         "3"
>O>         "6"
>>>         "6"
>F>       "6"
>V>       "4"
>O>       "24"
>>>       "24"
>F>     "24"
>O>     "4 ! is 24"
>>>     "4 ! is 24"
This has me really screwed up.I fully admit to not being a mental
giant so if you can explain this to me with that in mine I
would sure appreciate it.  Thanks for any help,Dale

Fri, 27 Aug 1999 03:00:00 GMT
Newbie ? about Factorial's

Don't forget that FACTORIAL is a recursive function.   That's somewhat
disguised by the fact that FACTORIAL's recursive call  to itself is
embedded in its RETURN statement:  "RETURN Factorial(num - 1) ..."

When you call FACTORIAL from the main routine with value 4, then it is
calling itself with value 3, then calling itself again with value 2,
then calling itself again with value 1.  The trace is showing your
progress back up the stack, through 1, 1 * 2 = 2, 2 * 3 = 6, and 3 * 4
= 24.

REXX is a great language to do recursion in.  It makes it so easy.

----------

Sent:  Monday, March 10, 1997 9:59 AM
To:  Multiple recipients of list REXXLIST
Subject:  Newbie ? about Factorial's

While learning Rexx I have run up against this code that I sure could
use some help in understanding how it works.
/* FROM PAGE 234 */
TRACE ?I
SAY 'Enter a number'
PULL number
SAY number || ' ! is' Factorial(number)
EXIT

Factorial: PROCEDURE
num = ARG(1)
IF num = 1 THEN
RETURN num
RETURN Factorial(num - 1) * num

When I look at the output of the trace instruction I get all messed
up.
The line.....RETURN Factorial(num - 1) * num......if I enter
lets say 4, on the first time through should produce
(4  - 1) * 4 and I come up with 12.But when the return to
.....SAY number || ' ! is' Factorial(number)....is completed
the trace output shows....
>>>               "1"
>F>           "1"
>V>           "2"
>O>           "2"
>>>           "2"
>F>         "2"
>V>         "3"
>O>         "6"
>>>         "6"
>F>       "6"
>V>       "4"
>O>       "24"
>>>       "24"
>F>     "24"
>O>     "4 ! is 24"
>>>     "4 ! is 24"
This has me really screwed up.I fully admit to not being a mental
giant so if you can explain this to me with that in mine I
would sure appreciate it.  Thanks for any help,Dale

Fri, 27 Aug 1999 03:00:00 GMT
Newbie ? about Factorial's

On Monday, 97/03/10, Dale Winters wrote to All about "Newbie ? about
Factorial'" as follows:

DW> While learning Rexx I have run up against this code that I sure
DW> could use some help in understanding how it works.
DW> /* FROM PAGE 234 */
DW> TRACE ?I
DW> SAY 'Enter a number'
DW> PULL number
DW> SAY number || ' ! is' Factorial(number)
DW> EXIT
DW>
DW> Factorial: PROCEDURE
DW>  num = ARG(1)
DW>  IF num = 1 THEN
DW>   RETURN num
DW>  RETURN Factorial(num - 1) * num
DW>
DW> When I look at the output of the trace instruction I get all
DW> messed up. The line.....RETURN Factorial(num - 1) * num......if I
DW> enter lets say 4, on the first time through should produce (4  - 1) *
DW> 4 and I come up with 12.But when the return to .....SAY number || '

It's not (4 - 1)*4 but Factorial(4 - 1)*4, which is Factorial(3)*4, or
24. The first RETURN, when completed, yields the final result. That's
the hokey way recursive algorithms work, because the first level of
call is the last to return a result.

Regards

Dave
<Team PL/I>

* KWQ/2 1.2i * 3 things occur when you age.. 1) memory goes 2) uh.. um..
--
Please remove the '\$' in the from line before reply via email.
Anti-UCE filter in operation.

Sat, 28 Aug 1999 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages