I’m writing a short function and try to call it in a same file as follows.
a = 1.0:3.0
tfs(a)
function tfs(x)
n = length(x)
m = sum(x)/n
s = sqrt(sum((x-m).^2/n))
return m, s
end
Of course, I got an error.
ERROR: LoadError: UndefVarError: tfs not defined
in include_from_node1(::String) at ./loading.jl:488
while loading /home/chobbes/Documents/git/smsa/test14.jl, in expression starting on line 2
I know there must be something naively wrong. Can anyone give me a hint? Thanks!!
OMG…That works!! I know this must be the dumbest question even in ‘First steps’…
Btw, if I define a function in a file and want to call it in terminal, what am I supposed to do to make sure that the terminal can ‘see’ that function? Thanks!!
If I only need the function to return m, but not s, what shall I do? Is there anything like ~ in Matlab? For example, [m, ~] = tfs(x) in Matlab. Thanks!!
You can either just index into the result, e.g. tfs(x)[1] or use m, _ = tfs(x), where _ is just some identifier right now, but might do the same thing as ~ in the future.
In Julia, multiple values are returned and assigned as tuples, e.g. (a, b) = (1, 2) or a, b = 1, 2. MATLAB’s nargout, which is often used in MATLAB to do optional work based on the number of returned values, does not exist in Julia. Instead, users can use optional and keyword arguments to achieve similar capabilities.
So I wonder what the ‘optional and keyword arguments’ are? And is there any workaround at all to achieve what nargout does in Matlab? Thanks!!