[Question]
  [
Write a function, `f`, that takes in a positive integer and returns a function.
The new function returned should be identical to `f`. However, when the "termination call" happens, `f` should instead return the sum of all integers passed. 
For example, `g=f(4)` (if `f` is the first function) should set `g` to another function. `h=g(3)` will do the same. However, when you call `h` with no arguments (see below for details), it should output 7, as that is the sum of the previous function arguments. Put another way, `f(3)(4)() == 7`. 
Do note this is not the same as `f(3,4)()`.
"Termination call" is one of the following options (your choice):
* call w/o arguments
* null as argument
* any non-positive value
Arbitrary amount of function calls should be supported, there is no predefined limit.
It's guaranteed that total sum will not be bigger than 1'000.
We can assume that there is at least one call is made prior to "termination call".
Your code should not use static, per-program variables, so it should be possible to run the experiment multiple times in the same runtime and observe exactly the same behavior.
Examples:
```
f(1)() == 1
f(4)(2)(7)() == 13
f(4)(2)(7)(5)(2)() == 20
```
  
[Answer]
## JavaScript (ES6), 18 bytes
```
f=n=>m=>m?f(m+n):n
```
Pass a falsy value to retrieve the sum. Zeros could be allowed for a cost of 2 bytes.
[Try it online](https://tio.run/nexus/javascript-babel-node#@59mm2drlwtE9mkaudp5mlZ5/5Pz84rzc1L1cvLTNRTSNAw1NYw0NYw1NXRNNDU0FTT/AwA)
**Ungolfed:**
```
f = function(n) {
    return function(m) {
        if (m) {
            return f(m+n);
        } else {
            return n;
        }
    }
}
```
[Answer]
## [Haskell](https://www.haskell.org/) (GHC), 118 bytes
This is 98 bytes for the code and 20 bytes for the GHC compiler flag `-XFlexibleInstances`, which enables a type system extension.
```
class F a where f::Int->a
instance F(()->Int)where f n()=n
instance F a=>F(Int->a)where f=(f.).(+)
```
This defines a "function" `f`, which can be called with an arbitrary number of integers followed by the unit `()`, after which it returns an integer.
Type annotations are required.
[Try it online!](https://tio.run/nexus/haskell#TY3LCsIwFET3fsVAN/ci6UJxE0igC1MK4s4PiCXBQL1IU1AQv72@Irg6w3CGuasKu2bfHpp2CzeEWzoOoZM8eelDRqUecz/4nOHgcT2FMSBq3cmkrF@k4sERsbKvlosCITbyJ8Ab6@i7@0mGYs01LXk@@yQwuIxJJlCk9eeCaVO4KmRojXeanw "Haskell – TIO Nexus")
## Explanation
Forcing Haskell's strict type system to allow this requires some magic, namely, enabling the GHC extension for flexible typeclass instances.
How this works is that `f` is a parametrically polymorphic function restricted by a type class constraint: its type is `F a => Int -> a`.
This means that `f` takes an integer and returns a value of type `a`, for any type `a` that belongs to the typeclass `F`.
`F` is just the name of the typeclass that provides the function `f`; it's declared on the first line.
The next two lines are two instances of `F` for different types `a`.
The second line states that the type of functions from `()` to integers belongs to `F` (where `()` is the unit type whose only member is the value `()`), and the implementation is `f n () = n`; the function returns its first argument.
The last line states that if `a` belongs to `F`, then so does the type of functions from integers to `a`: from a function `f :: Int -> a` we can generate another function `f :: Int -> Int -> a`.
The implementation is `f m n = f (m+n)` (the code uses combinators to make it shorter), where the `f` on the left is the new one, and the `f` on the right is the old one.
This essentially gives `f` a new integer argument, which is added to the next one.
Multiple arguments are summed together like this:
```
  f  a1   a2   a3   a4   a5  ()
= f (a1 + a2)  a3   a4   a5  ()
= f (a1 + a2 + a3)  a4   a5  ()
= f (a1 + a2 + a3 + a4)  a5  ()
= f (a1 + a2 + a3 + a4 + a5) ()
=    a1 + a2 + a3 + a4 + a5
```
The `f` on each line has a different type.
Haskell functions are curried automatically, so if you give `f` only integers, you get a function.
[Answer]
# Python 2, ~~42~~ ~~41~~ 36 bytes
This solution will never have an overflow, since Python supports arbitrary-precision integers. Zero is the "special value".
```
f=lambda n:lambda m:m and f(m+n)or n
```
[**Try it online**](https://tio.run/nexus/python2#@59mm5OYm5SSqJBnBWXkWuUqJOalKKRp5GrnaeYXKeT9LyjKzCsBChhqahgZaWrommpqGBoAsYmmhoHmfwA)
**Ungolfed:**
```
def f(n):
    def g(m=''):
        return f(m+n)if m<''else n
    return g
```
[Answer]
## Mathematica, 25 bytes
```
f[x_]@y_=f[x+y]
f[x_][]=x
```
[Try it online!](https://tio.run/nexus/mathics#@58WXREf61AZbwtkaFfGcoH50bG2Ff8DijLzShQcFNKijWOjTWKjDYEIyIr9DwA "Mathics – TIO Nexus") (Using Mathics.)
It's possible to do three bytes less by porting the JavaScript answer, but I wanted to present a more idiomatic Mathematica solution. The `@` is just a bit of syntactic sugar, which makes the solution equivalent to:
```
f[x_][y_]=f[x+y]
f[x_][]=x
```
So yeah the idea is that in Mathematica you can't just define a function `f[x_]` but you can directly attach a value to a more complicated expression containing `f`, e.g. `f[x_]` being passed another argument. By setting up two definitions for this, we can get the desired behaviour:
* The first definition collapses one `f[x][y]` call into `f[x+y]`, thereby consuming one "call" and adding up the arguments inside. This rule applies until we're left with `f[sum][]`.
* The second definition unpacks this final case by defining the entire thing to evaluate to `sum`.
[Answer]
# C, ~~62~~ 58 bytes, borderline competing
Saved 4 bytes thanks to Kevin! (Still not removing typedef because it's something needed in order to be called.)
```
typedef(*(*B)(_))(_);q;f(x,o,_){x=x?(q+=x,f):(x=q,q=0,x);}
```
The function to call is `f`; you stop calling it and get the result by calling it with a non-positive number like `0`. **[Try a test harness online!](https://tio.run/nexus/c-gcc#lZDRaoNAEEXf/YohpTCr0qgJFNxsJX5HQUp1qdhqtrWwJeTb7YzZBDS@ZFBw5u49c9fhoW7fP3/LCnY/fVl3Tx8v3noN/d@hKiuN/l5gIaRrYY9@Pg6MzH2NTViIowlUs4uzKG1kM35hoUxoVERiirnQ8nQPsWNmp4w0DmsCBpuMUWlHsCvJWZ1doz2brbIZUiobagpgXRgryPr1Vrcojh5QHb7rtte40pjEAumJBCh4LF/bVYi0jN9RY5FVISe@JIYAYhjr4oPzcHZSYxxFArcJkTab58kmPueBK155bRYHl6FGICZQrhuZa5vcjKecBR9H85YdnHhsZhfjCAEt4zuTe/orZiJZT8PwDw)**
So, as far as I can tell, the only way to "curry" functions that have multiple return types is to do one of the following:
1. Cast the result to a function to tell the compiler you wish to call the result again;
2. or create a `union`/`struct` type that has an `int` and function/self-referential subtypes.
I tried doing (2), but it seemed a bit against the spirit of the question and, quite frankly, nigh undoable. Thus, in keeping with the spirit of the challenge, I have opted for option (1). This requires casting each returned function into a function, that it may be used.
This "currying" syntax looks a bit odd, but is quite similar. To emulate `f(21)(1)`, one would have to write `((B)((B)f(21))(1))(0)`. I defined the `B` type to be a function that takes an integer and returns a pointer to a function that takes an integer. Expanded, this looks like:
```
   ( (B)( (B) f(21) )(1) )(0)
//            f(21)            - call f with 21
//        (B)                  - cast to B, a function pointer
//      (           )(1)       - call with 1
//   (B)                       - cast to a function pointer
// (                     )(0)  - call with 0
```
[Answer]
# C++, 72 bytes
```
#define O(P)operator()(P){return{P+a};}int
struct F{F O(int(m))O()a;}f;
```
This defines a type `F` which acts as the requested function, and a variable `f` of that type to invoke. It's valid as of C++11 and works with online versions of GCC, clang, icc and VC++.
Usage:
```
int main() {
  return f(1)(2)(3)(); // returns 6
}
```
Explanation:
After preprocessing and reformatting, it looks like:
```
struct F {
  F operator()(int(m)) { return{int(m)+a}; }
  int operator()() { return {+a}; }
  int a;
} f;
```
This would normally be written:
```
struct F {
  F operator()(int m) { return {m+a}; }
  int operator()() { return a; }
  int a;
} f;
```
`return a;` and `return {+a};` do the same thing, as unary `+` doesn't change the value, and redundant braces around the return value are allowed. `int m` and `int(m)` do the same thing, as redundant parentheses around a variable name are allowed, including function parameters. `return {m+a};` and `return {int(m)+a};` do the same thing, as a cast of `m` from `int` to `int` does not change its value. These changes get the two `operator()` overloads closer in syntax, allowing a single macro definition to be invoked twice. Picking the right order for the three members allows the first word of the next line (`int`) to be included in the macro definition as well.
[Answer]
# Ruby, 23 bytes
```
f=->n{->m{m ?f[n+m]:n}}
```
Usage:
```
f[1][2][3][nil]
=> 6
```
[Answer]
# C, ~~104~~ 96 bytes
```
#define a(i)s(i)|b
#define b(i)u(i)|c
#define c(i)u(i)|b
b,c,d;s(i){b=c=i;i=d;}u(i){c=b+=i;i=d;}
```
Uses the method from the link that @JulianWolf shared. Last argument must be 0.
[Try it online!](https://tio.run/nexus/c-gcc#PYuxCsMwDER3f0VICUjUQ9vQyfhjLCkBDTWlSScn355YBXc4uPeOOy4yzZqnLoHiUrORa4oqfk3xX3FT5Mizl2CXQpGjBo0SdhsLR7o2cbySZsDy/mheZ@gH6QbpvUvwRBgRHgg3NLz/@mjosP5O "C (gcc) – TIO Nexus")
[Answer]
# Math.JS, 38 Bytes
```
f(x)=i(x,0)
i(x,y)=x<0?y:j(z)=i(z,y+x)
```
Call it with `f(number_a)(number_b)(...)(negative_number)`
If we're allowed to specify the initial call, 12 bytes (`f(x)=i(x,0)\n`) can be dropped, and it can be called with `i(number_one,0)(number_two)(...)(negative_number)`
[Try it!](http://a-ta.co/math/f(x)%3Di(x%2C0)%0Ai(x%2Cy)%3Dx%3C0%3Fy%3Aj(z)%3Di(z%2Cy%2Bx)%0A%0Aprint(%0A%20%20%20%20f(3)(4)(5)(-1)%0A)%0A)
## Explanation
$$
\begin{align}
f(x) & = i(x, 0) \\
i(x, y) & = \begin{cases}
y, & \text{if } x < 0 \\
j(z) = i(z, y+x), & \text{otherwise}
\end{cases}
\end{align}
$$
As shown above, `f(x)` simply calls `i(x,0)`, then, `i(x,y)` returns the value of `y` if `x` is less than 0, or the function `j(z)=i(z,x+y)` otherwise.
[Answer]
## C, ~~232~~ 206 bytes
```
#include<string.h>
#include<stdlib.h>
#define f(X)s(""#X)?0:g
#define g(X)u(""#X)?0:h
#define h(X)u(""#X)?0:g
g=0,h=0;s(char*s){g=h=atoi(s);return 0;}u(char*s){char*a=strlen(s)?s:"0";g=h+=atoi(a);return 0;}
```
This can probably be golfed significantly, but should serve as a proof of concept that C can be used, without any language extensions\*, to solve this problem by calling without arguments rather than with a magic value.
\* @hvd has noted that, while this works out of the box using gcc, some of the behavior is not defined in the C standard, meaning that this may not be portable. Use at your own risk!
Ungolfed:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define f(X) start("" #X) ? 0 : f0
#define f0(X) update("" #X) ? 0 : f1
#define f1(X) update("" #X) ? 0 : f0
long f0 = 0;
long f1 = 0;
int start(const char *s) {
    f0 = f1 = strtol(s, NULL, 10);
    return 0;
}
int update(const char *s) {
    const char *a = strlen(s) ? s : "0";
    f0 = f1 += strtol(a, NULL, 10);
    return 0;
}
int main() {
    printf("f(1)()          -> %ld\n", f(1)());
    printf("f(1)(2)(0)(3)() -> %ld\n", f(1)(2)(0)(3)());
    printf("f(1)(-2)(3)()   -> %ld\n", f(1)(-2)(3)());
    printf("f()             -> %ld\n", f());
    return 0;
}
```
Compiling and running with `gcc arbitrary-length-currying.c -o arbitrary-length-currying && ./arbitrary-length-currying` outputs (after some warnings)
```
f(1)()          -> 1
f(1)(2)(3)(0)() -> 6
f(1)(-2)(3)()   -> 2
f()             -> 0
```
[Answer]
# 8086 machine code, 27 bytes
```
00000000  bb 00 00 85 c0 74 13 01  d8 be 00 01 89 e7 47 47  |.....t........GG|
00000010  57 b9 1b 00 f3 a4 5b 89  47 01 c3                 |W.....[.G..|
0000001b
```
This machine code must be at address 0x100, and assumes the tiny code model (cs=ds=es=ss). The function location can be changed without costing extra bytes, though. Putting it at offset `0` would save a byte (`xor si,si` instead of `mov si, 0x100`)
**Required calling convention**
This assumes the caller has pre-allocated at least 27 bytes on the stack. It takes a number in `ax`, and returns a function pointer in `bx`. Calling this pointer with `ax=0` terminates the chain, and returns the sum in `bx`.  
So for the first call:
```
mov bp, sp
sub sp, 28
mov ax, number_to_add
call function
; new function pointer in bx
```
Then, for each subsequent call:
```
sub sp, 28
mov ax, number_to_add
call bx
; new function pointer in bx
```
To terminate:
```
mov ax, 0
call bx
; result in bx
mov sp, bp
```
---
### Ungolfed (commented disassembly of the machine code):
```
00000000  BB0000            mov bx,0x0      ; 0 is replaced after copying
00000003  85C0              test ax,ax
00000005  7413              jz 0x1a         ; if(ax==0) ret (with value in bx)
00000007  01D8              add ax,bx       ; arg += total
00000009  BE0001            mov si,0x100    ; address of the original: ds:0x100
0000000C  89E7              mov di,sp
0000000E  47                inc di
0000000F  47                inc di          ; dst = sp+2 = above return address
00000010  57                push di
00000011  B91B00            mov cx,0x1b
00000014  F3A4              rep movsb         ; copy the function code.
00000016  5B                pop bx            ; bx = start of copy destination
00000017  894701            mov [bx+0x1],ax   ; update total in the copied code
0000001A  C3                ret               ; with bx = function pointer
```
After calling this with non-zero AX, `bx = sp` and the buffer is filled with a modified copy of the machine code from `function`. The 16-bit immediate in the first instruction holds the total. (It's written by the last instruction before the `ret`.)
`push di` / `pop bx` could be replaced with `mov bx, di` (before `rep movsb`), making it simpler but no savings.
Requiring the caller to pass a pointer to the dst buffer in `di` would save 4 bytes vs. calculating it relative to `sp`.
Making the function start address the same as the function size would save a byte (`mov cx, si`).
[Answer]
# Scala, 58 chars
```
case class f(n:Int){def apply(m:Int)=f(n+m)
def apply()=n}
```
[Try it online](https://ideone.com/RgGL2q)
### Ungolfed:
```
case class f(n:Int){
  def apply(m:Int)=f(n+m)
  def apply()=n
}
```
### Explanation:
This code defines a `case class` called f with a constructor taking an int. Definind a case class which will generate the equals, hashcode, toString and copy methods, and a companion object with the same name to enable object creation without the `new` keyword.
This class has an overloaded apply method:
One takes another integer to add and creates a new object with the updated sum, and one without arguments to get the sum.
In Scala, any object with an apply method can be called like a method, that is `o.apply(x)` can be written as `o(x)`. This is used in the standard libary for arrays, lists, maps and the `Function1` trait implemented by anonymous functions
[Answer]
# C (GCC), 83 bytes
My first C golf! There are a couple of other C solutions, but this one's a bit different. Preprocessor use is purely cosmetic. This approach was first discussed in Conor O'Brien's answer [here](https://codegolf.stackexchange.com/a/117049).
```
#define r union r
t=0;r{int v;r(*f)();};r e;r f(a){t+=a;e.v=a?f:t;t*=a>0;return e;}
```
The terminal value is zero. The return value is a union, so to call the result, use field `f`, and to access the final value, use field `v`, e.g.
```
f(1).f(2).f(3).f(0).v
```
[Try It Online](https://tio.run/##fY5BDsIgEEXX9hSTGhOolVC7c4JexA2hoCycGqRsmp69UmOMC@Ni3ur9/8fsLsbMniLcQ6ZjpqdHBHPVAaoahBAc53VnnScLAQbyPUEoopIYxiWWMLDKccZxwgA2n2Oaj3GrNFqRlD65Q8RYKX3MERuHQNmaXpM37Yml3nccxmL1fqDcdGcq61zTcOHYfkG7QHKROP7w2o/y32uklF/CND8B)
## Limitations
A global variable holds the running total. While this is explicitly disallowed, the submission does support repeated invocations (the total is reset in the terminal call), which seems to be the reason for the ban on global state.
A pointer to `f` is stored to the returned union through the `int` member, so this is clearly not portable. I'm not sure if this works on GCC on all platforms or just on Linux or just on x86 or just with ELF or... If anyone knows any details about this, please comment or send a message!
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~48~~ ~~47~~ ~~46~~ 44 32 bytes
```
∇r←(a f)x
r←⍎'(a+x)f'↓⍨-0=x
∇
0f
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob2pm/qO2CQb/H3W0FwEZGokKaZoVXCDmo94@dY1E7QrNNPVHbZMf9a7QNbCt4AKq4zJI@@@mAFShYJDGBTQBzNRwUzDUVDBA8CECRkCx/wA "APL (Dyalog Classic) – Try It Online")
Terminates by passing in zero . Call syntax: `((0 f 1) 2) 0`
-15 bytes thanks to @ngn
Requires `⎕IO←0`
Any golfing tips are welcome!
[Answer]
# [Python 3](https://docs.python.org/3/), 39 bytes
```
class f(int):__call__=lambda s,o:f(s+o)
```
[Try it online!](https://tio.run/##K6gsycjPM/7/PzknsbhYIU0jM69E0yo@PjkxJyc@3jYnMTcpJVGhWCffKk2jWDtf839BEVCFRpqGiaaGkaaGuaaGKZhhoKnJhS6l@R8A "Python 3 – Try It Online")
Not the shortest ([by 3 bytes](https://codegolf.stackexchange.com/a/117022/87681)), but it's different from all previous Python approaches. It works by making a callable `int` subclass. Termination call is optional, but requires a `0` argument if used.
[Answer]
# [Dyvil](https://github.com/dyvil/dyvil), 34 bytes
```
infix int apply(i:int,j:int=0)=i+j
```
**Usage**:
```
0() // = 0
0(1)() // = 1
0(1)(2)() // = 3
```
The trailing `()` can be omitted.
**Explanation**:
Defines a juxtaposition operator that takes two ints and adds them. The parameter `j` has the default value `0` to support the call without arguments. The `0` in the examples above is not the name, but a literal.
[Answer]
## C#, 62 bytes
```
dynamic f(int n)=>(System.Func<int,dynamic>)(m=>m<0?n:f(n+m));
```
To end the call pass in a negative number e.g.
```
f(1)(2)(3)(-1) == 6
```
[Answer]
# Pyth, 19 bytes
```
DhdDebR?bh+dbdR$end
```
[Try it online!](https://tio.run/nexus/pyth#@@@SkeKSmhRkn5ShnZKUEqSSmpfyv6AoM69EIyM1MUXDWFPDVFPDTFPDLz8vVVPzPwA "Pyth – TIO Nexus")
I'm impressed that Javascript beats Pyth, but then again Pyth is not quite designed to be passing functions.
[Answer]
## Perl 5, 36 bytes
```
sub f{my$n=pop;sub{@_?f($n+pop):$n}}
```
---
```
say f(1)->(); # 1
say f(1)->(2)->(3)->(); # 6
```
[Answer]
# [Brain-Flak](https://github.com/Flakheads/BrainHack), 6 bytes
Actually I just noticed that since the [ToS is a valid return format](https://codegolf.meta.stackexchange.com/a/8507/48198) popping the *0* is not really needed which saves 2 bytes: 
```
({{}})
```
[Try it online!](https://tio.run/##SypKzMzLSEzO/v9fo7q6tlbz////xkb/dQ2N/hsZ/jf8bwAA "Brain-Flak (BrainHack) – Try It Online")
## Original submission(s), 8 bytes
Uses `0` as the special value:
```
({{}}{})
```
[Try it online!](https://tio.run/##SypKzMzLSEzO/v9fo7q6tra6VvP////GRv91DY3@Gxn@N/xvAAA "Brain-Flak (BrainHack) – Try It Online")
### Explanation
Given the arguments *a1*, *a2*, … , *an*, *0* the stack initially looks like this:
                                                       *an*
                                                       *⋮*
                                                       *a2*
                                                       *a1*
                                                       *0*
The code then goes on, pops every *ai*, accumulates them, pops the *0* adds them and pushes the result:
```
(      )  -- push the following value:
 {  }     --   while ToS ≠ 0 (sums the runs):
  {}      --     pop 1 element
     {}   --   pop the remaining 0 & add it
```
---
### Alternative solutions, 8 bytes
Instead of popping the *0* and adding it to the sum, we can also swap stacks since the right one is initially empty:
```
({{}}<>)
```
[Try it online!](https://tio.run/##SypKzMzLSEzO/v9fo7q6ttbGTvP////GRv91DY3@Gxn@N/xvAAA "Brain-Flak (BrainHack) – Try It Online")
Using the `-r` flag the *0* is on the top of the stack, so we could pop it first:
```
({}{{}})
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/X6O6trq6tlbz/39jIwVdQyMFI0MFQwWD/7pFAA "Brain-Flak – Try It Online")
```
{}({{}})
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/v7pWo7q6tlbz/39jIwVdQyMFI0MFQwWD/7pFAA "Brain-Flak – Try It Online")
[Answer]
# Java 8, 111 bytes
A lambda from `int` to a function. Calls are terminated by passing zero.
```
i->new java.util.function.Function<Long,Object>(){int t=i;public Object apply(Long x){t+=x;return x<1?t:this;}}
```
[Try It Online](https://tio.run/##nVA7T8MwEJ6dX3GjLVoLJGAgTRALEhKIoSNicBM3XHCdyL6UVFF@e0iIKyQGXtPd95LuvlLt1bKqtS3z16FuNgYzyIzyHh4UWugiFkhPisaxRasMlGNKNoRGbhubEVZW3oZldWdJF9otvjXdV7ZYwOOm1BmlKWSNc6jzmzyHZMBlavXbz/GQ5qJDS0AJxuHUWQBV1@bAJyu0oqOTpI2dpsZZaFdn13RFL@jjvh9YHH39cl9hDruxAL4mh7Z4egblCi@mPtj64EnvZNWQrEeRjOUjyzj/9cNi8v898I8E@2xWzn2cB0UEfGFm4ogvZ3yEpx9QxBHroz4a3gE)
## Ungolfed
```
i ->
    new java.util.function.Function<Long, Object>() {
        int t = i;
        public Object apply(Long x) {
            t =+ x;
            return x < 1 ? t : this;
        }
    }
```
Due to an interesting interplay between byte counts of primitive and object integer types, intermediate functions take a `Long` as a parameter, but the total is stored in and returned as an `int`.
## Limitations
The returned function is mutated when called, so, for instance, it's not possible to obtain two integer results without two calls of the lambda. However, the functions returned by separate calls to the lambda are fully independent, so it seems the challenge requirements are met.
Liberal casting is required when constructing expressions that use the lambda. See the TIO for a usage example.
[Answer]
# [Rust](https://www.rust-lang.org/), 134 bytes
```
|x|{fn b(a:i32,n:i32)->Box<dyn std::any::Any>{if n>0{Box::new(Box::new(move|x|b(a+n,x))as Box<dyn Fn(_)->_>)}else{Box::new(a)}}b(0,x)}
```
[Try it online!](https://tio.run/##nY7LrsIwDET3@YpcVrZuQTzExoVIsOA3UKCpVKm4qElpq5BvL2HBo1s2lmXPnJm6sW7IWV50wYC@NE7mciuHe3f38XwCTcVqmfBz4lTtq26T9Syty4g090Q77pUvcslq7uOXiE0L7@VS3UxERcw/Jx2itvKFODAcI/GoMJjSmo9ZYwgnmEd5GFLRWDNOS8W1LtiV/AcTHyYJ5LDCWVa1fNbWEW2@Asaln1WVApw13Nb6Coiw/tEJ08XIGeUjcCqCEMMD "Rust – Try It Online")
In your face, strict type system! Terminates if the input is negative. You do have to downcast the result after every call though (see tio). I took a similar approach to the Math.js solution.
ungolfed:
```
|x| {
        fn b(a: i32, n: i32) -> Box<dyn std::any::Any> {
            if n > 0 { 
                Box::new(Box::new(move |x| b(a + n, x)) as Box<dyn Fn(_) -> _>)
            } else {
                Box::new(a)
            }
        }
        b(0, x)
    }
```
Rust's strong type system makes this almost impossible. Fortunately, trait objects come to the rescue. Trait objects erase the type and all associated data except for the trait's implementation. In this instance, I use the `Any` trait, which allows downcasting back into a concrete type. There are some wrinkles because you can't explicitly name the type of closures, so I have to cast the closure into a `Fn` trait object and then recast it into an an `Any` before use.
[Answer]
# [Aussie++](https://github.com/zackradisic/aussieplusplus/), ~~98~~ 96 bytes
-2 because I remembered the angle brackets aren't necessary for a single-statement `YA RECKON` body.
```
THE HARD YAKKA FOR f IS(x)<THE HARD YAKKA FOR g IS(y)<YA RECKON y ?BAIL f(x +y);BAIL x;>BAIL g;>
```
Tested in commit 9522366. For the final call, give it `BUGGER ALL` or `YEAH, NAH!` instead of a number.
The general structure is something like
```
function f(x) {
    function g(y) {
        if (y) return f(x + y);
        return x;
    }
    return g;
}
```
Since Aussie++ doesn't have function expressions or anonymous functions yet, it is necessary to put the function declaration and return as separate statements.
[Answer]
## PHP7.4 (45 chars)
The superglobal function **[$\_ENV](https://www.php.net/manual/en/reserved.variables.environment.php)** will return an adder function continuously.  
After the operations, when no argument is provided **$\_ENV** will return the result.  
```
$_ENV=fn($a)=>fn($b=-1)=>~$b?$_ENV($a+$b):$a;
```
Usage :
```
var_dump($_ENV(1)() == 1);                 // bool(true)
var_dump($_ENV(4)(2)(7)() == 13);          // bool(true)
var_dump($_ENV(4)(2)(7)(5)(2)() == 20);    // bool(true)
```
**Info**: Overriding a [superglobal](https://www.php.net/manual/en/language.variables.superglobals.php) like *$\_ENV* variable isn't recommended.
[Answer]
# [Perl 6](http://perl6.org/), 31 bytes
```
sub f(\n){->$m?{$m??f n+$m!!n}}
```
[Answer]
## [Python](https://www.python.org/]), 61 bytes
```
f=lambda n="":0if n==""else lambda m="":n if m==""else f(n+m)
```
Much longer than the other Python version, but uses the no-argument syntax rather than a magic number. Can probably be improved upon.
If it can be assumed that "at least one call will be made before the termination call", this can be reduced to **44 bytes**:
```
f=lambda n:lambda m="":n if m==""else f(n+m)
```
[Answer]
## [Julia](https://julialang.org/) v0.5+, 52 bytes
```
type F n end
F()=0
(f::F)()=f.n
(f::F)(x)=(f.n+=x;f)
```
Call as `F`. This could probably be made a lot shorter by adopting a less OO method, but I always like getting the chance to use this idiom.
If it can be assumed that "at least one call will be made before the termination call", the second line can be removed to save 6 bytes.
[Answer]
# [Julia 0.5](http://julialang.org/), 18 bytes
```
!n=k->k>0?!(n+k):n
```
[Try it online!](https://tio.run/nexus/julia5#@6@YZ5uta5dtZ2CvqJGnna1plfc/N7FAo6AoM68kJ08nmotTQ1FTw1BTw0ATwjTR1DDS1DDHImAKZgCFYzX/AwA "Julia 0.5 – TIO Nexus")
[Answer]
# R, 40 bytes
```
f=function(x)function(y)`if`(y,f(x+y),x)
```
0 acts as the stop value here. For two more bytes, we can omit it.
The problem is that R lacks a concise built-in lambda. But [if we add one](https://github.com/klmr/functional/#a-concise-lambda-syntax), we can get the code to **26 bytes**:
```
f=x->(y->`if`(y,f(x+y),x))
```
(Yes, that’s valid R. It just needs an import.)
[Answer]
# PHP, 44 Bytes
An Idea from [@user63956](https://codegolf.stackexchange.com/users/63956/user63956)
Termination call `0`
```
function f($i){return[$_GET[0]+=$i][$i]?:f;}
```
[Online Version](http://sandbox.onlinephpfunctions.com/code/03419ad8be41d166de374cc7dc144274fd5a1cf0)
Termination call with `NULL` need a càst `[$i]` to `[+$i]`
# PHP, 47 Bytes
```
function f($i){global$s;return$i?f.!$s+=$i:$s;}
```
[Online Version](http://sandbox.onlinephpfunctions.com/code/1d2b18bcc5c0cd2c670ad83c8fb7185d30244b25)
# PHP, 52 Bytes
Termination call `NULL` or any other value that is false in PHP
```
function f($i){global$s;$i?$s+=$i:print$s;return f;}
```
if the program must terminate after the Output replace `print$s` with `die("$s")` + 2 Bytes
[Online Version](http://sandbox.onlinephpfunctions.com/code/6595b125390e7d6dfc8795d6eb8e5141bc131bcc)
]