# Calling a function inside other function

**URL:** <https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023>\
**Category:** Questions\
**Tags:** book-programming-elixir, deployment, troubleshooting, learning-elixir\
**Created:** [October 19, 2020, 1:39pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023 "2020-10-19T13:39:46Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 19, 2020, 1:39pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/1 "2020-10-19T13:39:46Z")

</div>

Hi,

I am having three functions and the data inputs to only one of them. Lets say i have a function `A` having two parameters `a` and `b`. The `sum` operation is going inside.

```elixir
def sum(a, b), do: a + b

```

Then in the second function, I want to add ten to the result of `sum(a, b)`. So i can simply do like

```elixir
def sum(a, b) do 
a + b
|> add()
end

```

and then define `add`.

```elixir
def add(sum) do
sum + 10
end

```

Now, my question is that how can I use this sum function and add functions separately. let’s say in a third function I want to take the product of `sum` and `add` function. Right now I can’t use them as the `sum` function is always calling the `add` function.

```elixir
def product() do
# code here
add * sum
end

```

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [October 19, 2020, 1:45pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/2 "2020-10-19T13:45:52Z")

</div>

Are you looking for this one?

> **[Functions
 · Elixir School](https://elixirschool.com/en/lessons/basics/functions/#anonymous-functions)**

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 19, 2020, 1:49pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/3 "2020-10-19T13:49:53Z")

</div>

Actually no, I am looking for something where I can use the output of a function and also use it in some other function without specifying the arguments.

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [October 19, 2020, 1:53pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/4 "2020-10-19T13:53:38Z")

</div>

That sounds like OOP. Elixir doesn’t have objects. Functions act on their parameters (besides side-effects).

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 19, 2020, 1:55pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/5 "2020-10-19T13:55:47Z")

</div>

yeah, you are right … is there any approach that I can follow to get what I want …  
do you think `function arity` can be my friend

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [October 19, 2020, 1:56pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/6 "2020-10-19T13:56:29Z")

</div>

I still don’t really understand what you’re after, so I can’t really give suggestions for alternatives.

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 19, 2020, 2:00pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/7 "2020-10-19T14:00:33Z")

</div>

I want to use the output of a function A in let says function B. Sometimes we don’t have those parameters in function B, then how can we use the function A output in function B.

There is one way we can call function B in Function A, like this

```elixir
def A(a, b, c) do
a+ b + c 
|> B()
end

```

But I don’t want this approach. I want to handle them separately. How you got my question.  
Thanks

---

<div class="post-metadata">

**Author:** ![Jcambass](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jcambass/32/18876_2.png) [@Jcambass](https://forum.elixirforum.com/u/Jcambass)\
**Post date:** [October 19, 2020, 2:06pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/8 "2020-10-19T14:06:58Z")

</div>

> I want to use the output of a function A in let says function B. Sometimes we don’t have those parameters in function B, then how can we use the function A output in function B.

You can’t. If you need the output of function A you either need to call it and pass the output of it as an parameter to function B, or you need to call function A with arguments in function B.

Your question is quite theoretical, maybe you can share the problem you’re actually trying to solve (if you have a concrete thing you’re trying to achieve). Functional is a different way of programming but it can solve the same problems as OOP, it just needs a different way of thinking about the problem you want to solve.

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 19, 2020, 2:21pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/9 "2020-10-19T14:21:27Z")

</div>

Hi,

thanks for your feedback.

```elixir
def delta_link(tuple, count) do
    # [{tuple, count}]

    linkv(tuple, count)
  end

  def linkv(tuple, count) do
    try do
      :ets.new(:linkv, [:named_table, :duplicate_bag])
    rescue
      ArgumentError -> :ok
    end

    :ets.insert(:linkv, [{tuple, count} | link()])

    :ets.tab2list(:linkv)
    |> Count.count()
    |> IO.inspect()
  end

  def insert(s, d) do
    dl = :ets.new(:delta_link, [:duplicate_bag])

    :ets.insert(dl, {s, d})

    :ets.lookup(dl, s)
    |> delta_link(1)
  end

```

