Starting with julia's functions


#1

hi
i follow Julia-'s help on http://docs.julialang.org/en/stable/manual/functions/ and i try to inderstand how functions work in Julia
i type this in a file that i run with an include in the RPPL :

function f(x,y)
        x + y
end
f(2,3)

it seems to work but Julia says :

WARNING: Method definition f(Any, Any) in module Main at /Users/vincentdouce/Desktop/Julia/vademecum_Julia.jl:16 overwritten at /Users/vincentdouce/Desktop/Julia/vademecum_Julia.jl:16.

i must admit i do not undestand at all what it means, and i pain to find any explanation on http://docs.julialang.org about this point.

morehowever, i type this :

function f(x)
  x*2
end
f(2,3)

and the result is 5.
i am ok that my script is wrong according to the number of variables of f, but why 5 ?

de plus je ne parviens pas du tout à comprendre comment tracer une famille de fonctions. Par exemple si je veux tracer les courbes f(x)=a.cos(x) pour a variant
j’ai essayé :

1 function f(x,y)
2   return(x*cos(y))
3 end
4 
5 tvec = linspace(-π,π , 100)
6 for i=-5:5
7   plot!(tvec, f(i,tvec))
8 end

but the result is :
WARNING: Method definition f(Any, Any) in module Main at [line 2] overwritten at [line 2]

???

thanks for your help


#2

Julia is rereading the file, so the method definition is overwritten, hence the warning.

It is possible that you have a definition f(x) in your global scope. Try workspace() to clear it.

Finally (sorry, my French is very basic so I am not sure I understand), there is no “standard” plotting library in Julia. Many people like Plots.jl, which has plenty of examples.


#3

Every time you include your files, julia also creates the functions again which are defined in the files. This leads to the method definition warning as julia already knows a function with the same name and argument types and warns you, that it will be overridden.

Your second problem comes from multiple dispatch: a function with the same name, but different number or types of arguments is a new method. Julia decides which method to use by the types of the arguments. In your case f(2,3) evaluates to 5 because the version of fwith two arguments is called - this is f(x,y) = x+y.


#4

Here’s an answer with work arounds which I gave a new Julia user before: