# What are different types of functions in Julia and what are the differences between them?

**URL:** <https://discourse.julialang.org/t/what-are-different-types-of-functions-in-julia-and-what-are-the-differences-between-them/98113>\
**Category:** General Usage\
**Tags:** question, error, syntax, optimization, function\
**Created:** [April 30, 2023, 6:08am UTC](https://discourse.julialang.org/t/what-are-different-types-of-functions-in-julia-and-what-are-the-differences-between-them/98113 "2023-04-30T06:08:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![meet.saiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meet.saiya/32/49312_2.png) [@meet.saiya](https://discourse.julialang.org/u/meet.saiya)\
**Post date:** [April 30, 2023, 6:08am UTC](https://discourse.julialang.org/t/what-are-different-types-of-functions-in-julia-and-what-are-the-differences-between-them/98113/1 "2023-04-30T06:08:21Z")

</div>

I’m trying to replicate a code of [Sigmoidal programming](https://github.com/madeleineudell/SigmoidalProgramming.jl) for my optimization problem. The example uses a code where they pass in a list of functions.

```julia
fs = fill(logistic, nvar)
dfs = fill(logistic_prime, nvar)

```

The output shows a list of functions.

```julia
200-element Vector{typeof(logistic_prime)}:
 logistic_prime (generic function with 1 method)
 logistic_prime (generic function with 1 method)
 
 ⋮
 logistic_prime (generic function with 1 method)
 logistic_prime (generic function with 1 method)

```

However, when I tried to define a list for a function x^b, where b is a parameter between 0 to 1, it generated a different kind of vector list and the program eventually throws up error.

```julia
coeffs = repeat([0, 15, 30, 60], outer = states) # generate an array of coefficients of objective function
fs = Function[x -> (coeffs[i]*x^b) for i=1:(states*actions)]
dfs = Function[x-> (coeffs[i]*b*x^(b-1)) for i=1:(states*actions)]

```

The output of this snippet is,

```julia
Function[var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"(), var"#49#50"()]Function[var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"(), var"#51#52"()]

```

Which is not the same as what they get using a library. How do I rectify this?  
What am I missing and where do I get the fundamentals correct?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 30, 2023, 8:25am UTC](https://discourse.julialang.org/t/what-are-different-types-of-functions-in-julia-and-what-are-the-differences-between-them/98113/2 "2023-04-30T08:25:57Z")

</div>

Those are still generic functions, just anonymous ones (which you made with `x -> x^b` syntax but also could’ve done `function (x) x^b end`), so you get an automatically generated name. `var"#49#50"` is the name of the function’s type, so `var"#49#50"()` is an instance of that type, thus the function itself.

The reason why the name doesn’t refer to the function itself is for efficiency’s sake. The type’s instances are [Function-like objects](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects) that are also `<:Function`. Instead of making and compiling totally separate methods, one function with extra parameters can stand in for a set of functions, and the instance’s fields capture those parameters:

```julia
julia> y = 10:11; println(Function[x -> y[i]*x for i in 1:2])
Function[var"#11#12"{Int64}(1), var"#11#12"{Int64}(2)]

```

I’m actually surprised your anonymous functions in `fs` or `dfs` lack fields, they should have at least captured the `i`. I suppose your code snippet is leaving out a lot of things e.g. `states`, `b`, `actions`.

---

<div class="post-metadata">

**Author:** ![meet.saiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meet.saiya/32/49312_2.png) [@meet.saiya](https://discourse.julialang.org/u/meet.saiya)\
**Post date:** [April 30, 2023, 8:39am UTC](https://discourse.julialang.org/t/what-are-different-types-of-functions-in-julia-and-what-are-the-differences-between-them/98113/3 "2023-04-30T08:39:26Z")

</div>

The `fs` and `dfs` are from the sigmoidal programming library mentioned in the link earlier and they are not in my code. I just copied one of the examples in the repo and ran it on my system. That seems to work but when I do it myself, it throws up errors.  
`states`, `actions` are used to define the constraints and this issue is specific to objective function, ergo they’re unrelated I guess, however, I am open to sharing entire code if that helps in any way. `b` is a model parameter and is between 0 to 1. So what I basically want to do is generate a list of functions with `coeff[i]*x^b` where I can change b at the top, where I assign different values to different parameters of the model and it would work out. Or so I think. `coeff[i]` is basically the cost of the associated variable.

Here is the link to my other, related question: [Community post](https://discourse.julialang.org/t/error-code-for-glpk-and-sigmoidalprogramming/98043)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 30, 2023, 6:25pm UTC](https://discourse.julialang.org/t/what-are-different-types-of-functions-in-julia-and-what-are-the-differences-between-them/98113/4 "2023-04-30T18:25:01Z")

</div>

Linking this thread on [all the ways to define functions in Julia](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816) and [related article](https://medium.com/@evalparse/there-are-so-many-ways-to-define-a-function-sort-of-in-julia-7821e49373cd), which may help answering the OP title question.
