Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Tail call
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Syntactic form== A tail call can be located just before the syntactical end of a function: <syntaxhighlight lang="javascript"> function foo(data) { a(data); return b(data); } </syntaxhighlight> Here, both <code>a(data)</code> and <code>b(data)</code> are calls, but <code>b</code> is the last thing the procedure executes before returning and is thus in tail position. However, not all tail calls are necessarily located at the syntactical end of a subroutine: <syntaxhighlight lang="javascript"> function bar(data) { if (a(data)) { return b(data); } return c(data); } </syntaxhighlight> Here, both calls to <code>b</code> and <code>c</code> are in tail position. This is because each of them lies in the end of if-branch respectively, even though the first one is not syntactically at the end of <code>bar</code>'s body. In this code: <syntaxhighlight lang="javascript"> function foo1(data) { return a(data) + 1; } </syntaxhighlight> <syntaxhighlight lang="javascript"> function foo2(data) { var ret = a(data); return ret; } </syntaxhighlight> <syntaxhighlight lang="javascript"> function foo3(data) { var ret = a(data); return (ret == 0) ? 1 : ret; } </syntaxhighlight> the call to <code>a(data)</code> is in tail position in <code>foo2</code>, but it is '''not''' in tail position either in <code>foo1</code> or in <code>foo3</code>, because control must return to the caller to allow it to inspect or modify the return value before returning it.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)