Function definition

So I want to define a new function, probit, from functions that I use in the SpecialFunctions toolbox. i.e. the function itself is a variant of a function found in this toolbox.
Right now I have the following

function probit(t)
    using SpecialFunctions
    return 0.5*(1+erf(t/sqrt(2)))
end

However I figure that having to load the SpecialFunctions package every time this function is called may not be the most efficient way of doing things. Is there some way to import just the erf function and therefore save some time?
Alternately could I just make it so that this function returns an error if the SpecialFunctions package has not been loaded somehow?

Thanks

EDIT: Actually when I run the above I get an error: “syntax: “using” expression not at top level”

so should I just keep this at the top of my function definition file?

This should work, using SpecialFunctions: erf

I think the important bit here is that the using statement does not belong into the function definition, but at the top level, for example at the beginning of the file where you define the function probit.


julia> function probit(t)
           using SpecialFunctions
           return 0.5*(1+erf(t/sqrt(2)))
       end
ERROR: syntax: "using" expression not at top level

Edit: Yes, exactly. Then it is also clear that the problem that you anticipate “load the SpecialFunctions package every time this function runs” does not happen.

1 Like