tailcall(n) Tcl Built-In Commands tailcall(n)

```

```

# [NAME](#NAME)

tailcall - Replace the current procedure with another command

# [SYNOPSIS](#SYNOPSIS)

**tailcall command** ?*arg ...*?

```

```

# [DESCRIPTION](#DESCRIPTION)

The **tailcall** command replaces the currently executing procedure, lambda application, or method with another command. The *command*, which will have *arg ...* passed as arguments if they are supplied, will be looked up in the current namespace context, not in the caller's. Apart from that difference in resolution, it is equivalent to:

```
return [uplevel 1 [list command ?arg ...?]]
```

This command may not be invoked from within an **uplevel** into a procedure or inside a **catch** inside a procedure or lambda.

# [EXAMPLE](#EXAMPLE)

Compute the factorial of a number.

```
proc factorial {n {accum 1}} {


    if {$n < 2} {


        return $accum


    }


    tailcall factorial [expr {$n - 1}] [expr {$accum * $n}]
}
```

Print the elements of a list with alternating lines having different indentations.

```
proc printList {theList} {


    if {[llength $theList]} {


        puts "> [lindex $theList 0]"


        tailcall printList2 [lrange $theList 1 end]


    }
}
proc printList2 {theList} {


    if {[llength $theList]} {


        puts "< [lindex $theList 0]"


        tailcall printList [lrange $theList 1 end]


    }
}
```

# [SEE ALSO](#SEE_ALSO)

[apply(n)](/man/os/tcl/apply.n), [proc(n)](/man/os/tcl/proc.n), [uplevel(n)](/man/os/tcl/uplevel.n)

# [KEYWORDS](#KEYWORDS)

call, recursion, tail recursion

8.6 Tcl

---

Document: tailcall(n) from tcl 9.1.0-r0 (Chainguard OS)
Source: https://downloads.sourceforge.net/project/tcl/Tcl/9.1.0/tcl9.1.0-src.tar.gz
