# Different behaviors

**URL:** <https://discourse.julialang.org/t/different-behaviors/3247>\
**Category:** New to Julia\
**Created:** [April 17, 2017, 7:54am UTC](https://discourse.julialang.org/t/different-behaviors/3247 "2017-04-17T07:54:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sungjae](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sungjae](https://discourse.julialang.org/u/sungjae)\
**Post date:** [April 17, 2017, 7:54am UTC](https://discourse.julialang.org/t/different-behaviors/3247/1 "2017-04-17T07:54:44Z")

</div>

Consider the following code:

> x = 0  
> y = 1  
> if y\>0  
> f(x) = x  
> else  
> f(x) = 10000  
> end  
> y = (f(x))^2  
> println(y)

Running this code simply prints 0. No surprises here. Now, define the function myfun as follows:

> function myfun(x, y)  
> if y\>0  
> f(x) = x  
> else  
> f(x) = 10000  
> end  
> y = (f(x))^2  
> return y  
> end

Then, I expected that myfun(0,1) would return the same value as the above code. But actually it does not but it returns 100000000.

What am I not understanding here? Please help…

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [April 17, 2017, 8:27am UTC](https://discourse.julialang.org/t/different-behaviors/3247/2 "2017-04-17T08:27:05Z")

</div>

Not allowed ([https://github.com/JuliaLang/julia/issues/17782](https://github.com/JuliaLang/julia/issues/17782) ) use anonymous functions

---

<div class="post-metadata">

**Author:** ![ararslan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ararslan/32/3825_2.png) [@ararslan](https://discourse.julialang.org/u/ararslan)\
**Post date:** [April 18, 2017, 7:11pm UTC](https://discourse.julialang.org/t/different-behaviors/3247/3 "2017-04-18T19:11:22Z")

</div>

To expand briefly on @pkofod’s response, defining named functions within a function is not supported. To achieve the desired behavior, you can to use [anonymous functions](https://docs.julialang.org/en/latest/manual/functions.html#man-anonymous-functions-1). So your second example should be written instead as

```julia
function myfun(x, y)
    if y > 0
        f = x -> x
    else
        f = x -> 10000
    end
    y = f(x)^2
    return y
end

```

As I understand it, allowing `f` to be a “regular” function here is problematic because it may have different methods depending on the code path, and rebuilding its method list on each call is slow.

The simpler, more Julian approach here would be to define `f` outside of `myfun`:

```julia
f(x, y) = y > 0 ? x : 10000
myfun(x, y) = f(x, y)^2

```

---

<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:** [April 18, 2017, 7:34pm UTC](https://discourse.julialang.org/t/different-behaviors/3247/4 "2017-04-18T19:34:09Z")

</div>

> [@ararslan](#):
>
> defining named functions within a function is not supported.

They are I thought. As long as they are not conditionally defined like in this example?

```julia
julia> f(y) = (g(x) = x + y)
f (generic function with 2 methods)

julia> g5 = f(5)
(::g) (generic function with 1 method)

julia> g5(3)
8

```

> [@ararslan](#):
>
> The simpler, more Julian approach here would be to define f outside of myfun:

I don’t agree. Defining closures inside functions is very “Julian” in my view.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 18, 2017, 7:52pm UTC](https://discourse.julialang.org/t/different-behaviors/3247/5 "2017-04-18T19:52:35Z")

</div>

Defining “named” function in local scope/as closure is fully supported, the difference is the function type.

In the same scope, all functions definitions of the same name are of the same type. So

```julia
function f()
    ...
    g(...) = ...
    ...
    g(...) = ...
end

```

Will create a type with two methods. The type and methods definitions are generated at compile (lowering) time and be define before `f` is called. (Though calling them with the wrong signature might cause undefined variable error)

Anonymous functions don’t have a name (because, well, they are anonymous…) so they each have their own types.

---

<div class="post-metadata">

**Author:** ![sungjae](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sungjae](https://discourse.julialang.org/u/sungjae)\
**Post date:** [April 25, 2017, 5:41pm UTC](https://discourse.julialang.org/t/different-behaviors/3247/6 "2017-04-25T17:41:49Z")

</div>

Oops. I accidentally submitted a reply before it was completed. I will try to do it again here.

It is good to define

> f(x,y) = y\>0 ? x: 10000  
> myfun(x,y) = f(x,y)^2

when there is only one condition. Suppose that I want to use

> if y\<0  
> f = x → x  
> elseif 0\<= y \< 1  
> f = x → x^2  
> else  
> f = x → 10000  
> end

In this case I guess I can simply use

> f(x,y) = x \* (y\<0) + (x^2) \* (0\<=y\<1) + 10000 \* (y\>=1)

Do you have any comments about this approach?

Thank you.
