# 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

#### vefatica

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.

#### vefatica

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

#### vefatica

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

#### joshjeppson

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

#### rconn

You can't recurse more than 16 levels.

Rex Conn
JP Software
----- Original Message -----
From: joshjeppson
To: rconn@jpsoft.com
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

#### joshjeppson

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

Replies
6
Views
128
Replies
11
Views
224
Replies
6
Views
262
Documentation Function parameters
Replies
0
Views
207
Replies
0
Views
216
Replies
4
Views
627
Replies
3
Views
448
Replies
8
Views
785
Replies
6
Views
907
Replies
10
Views
2K
Replies
4
Views
1K
Replies
5
Views
991
Replies
3
Views
946
Replies
3
Views
1K
Replies
3
Views
2K
Replies
17
Views
2K
Replies
0
Views
822
Replies
10
Views
2K
Documentation @CAPI function summary
Replies
0
Views
703
Replies
3
Views
1K
Replies
3
Views
2K
Replies
10
Views
2K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
13
Views
2K
Replies
2
Views
1K
Replies
10
Views
3K
Replies
2
Views
938
Replies
5
Views
1K
Replies
15
Views
2K
Replies
13
Views
2K
Replies
1
Views
847
Replies
3
Views
2K
Replies
17
Views
2K
Replies
1
Views
1K
Replies
0
Views
1K
Replies
0
Views
1K
How to? user function
Replies
1
Views
1K
Replies
6
Views
2K
Replies
31
Views
6K
Replies
13
Views
2K
Replies
4
Views
1K
Replies
10
Views
2K
Replies
5
Views
2K
Replies
7
Views
2K
Replies
5
Views
1K
Replies
4
Views
1K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
16
Views
5K