# Make function arguments positional or keyword

**URL:** <https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984>\
**Category:** General Usage\
**Tags:** functions, keyword-arguments\
**Created:** [March 31, 2017, 1:45pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984 "2017-03-31T13:45:04Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [April 1, 2017, 6:53pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/21 "2017-04-01T18:53:56Z")

</div>

@mkborregaard: I just wanted to have that flexibility in case people would ask. I love Julia language, and I would like to give people no chance to discard the fact that this is an awesome language Thanks.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 1, 2017, 7:22pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/22 "2017-04-01T19:22:38Z")

</div>

It’s not about the REPL or Atom. It is simply that there are two ways of defining a function:

```julia
f(x) = println(x)
# or
function f(x)
    println(x)
end

```

They are equivalent. But you tried a mix between the two, e.g. you tried something like this:

```julia
f(x)
   println(x)
end

```

That doesn’t work.

---

<div class="post-metadata">

**Author:** ![adamslc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adamslc/32/3452_2.png) [@adamslc](https://discourse.julialang.org/u/adamslc)\
**Post date:** [April 1, 2017, 10:08pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/23 "2017-04-01T22:08:59Z")

</div>

I’ve actually written a little package called [RequiredKeywords.jl](https://github.com/adamslc/RequiredKeywords.jl) that implements this solution using a macro. You can use it like

```julia
julia> using RequiredKeywords

julia> @required_keywords f(x; y) = x*y
f (generic function with 1 method)

julia> f(2,y=2)
4

julia> f(2)
ERROR: Unassigned Keyword: Required keyword y::Int64 not included.
Stacktrace:
 [1] f(::Int64) at ./REPL[5]:1

```

It also works for the longform `function` style definitions, and you can still specify types for the optional keyword arguments.

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [April 3, 2017, 2:37pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/24 "2017-04-03T14:37:18Z")

</div>

@adamslc: Great. Thanks.

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [April 3, 2017, 2:39pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/25 "2017-04-03T14:39:02Z")

</div>

> [@mkborregaard](#):
>
> f(x) = println(x)

Just a quick question. When we write function this way, how do we handle function if the code lines extend multiple lines, but not s single one?  
Thanks.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 3, 2017, 2:44pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/26 "2017-04-03T14:44:57Z")

</div>

```julia
f(x) = (y = x + x; println(y))

```

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [April 3, 2017, 3:00pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/27 "2017-04-03T15:00:57Z")

</div>

@mkborregaard: OK. Thanks.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 3, 2017, 3:07pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/28 "2017-04-03T15:07:05Z")

</div>

Or with begin… end

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 3, 2017, 3:20pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/29 "2017-04-03T15:20:01Z")

</div>

I think I saw somewhere that

```julia
f(x) = begin
    y = x + x
    println(y)
end

```

was deprecated as recommended style in favour of

```julia
function f(x)
    y = x + x
    println(y)
end

```

so I thought only the really compact format was recommended?

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 15, 2017, 7:15am UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/30 "2017-04-15T07:15:50Z")

</div>

I don’t think the former was ever recommended style, but I don’t see anything wrong with it either. It can be nice for consistency if a file primarily contains pure functions defined from short form syntax. Scala encourages the `=` syntax for pure functions too, and I think that’s a good convention: it is weird to see `foo!(x) = bar!(x)` because mutation is not typically mathematically expressed in definitions.

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [September 22, 2018, 10:10pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/31 "2018-09-22T22:10:41Z")

</div>

> [@mbauman](#):
>
> f(;x::Int=throw(ArgumentError(“missing x”))) = x

How could I reduce the length of my function definitions if I have multiple named arguments? For example, I want to do something like:

```julia
err = z -> throw(ArgumentError("missing x"))
f(;x::Int64=err) = x
f()

```

but this gives a method-not-found error.

I see that there are easier ways for v1 (i.e., just doing `f(; x::Int)`), but how could I do for v0.6?

EDIT: can do with

```julia
err() = throw(ArgumentError("missing x"))
f(;x::Int64=err()) = x
f()

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 23, 2018, 1:05am UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/32 "2018-09-23T01:05:20Z")

</div>

> [@jacob-roth](#):
>
> I see that there are easier ways for v1 (i.e., just doing `f(; x::Int)` ), but how could I do for v0.6?

Just use the Compat package and put `@compat` in front of the function declaration, and then you can use the Julia 1.0 syntax for required-keyword arguments.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 23, 2018, 7:27pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/33 "2018-09-23T19:27:01Z")

</div>

Split this topic on September 23, 2018, 7:27pm UTC

3 posts were split to a new topic: [Struct with fixed array sizes](https://discourse.julialang.org/t/struct-with-fixed-array-sizes/15396)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 23, 2018, 7:33pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/34 "2018-09-23T19:33:31Z")

</div>

Split this topic on September 23, 2018, 7:33pm UTC

A post was merged into an existing topic: [Struct with fixed array sizes](https://discourse.julialang.org/t/struct-with-fixed-array-sizes/15396/4)

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [October 19, 2018, 7:23pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/35 "2018-10-19T19:23:07Z")

</div>

Sorry to revive this. Using `Compat 1.3.0`, and Julia v0.6.4, I get the following error:

```julia
julia> using Compat
INFO: Recompiling stale cache file /homes/USER/.julia/lib/v0.6/Compat.ji for module Compat.

julia> Pkg.status("Compat")
 - Compat 1.3.0

julia> @compat f(; x::Int64) = x
ERROR: syntax: invalid keyword argument syntax "x::Int64" (expected assignment)

julia> @compat function(; x::Int64)
           return x
       end
ERROR: syntax: invalid keyword argument syntax "x::Int64" (expected assignment)

```

What am I messing up?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 19, 2018, 8:23pm UTC](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984/36 "2018-10-19T20:23:57Z")

</div>

Looks like a [https://github.com/JuliaLang/Compat.jl/pull/586](https://github.com/JuliaLang/Compat.jl/pull/586) — it didn’t handle the case of typed keyword arguments.

[Previous page](https://discourse.julialang.org/t/make-function-arguments-positional-or-keyword/2984.md?page=1)
