Starting with julia's functions


i follow Julia-'s help on 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

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 about this point.

morehowever, i type this :

function f(x)

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


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.


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.


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