# 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

Thread starter Title Forum Replies Date
Fixed Function @timer[] does not work as required Support 6
Documentation Function parameters Support 0
Documentation Function @DRIVE Missing in Help Lists Support 0
How to? Get name of current LIBRARY function? Support 4
WeekNum function? Support 3
No blank line in TEXT...ENDTEXT in a LIBRARY function Support 8
Ctrl-C from within a library function Support 6
email[string] function Support 10
FUNCTION to count NUMBER of files matching a pattern and specific length/not length Support 4
Using the @instr function Support 5
'}' in a library function? Support 3
Can the argument to RETURN be a function expansion? Support 3
Trouble with @full[] function. Support 3
Quick Function Question Support 17
Quoting @Function parameters? Support 0
Debugging a LIBRARY function Support 10
Documentation @CAPI function summary Support 0
%x\$ in function Support 3
@eval function, interesting results any explanation? Support 3
Function to get interface user-defined name? Support 10
Fixed Help @FUNCTION, missing paragraph Support 8
Variable followed by function constantly failing... Support 5
Call a complex function Support 13
Fixed Cannot use the "Browse..." function in "Find files/text" dialogbox Support 2
Function #IDOW returns only 2 characters Support 10
scrput `` in alias or function Support 2
International characters - TYPE vs %@LINE function Support 5
WAD Nested variable expansion in PDIR's @function[*]? Support 13
In addition to ALIAS /Z and FUNCTION /Z, HISTORY /Z? Support 1
pdir function usage Support 3
FUNCTION with variable number of arguments Support 17
%@REGQUERY function Support 1
Documentation v15 help: Error on @TRIMALL function page Support 0
Why doesn't this @Field function work? Support 0
How to? user function Support 1
Why is this function getting these results in these circumstances? Support 6
WAD function return values with parentheses prevent evaluation of additional functions Support 31
Just an out-of-curiosity question the List command vs. the @Lines function... Support 13
How to? Just verification for what I think I know regarding "Function"... Support 4
How to? need help with "function" Support 10
How to? Function Key With INPUT Support 5
How to? "Function" and "%[xxx]" Support 7
Kind of a out-of-curiosity question re. the "Function" command... Support 5
Simple question about "Function" command... Support 4
Just a curiosity question related to the "Function" command... Support 2
not sure of windows registry function use Support 2
The @urlencode function doesn't seem to URL encode Support 16
issue to have "if" inside function Support 3
is the search-function in VIEW working? Support 2