> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/a/accumulator_factory.md).

# Accumulator factory

```ruby
class Accumulator(sum) {
    method add(num) {
        sum += num
    }
}
 
var x = Accumulator(1)
x.add(5)
Accumulator(3)
say x.add(2.3)               # prints: 8.3
```

The same thing can be achieved by returning a closure from the *Accumulator* function.

```ruby
func Accumulator(sum) {
    func(num) { sum += num }
}
 
var x = Accumulator(1)
x(5)
Accumulator(3)
say x(2.3)                  # prints: 8.3
```