in this code, i want to use `delta_link` separately and `linkv` separately. I just want to call the output of `delta_link` in `linkv`.

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [October 19, 2020, 2:40pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/10 "2020-10-19T14:40:35Z")

</div>

Hello, I can’t understand your question. Why can’t you just call `add(sum(a, b), 10)` where you need the result?

By the way, the following code:

```elixir
a + b
|> add()

```

is actually `a + add(b)`. If you really want to use pipes you need to write:

```elixir
(a + b)
|> add()

```

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 19, 2020, 4:59pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/11 "2020-10-19T16:59:12Z")

</div>

> [@lud](#):
>
> ```elixir
> (a + b)
> |> add()
> 
> ```

The full pipe way 🙂

```elixir
a 
|> Kernel.+(b) 
|> add()

```

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 19, 2020, 5:10pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/12 "2020-10-19T17:10:17Z")

</div>

> [@owaisqayum](#):
>
> ```elixir
> try do
> :ets.new(:linkv, [:named_table, :duplicate_bag])
> rescue
> ArgumentError -> :ok
> end
> 
> ```

Usually ets table are managed by a process… why are You testing if it exists in a try rescue block?

> [@owaisqayum](#):
>
> use the output of a function and also use it in some other function without specifying the arguments.

Some kind of clojure?

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 19, 2020, 6:14pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/13 "2020-10-19T18:14:31Z")

</div>

I call the function every time. It helps not creating the table every time. So, if a table already exists, it skips that call.

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 19, 2020, 6:18pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/14 "2020-10-19T18:18:22Z")

</div>

That is why You manage an ets table with a process, You call new only once, when the server start.

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 19, 2020, 6:36pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/15 "2020-10-19T18:36:51Z")

</div>

i havent used processes for now. Will use in future modification.

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 19, 2020, 7:11pm UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/16 "2020-10-19T19:11:04Z")

</div>

I was writing one last night…

Maybe it can help You.

```elixir
defmodule EventStore.Core.ListenersProvider do
  use GenServer
  require Logger

  @name __MODULE__

  def start_link(_) do
    GenServer.start_link(@name, nil, name: @name)
  end

  def register(pid, filter_fun \\ fn _ -> true end)

  def register(pid, filter_fun) when is_function(filter_fun) do
    GenServer.call(@name, {:register, pid, filter_fun})
  end

  def unregister(pid) do
    GenServer.cast(@name, {:unregister, pid})
  end

  def get_listeners do
    @name
    |> :ets.match_object({:"$1", :"$2", :_})
    |> Enum.map(fn {pid, filter_fun, _ref} -> {pid, filter_fun} end)
  end

  def stop, do: GenServer.cast(@name, :stop)

  @impl GenServer
  def init(_) do
    Logger.debug(fn -> "#{@name} is starting}" end)
    #
    Process.flag :trap_exit, true
    :ets.new(@name, [:set, :protected, :named_table])
    {:ok, nil}
  end

  @impl GenServer
  def handle_call({:register, pid, filter_fun}, _from, _state) do
    ref = Process.monitor(pid)
    :ets.insert_new(@name, {pid, filter_fun, ref})
    {:reply, :ok, nil}
  end

  @impl GenServer
  def handle_cast({:unregister, pid}, _state) do
    case :ets.match(@name, {pid, :_, :"$1"}) do
      [[ref]] when is_reference(ref) -> Process.demonitor(ref)
      _ -> nil
    end
    :ets.delete(@name, pid)

    {:noreply, nil}
  end

  @impl GenServer
  def handle_cast(:stop, _state), do: {:stop, :normal, nil}

  @impl GenServer
  def handle_info({:DOWN, _ref, :process, pid, _status}, _state) do
    :ets.delete(@name, pid)
    {:noreply, nil}
  end

  @impl GenServer
	def terminate(reason, _state) do
    Logger.debug(fn -> "#{@name} is stopping : #{inspect reason}" end)
    #
    :ets.delete(@name)
		:ok
	end
end

```

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [October 20, 2020, 7:28am UTC](https://forum.elixirforum.com/t/calling-a-function-inside-other-function/35023/17 "2020-10-20T07:28:53Z")

</div>

Thank you so much … it matters a lot.
