---
title: "reduce"
excerpt: "Take a starting value. Then, for each element of an array, calculate the next value,"
layout: manual
---

Take a starting value. Then, for each element of an array, calculate the next value,

using the previous value and the element.

```js
reduce(array: [KclValue], start: KclValue, reduce_fn: FunctionParam) -> KclValue
```


### Arguments

| Name | Type | Description | Required |
|----------|------|-------------|----------|
| `array` | [`[KclValue]`](/docs/kcl/types/KclValue) |  | Yes |
| `start` | [`KclValue`](/docs/kcl/types/KclValue) | Any KCL value. | Yes |
| `reduce_fn` | `FunctionParam` |  | Yes |

### Returns

[`KclValue`](/docs/kcl/types/KclValue) - Any KCL value.


### Examples

```js
// This function adds two numbers.
fn add(a, b) {
  return a + b
}

// This function adds an array of numbers.
// It uses the `reduce` function, to call the `add` function on every
// element of the `arr` parameter. The starting value is 0.
fn sum(arr) {
  return reduce(arr, 0, add)
}

/* The above is basically like this pseudo-code:
fn sum(arr):
    sumSoFar = 0
    for i in arr:
        sumSoFar = add(sumSoFar, i)
    return sumSoFar */


// We use `assertEqual` to check that our `sum` function gives the
// expected result. It's good to check your work!
assertEqual(sum([1, 2, 3]), 6, 0.00001, "1 + 2 + 3 summed is 6")
```

![Rendered example of reduce 0](data:image/png;base64,[encoded data, 26204 characters])

```js
// This example works just like the previous example above, but it uses
// an anonymous `add` function as its parameter, instead of declaring a
// named function outside.
arr = [1, 2, 3]
sum = reduce(arr, 0, fn(i, result_so_far) {
  return i + result_so_far
})

// We use `assertEqual` to check that our `sum` function gives the
// expected result. It's good to check your work!
assertEqual(sum, 6, 0.00001, "1 + 2 + 3 summed is 6")
```

![Rendered example of reduce 1](data:image/png;base64,[encoded data, 26204 characters])

```js
// Declare a function that sketches a decagon.
fn decagon(radius) {
  // Each side of the decagon is turned this many degrees from the previous angle.
  stepAngle = 1 / 10 * tau()

  // Start the decagon sketch at this point.
  startOfDecagonSketch = startSketchOn('XY')
    |> startProfileAt([cos(0) * radius, sin(0) * radius], %)

    // Use a `reduce` to draw the remaining decagon sides.
    // For each number in the array 1..10, run the given function,
  // which takes a partially-sketched decagon and adds one more edge to it.
  fullDecagon = reduce([1..10], startOfDecagonSketch, fn(i, partialDecagon) {
    // Draw one edge of the decagon.
    x = cos(stepAngle * i) * radius
    y = sin(stepAngle * i) * radius
    return line(partialDecagon, end = [x, y])
  })

  return fullDecagon
}

/* The `decagon` above is basically like this pseudo-code:
fn decagon(radius):
    stepAngle = (1/10) * tau()
    plane = startSketchOn('XY')
    startOfDecagonSketch = startProfileAt([(cos(0)*radius), (sin(0) * radius)], plane)

    // Here's the reduce part.
    partialDecagon = startOfDecagonSketch
    for i in [1..10]:
        x = cos(stepAngle * i) * radius
        y = sin(stepAngle * i) * radius
        partialDecagon = line(partialDecagon, end = [x, y])
    fullDecagon = partialDecagon // it's now full
    return fullDecagon */


// Use the `decagon` function declared above, to sketch a decagon with radius 5.
decagon(5.0)
  |> close()
```

![Rendered example of reduce 2](data:image/png;base64,[encoded data, 38176 characters])


