# In places style question

**URL:** <https://discourse.julialang.org/t/in-places-style-question/33822>\
**Category:** General Usage\
**Created:** [January 26, 2020, 9:08pm UTC](https://discourse.julialang.org/t/in-places-style-question/33822 "2020-01-26T21:08:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [January 26, 2020, 9:08pm UTC](https://discourse.julialang.org/t/in-places-style-question/33822/1 "2020-01-26T21:08:01Z")

</div>

Suppose I have a function that performs an place transformation on the first variable

```julia
function f!(x,y)
    # code here
    x
end

```

This function can result in an error, so I would like to return a `flag`, variable, alerting me if there was a problem. Should the code now have a `return`? If it has a `return`, should it not be named with a `!`? As a simple example, consider:

```julia
function f!(x, y)
    flag = 0;
    for j in 1:length(x)
        x[j]-=y;
    end
    if(sum(x)<0)
        flag = -1;
    end
    x
    return flag
end

```

---

<div class="post-metadata">

**Author:** ![kevinczimmerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevinczimmerman/32/13040_2.png) [@kevinczimmerman](https://discourse.julialang.org/u/kevinczimmerman)\
**Post date:** [January 26, 2020, 9:14pm UTC](https://discourse.julialang.org/t/in-places-style-question/33822/2 "2020-01-26T21:14:15Z")

</div>

Well the ! indicates that the function modifies its arguments.[https://docs.julialang.org/en/v1/manual/style-guide/#Append-!-to-names-of-functions-that-modify-their-arguments-1](https://docs.julialang.org/en/v1/manual/style-guide/#Append-!-to-names-of-functions-that-modify-their-arguments-1)  
So returning a variable shouldn’t change the name.

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [January 26, 2020, 9:16pm UTC](https://discourse.julialang.org/t/in-places-style-question/33822/3 "2020-01-26T21:16:19Z")

</div>

In that example, there is a `return a`. I thought such a function was to end with :

```julia
    a
end

```

and not `return a`.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [January 26, 2020, 9:19pm UTC](https://discourse.julialang.org/t/in-places-style-question/33822/4 "2020-01-26T21:19:01Z")

</div>

There’s no difference to ending a function with `a` or `return a` (see the docs [here](https://docs.julialang.org/en/v1/manual/functions/#The-return-Keyword-1)). The convention with `!` is that a function ending with `!` modifies the first argument itself, e.g. `set_first_to_1!(v::Vector) = v[1]=1` which is a function that mutates `v` by setting its first entry to 1.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [January 27, 2020, 1:27am UTC](https://discourse.julialang.org/t/in-places-style-question/33822/6 "2020-01-27T01:27:41Z")

</div>

The exclamation mark is just another character in the function name, it has no effects on the code.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 27, 2020, 4:22am UTC](https://discourse.julialang.org/t/in-places-style-question/33822/7 "2020-01-27T04:22:11Z")

</div>

As a matter of style and simplicity, rather than returning an error code or an error value or setting an auxiliary error variable – and the error is not something that is recoverable – `throw(<some type of exception>)` instead. That keeps function call results useful for additional calculation immediately.

[throw an exception](https://docs.julialang.org/en/latest/manual/control-flow/#The-%5Bthrow%5D(@ref)-function-1)  
[using error()](https://docs.julialang.org/en/latest/manual/control-flow/#Errors-1)

---

<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:** [January 27, 2020, 8:17am UTC](https://discourse.julialang.org/t/in-places-style-question/33822/8 "2020-01-27T08:17:31Z")

</div>

There are two things going on here:

1. the `!` is a style convention to indicate that the function modifies its argument(s),

2. some of these function also return this argument, making code like

From your example, we don’t know if this is relevant for your context. If yes (ie you want an in-place and a functional version), then I would return `(x, flag)`.

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [January 27, 2020, 1:11pm UTC](https://discourse.julialang.org/t/in-places-style-question/33822/9 "2020-01-27T13:11:14Z")

</div>

That third argument, the `z`, was not meant to mean anything else. I edited it out.
