Hi, all. I am new to Julia, is it possible to build a function in struct, and whether it is efficient in coding comparing with an independent function?

struct s
       a
       function f(x,a)
             return a*x
       end
end

Sure, it’s valid to put almost any code in the body of struct. The body seems to act as if it was within a let, that is, it’s a local scope. So whatever you define in the body will not be available outside by default. It’s possible to override this using a global declaration.

Thanks, may I ask which one is better? (function in struct or outside struct)

See: Building function (like OOP) inside a struct - #2 by stevengj