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

I’m trying to replicate a code of Sigmoidal programming for my optimization problem. The example uses a code where they pass in a list of functions.

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

The output shows a list of functions.

``````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.

``````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,

``````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?

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 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> 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`.

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

Linking this thread on all the ways to define functions in Julia and related article, which may help answering the OP title question.