# Function calling another function

**URL:** <https://forum.elixirforum.com/t/function-calling-another-function/39182>\
**Category:** Questions\
**Tags:** function\
**Created:** [April 23, 2021, 4:38pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182 "2021-04-23T16:38:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![grangerelixx](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@grangerelixx](https://forum.elixirforum.com/u/grangerelixx)\
**Post date:** [April 23, 2021, 4:38pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/1 "2021-04-23T16:38:39Z")

</div>

I have a function `Function_1(arg1,arg2,arg3)`

I would like to call Function\_1 in another function `Function_2( )` where only arg3 is passed like `arg3 \\ :hello`

And I would like to use Function\_2( ) in my code like this

```elixir
result =
arg_1_val
|> Function_2(arg_2_val)

```

Here arg\_1\_val is my arg1, arg\_2\_val is my arg2 and arg3 will be passed in Function\_2 definition.

Any inputs on what is the right approach to do this are appreciated.

---

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [April 23, 2021, 5:08pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/2 "2021-04-23T17:08:10Z")

</div>

Hi @grangerelixx, can you make this more concrete? You aren’t actually posting valid elixir code, which makes this hard to understand.

---

<div class="post-metadata">

**Author:** ![grangerelixx](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@grangerelixx](https://forum.elixirforum.com/u/grangerelixx)\
**Post date:** [April 23, 2021, 5:20pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/3 "2021-04-23T17:20:59Z")

</div>

So initially I had this function definition

```elixir
function_1(arg1,arg2,arg3) do 
  does something
end

```

and I used this function in many parts of my code like this

```elixir
val1 = 3
val2 = 6

result1 =
val1
|> function_1(4,5)

result2 =
val2
|>function_1(7,5)

result3 = 
val1
|> function_1(3,10)

result4 = 
val2
|> function_1(9,10)

```

Since the arg3 is repetitive in some cases, I would like to create two functions which call fucntion\_1, say function \_with\_arg3\_equals\_5 and function \_with\_arg3\_equals\_10.

So now I can use these new functions

```elixir
result1 =
val1
|> function _with_arg3_equals_5(4)

result2 =
val2
|>function _with_arg3_equals_5(7)

result3 = 
val1
|> function _with_arg3_equals_10(3)

result4 = 
val2
|> function _with_arg3_equals_10(9)

```

---

<div class="post-metadata">

**Author:** ![derek-zhou](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/derek-zhou/32/19943_2.png) [@derek-zhou](https://forum.elixirforum.com/u/derek-zhou)\
**Post date:** [April 23, 2021, 5:33pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/4 "2021-04-23T17:33:53Z")

</div>

Of cause you can do that. However, I’d suggest you to finish some elixir tutorial on your own first, otherwise it is hard for other people to offer you help without knowing you have a baseline knowledge.

If you have prior experience with functional programming, the official [Elixir tutorial](https://elixir-lang.org/getting-started/introduction.html) should be enough. If you do not have any experience of functional programming, you can consider to purchase a book or some online tutorial that have more details than the official elixir tutorial.

---

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [April 23, 2021, 6:46pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/5 "2021-04-23T18:46:05Z")

</div>

So your question is?

It will help if you can post real code.  
My advice is not to create extra functions, just be explicit when you call a function. It will make your code more readable, and lean.

---

<div class="post-metadata">

**Author:** ![grangerelixx](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@grangerelixx](https://forum.elixirforum.com/u/grangerelixx)\
**Post date:** [April 23, 2021, 7:20pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/6 "2021-04-23T19:20:29Z")

</div>

Thank you all for the inputs. I was able to execute what I intended to.

```elixir
def function_1(arg1,arg2,arg3) do 
  does something
end

def function_with_arg3_equals_5(arg1,arg2,arg3 \\ 5) do
  function_1(arg1,arg2,arg3)
end

def function_with_arg3_equals_10(arg1,arg2,arg3 \\ 10) do
  function_1(arg1,arg2,arg3)
end

```

---

<div class="post-metadata">

**Author:** ![patrickdm](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/patrickdm/32/7579_2.png) [@patrickdm](https://forum.elixirforum.com/u/patrickdm)\
**Post date:** [April 23, 2021, 7:25pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/7 "2021-04-23T19:25:16Z")

</div>

IMHO, with no risk if being unfaithful with the functions names, you can skip completely the third optional argument:

> [@grangerelixx](#):
>
> ```elixir
> def function_1(arg1, arg2, arg3) do 
> does something
> end
> 
> def function_with_arg3_equals_5(arg1, arg2) do
> function_1(arg1, arg2, 5)
> end
> 
> def function_with_arg3_equals_10(arg1, arg2) do
> function_1(arg1, arg2, 10)
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [April 23, 2021, 7:34pm UTC](https://forum.elixirforum.com/t/function-calling-another-function/39182/8 "2021-04-23T19:34:10Z")

</div>

> [@patrickdm](#):
>
> you can skip completely the third optional argument:

I will replace “can” with “must”. Otherwise, if you call: `function_with_arg3_equals_5(1, 2, 10000)` it defeats the purpose of the funtion name, and you should be calling `function_1/3`
