Anonymous functions

The function f(x, y) at the end of the Defining functions section can also be written with no name, as an anonymous function: (x, y) -> x^3 – y + x * y. We can, however, bind it to a name such as f = (x, y) -> x^3 – y + x * y, and then call it, for example, as f(3, 2). Anonymous functions are also often written using the following syntax:

function (x)
    x + 2
end
(anonymous function)
julia> ans(3)
5

Often, they are also written with a lambda expression as (x) -> x + 2. Before the stab character "->" are the arguments, and after the stab character we have the return value. This can be shortened to x -> x + 2. A function without arguments would be written as () -> println("hello, Julia").

Here is an anonymous function taking three ...

Get Julia: High Performance Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.