> For the complete documentation index, see [llms.txt](https://jona-projects.gitbook.io/kiwicalc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jona-projects.gitbook.io/kiwicalc/functions/functionchain.md).

# FunctionChain

### class `FunctionChain`

You can execute several functions on the parameters via the `FunctionChain` class. For instance, lets define some functions: \`f(x) = 2x\`, \`g(x) = x^2\`, and \`h(x) = x-2\`. They will be executed one after another, where each function's input is the last function's output: \`h(g(f(x)))\`. For instance, for \`x = 4\`, \`f(4) = 8\`, \`g(8) = 64\` and \`h(64) = 62\`. Therefore, the final output will be \`62\`. The `FunctionChain` class inherits from `FunctionCollection`, since it's a special collection of functions. Therefore, you can also use methods from `FunctionCollection` here.

#### Creating a new instance

You can create a new instance of `FunctionChain` via the `__init__()` method by entering functions, exactly the same like creating a `FunctionCollection` object. Here is the signature of the method:

```
                        def __init__(self, *functions):
                    
```

For example:

```
                        function_chain = FunctionChain("f(x) = x^2", "g(x) = x+5", "h(x) = sin(x)")
                    
```

In addition, you can also create new instances via the `chain()` method in the `Function` class. For instance:

```
                        function_chain = Function("f(x) = x^2").chain(Function("f(x)=x+5")).chain(Function("h(x) = sin(x)"))
                    
```

#### Execute

You can call all of the functions via the `execute_all()` method, or the built in `__call__() method.` For example:

```
                        function_chain = FunctionChain("f(x) = x^2", "g(x) = x+5", "h(x) = sin(x)")
print(function_chain.execute_all(1))
print(function_chain(1))
                    
```

Output:

```
                        0.8414709848078965
0.8414709848078965       
                    
```

#### Customized Execution

You can execute the functions in a reverse order via the `execute_reverse()` method.In addition, you can execute only certain functions from the collection via the `execute_indices()` method. The method accepts a collection of indices in the collection, and execute the functions in these indices by the specified order. For instance:

```
                        function_chain = FunctionChain("f(x) = x^2", "g(x) = x+5", "h(x) = sin(x)")
print(function_chain.execute_reverse())  # execute in reverse order
print(function_chain.execute_indices([3, 1, 0, 2]))
                    
```

#### Plotting

You can plot the `FunctionChain` object via the `plot()` method, Here is the signature of method:

```
                        def plot(self, start: float = -10, stop: float = 10,
         step: float = 0.01, ymin: float = -10, ymax: float = 10, text=None, show_axis=True, show=True,
         values=None):
                    
```

For example:

```
                        function_chain = FunctionChain("f(x) = x^2", "g(x) = x+5", "h(x) = sin(x)")
function_chain.plot()
                    
```

#### Scattering

You can also scatter the functions via the `scatter()` method. For instance:

```
                        function_chain = FunctionChain("f(x) = x^2", "g(x) = x+5", "h(x) = sin(x)")
function_chain.plot()
                    
```

#### `chain()`

You can chain another function to the collection by using the `chain()` method. For instance:

```
                        function_chain = FunctionChain("f(x) = x^2", "g(x) = x+5")
function_chain.chain("h(x) = sin(x)")
                    
```

#### Indexers

You can fetch the item by its index via the `[]` operator. For instance:

```
                        function_chain = FunctionChain("f(x) = x^2", "g(x) = x+5")
print(function_chain[0])
                    
```
