# Why putting a function argument first

**URL:** <https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700>\
**Category:** General Usage\
**Created:** [April 6, 2021, 4:23pm UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700 "2021-04-06T16:23:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [April 6, 2021, 4:23pm UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/1 "2021-04-06T16:23:28Z")

</div>

Although it appears #1 convention on [style guide of the docs](https://docs.julialang.org/en/v1/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia-Base) to write functions for a long time till Julia 1.6 now, it brings difficulty on some user cases like [manipulating data](https://discourse.julialang.org/t/dataframes-question-filter-function-parameter-order/56395) and piping. I was wondering if it would make life a little easier to withdraw the restriction and let users decide a function argument position at their convenience.

- If the only reason to put a function argument first were using do-block, I think there should be many better solutions out there. My two cents: to implement few more keywords like

```julia
sdo/do2/ddo, tdo/do3

```

and keep backwards compatibility. Eg. calling a function  
`f(x, g::Function, y)` with a function argument at the second place could be called by `sdo-block`

```julia
f(x, y) sdo
    ...
end

```

The `sdo-block` would put the anonymous function into the second position.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 6, 2021, 5:44pm UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/2 "2021-04-06T17:44:12Z")

</div>

You can also create two methods for your function, one that takes a `Function` first and another that takes a `Dataframe` (or similar) first.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 7, 2021, 6:52am UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/3 "2021-04-07T06:52:20Z")

</div>

When `do` block get large, I factor them out to a function anyway. You can do the same and use the result in any position, eg

```julia
_f(x) = ...

g(x, _f, y)

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 7, 2021, 7:38am UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/4 "2021-04-07T07:38:42Z")

</div>

> [@qsong](#):
>
> I was wondering if it would make life a little easier to withdraw the restriction and let users decide a function argument position at their convenience.

It’s no restriction. That’s a style guide: “only suggestions to help familiarize you with the language and to help you choose among alternative designs”, as stated in the first paragraph. If you have reasons to depart from that style for your use case, you are free to do it.

And if the issue is that the style used in `Base` or in others’ code is inconvenient for your use case, the joy of multiple dispatch is that it allows you to make alternative custom methods with the order of arguments that fits your needs very quickly, without having to increase the complexity of the source.

---

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [April 7, 2021, 9:01am UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/5 "2021-04-07T09:01:54Z")

</div>

By “restriction” I mean the long-time convention, which people follows along.  
Eg. `filter` in `DataFrame.jl` follows the guide and keep consistent with `Base.filter`, which is a little uncomfortable to most people I met.

In the case of `map(f, args...)` the function argument **should** be put first, while in some other cases it would be more **natural** to put it on other place.

I don’t know many program languages but is it the case for those that support high-order functions having a style to put a function argument first?

I guess this post is about challenging the reasonableness of this convention, but maybe it is too small…

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 7, 2021, 9:40am UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/6 "2021-04-07T09:40:10Z")

</div>

> [@qsong](#):
>
> In the case of `map(f, args...)` the function argument **should** be put first, while in some other cases it would be more **natural** to put it on other place.

You’ve told it: there is no absolute better convention. But I think that this doesn’t make the current one less reasonable, specially since adapting the interface to more convenient workflows with new methods is easy. In fact, for this use case others have already done it, with a full-featured package to manipulate data:

> **[Queryverse](https://www.queryverse.org/)**
>
> Queryverse

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 7, 2021, 11:56am UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/7 "2021-04-07T11:56:44Z")

</div>

> [@qsong](#):
>
> I don’t know many program languages but is it the case for those that support high-order functions having a style to put a function argument first?

There are two — related — issues here:

1. putting function arguments first,
2. having special syntactic sugar for it.

(1) is just a suggestion, you are free to do otherwise. Also, some Base functions with multiple function arguments (eg `mapreduce`) have functions in other positions out of necessity. It is not a big deal, just do what you prefer.

(2) is syntactic sugar for the most common case. It can be argued that not _everything_ needs its own special syntax, just covering 90% is fine.

That said, putting your function arguments first is quite common in the [Lisp family](http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm).

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [April 7, 2021, 12:24pm UTC](https://discourse.julialang.org/t/why-putting-a-function-argument-first/58700/8 "2021-04-07T12:24:21Z")

</div>

> [@qsong](#):
>
> `filter` in `DataFrame.jl` follows the guide and keep consistent with `Base.filter` , which is a little uncomfortable to most people I met.

I think its good that functions with the same name as base functions follow the same style but packages. Whether this is familiar or not depends on everyone’s prior experience. But in this specific example you’ll be happy to see your suggestion coming to dataframes soon

[https://dataframes.juliadata.org/dev/lib/functions/#DataFrames.subset](https://dataframes.juliadata.org/dev/lib/functions/#DataFrames.subset)
