# 1.0 annoyances and Matlab comparison

**URL:** <https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433>\
**Category:** Internals & Design\
**Created:** [March 1, 2018, 9:49pm UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433 "2018-03-01T21:49:56Z")\
**Posts on this page:** 9\
**Page:** 8

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 8, 2018, 1:42am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/142 "2018-03-08T01:42:30Z")

</div>

Use a module to encapsulate your functions. See [1.0 annoyances and Matlab comparison - #140 by PetrKryslUCSD](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/140).

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [March 8, 2018, 1:50am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/143 "2018-03-08T01:50:03Z")

</div>

```julia
module m
  local c = 0; 
  local function d(c2, c3 = "?")
    println(c2, "1"); end;
  function d(c)
    println(c, "2"); end;
  d(c);
  > 0, 1 ? ## or conflict?
  end;
m.d(1);
> 1, 2 ## ? 

```

---

<div class="post-metadata">

**Author:** ![Alex0573](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex0573/32/3018_2.png) [@Alex0573](https://discourse.julialang.org/u/Alex0573)\
**Post date:** [March 8, 2018, 1:57am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/144 "2018-03-08T01:57:10Z")

</div>

@PetrKryslUCSD - there are ways around it, but for me, coming from Cpp and Matlab, this is just an (extremely) undesired feature on Julia. Something to be aware of and a source of potential bugs in my code.  
Just like the fact that arrays are always passed by ref to functions, as in:

```julia
function change_arr(arr_in)
  arr_in[2] = 6;
  return;
end

arr_in = zeros(Int32, 1, 2);
arr_in[1] = 5;
println("before call - " * string(arr_in));
change_arr(arr_in);
println("after call - " * string(arr_in));

```

output:

```julia
before call - Int32[5 0]
after call - Int32[5 6]
```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 8, 2018, 1:59am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/145 "2018-03-08T01:59:33Z")

</div>

Well yes, it can be an unpleasant surprise if the writer of the function did not indicate that the arguments were getting changed inside. On the other hand, the benefits in terms of performance are tremendous.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 8, 2018, 2:03am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/146 "2018-03-08T02:03:31Z")

</div>

One can always screw it up with variables defined outside of the scope of the functions but still within the module. My point was that the module would protect the function from variables defined outside of the module. (The variables defined inside the module are also a little bit easier to control for these sorts of conflicts.)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 8, 2018, 6:41am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/147 "2018-03-08T06:41:12Z")

</div>

Arguments are always passed by value in julia. This is nice because the is no need for the constant fear of redundant copies like in c++.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [March 8, 2018, 7:32am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/148 "2018-03-08T07:32:13Z")

</div>

Agreed and point noted  
That was just a little experiment with module local scopes, it’s probably a bit tricky to make use of though - especially the local function

It’s quite easy to declare globals accidentally too, often you just need a top-level var with local scope

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 8, 2018, 7:55am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/149 "2018-03-08T07:55:00Z")

</div>

> [@Alex0573](#):
>
> I was not aware of that @andrewning, my experience is Cpp and Matlab, not Python.
> 
> Since the behaviour is different to Cpp and Matlab, I’m not convinced it was the best option to use the Python style in this case.

I think you have been misinformed. Matlab too has this (see [Nested Functions - MATLAB & Simulink - MathWorks Nordic](https://se.mathworks.com/help/matlab/matlab_prog/nested-functions.html)) The only reason you don’t easily see it is that you cannot define ordinary functions on the command line (an anonymous function will show you though.)

Javascript also has it, I think in fact it is very common.

In C++ you cannot define nested functions, apparently, otherwise they would probably have it too.

---

<div class="post-metadata">

**Author:** ![axsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axsk/32/1952_2.png) [@axsk](https://discourse.julialang.org/u/axsk)\
**Post date:** [June 22, 2018, 11:02am UTC](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433/150 "2018-06-22T11:02:09Z")

</div>

> [@jeff.bezanson](#):
>
> The goal was not to protect against uninitialized data via the length of the name, but just by forcing you to be explicit.

Shouldn’t the same rule apply for `similar` then?  
To me the fact it’s allocating uninitiliaizieid [\*] memory is as implicit here as with `Array`.

[\*] - I clearly don’t like the wording 😛

Edit: Woops, didn’t see this thread went stale. Sorry for bumping.

[Previous page](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433.md?page=7)
