# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# Function Recursion

#### joshjeppson

Is function recursion not supported?

I seem to be able to define recursive functions, but upon execution I get a TCC: Variable loop error. Even multi-function recursion fails, ie, A > B > A > B ...

The consummate example:
Code:
``````C:\>function factorial=`%@if[%1 gt 1,%@eval[%1 * %@factorial[%@dec[%1]]],1]`
C:\>echo %@factorial[5]
TCC: Variable loop``````

- Josh

On Fri, 18 Jul 2008 17:06:19 -0500, joshjeppson <> wrote:

>Is function recursion not supported?
>
>I seem to be able to define recursive functions, but upon execution I get a TCC: Variable loop error. Even multi-function recursion fails, ie, A > B > A > B ...
>
>The consummate example:
>
>Code:
>---------
>
>C:\>function factorial=`%@if[%1 gt 1,%@eval[%1 * %@factorial[%@dec[%1]]],1]`
>C:\>echo %@factorial[5]
>TCC: Variable loop

The problem is that both IF consequents are evaluated regardless if the value of
IF's antecedent. So, even when %1 EQ 1 (and thereafter), %@factorial[%@dec[%1]]
is evaluated.

On Fri, 18 Jul 2008 17:06:19 -0500, joshjeppson <> wrote:

>Is function recursion not supported?

Yes, to some level, but it's cumbersome. Here's an example (albeit very
contrived) of a UDF which may call itself.

v:\> function abv `%@execstr[iff %1 GE 0 then & echo %1 & else & echo
%@abv[%@eval[%1 * -1]]]`

v:\> echo %@abv[3]
3

v:\> echo %@abv[-3]
3

On Fri, 18 Jul 2008 17:06:19 -0500, joshjeppson <> wrote:

The problem is that both IF consequents are evaluated regardless if the value of IF's antecedent. So, even when %1 EQ 1 (and thereafter), %@factorial[%@dec[%1]] is evaluated.

It can be prevented like this:

v:\> function factorial=`%@if[%1 gt 1,%%@eval[%1 * %%@factorial[%@dec[%1]]],1]`

v:\> echo %@factorial[5]
120

v:\> echo %@factorial[6]
720

It can be prevented like this:

v:\> function factorial=`%@if[%1 gt 1,%%@eval[%1 * %%@factorial[%@dec[%1]]],1]`

v:\> echo %@factorial[5]
120

v:\> echo %@factorial[6]
720

The lack of short circuit evaluation in %@if, if and iff seems to trip me up every so often. Although, this time it is a little different in that the conditional wasn't the problem but the consequents.

Thanks again Vince, as always!

- Josh

You can't recurse more than 16 levels.

Rex Conn
JP Software
----- Original Message -----
From: joshjeppson
To: [email protected]
Sent: Friday, July 18, 2008 6:06 PM
Subject: [Support-t-332] Function Recursion

Is function recursion not supported?

I seem to be able to define recursive functions, but upon execution I get a TCC: Variable loop error. Even multi-function recursion fails, ie, A > B > A > B ...

The consummate example:

Code:

C:\>function factorial=`%@if[%1 gt 1,%@eval[%1 * %@factorial[%@dec[%1]]],1]`
C:\>echo %@factorial[5]
TCC: Variable loop
- Josh

You can't recurse more than 16 levels.

Rex Conn
JP Software

In my original function I actually only needed a single level of recursion. The factorial function was contrived because of its ubiquity as an example of recursion and to demonstrate that in the simplest case, recursion, as I was using it, was not working.

Between 32 levels of batch nesting, 22 levels of gosub nesting and 16 levels of function nesting, I think most of my batch file and command line recursion needs will be met.

- Josh

[FOX] Ultimate Translator
Translate