# Arrays Functions and the Dot Operator

## Array Functions and the Dot Operator

It often happens that we have a function which can be applied to a scalar number, but we want to apply it to an array. Of course, we could resort to a for loop to this element-wise function application. But that would be too much hassle.

Fortunately, in Julia we can easily turn a function than accepts a scalar value, and apply it element-wise to an array. The way to do this is to employ a ‘dot’ after the function’s name.

For example, let’s define a scalar function f, and apply it to an array.

f(x) = 3x^3/(1+x^2)
x = [2π/n for n=1:30]
y = f.(x)


Failing to use the dot in the above example would have lead to an error.

A common ‘gotcha’ is that, for example, trigonometric functions (or even arithmetic functions) can also require this treatment. We have to do:

y = sin.(x)


In case we need to use the dot operator a lot of times in an expression, instead of doing

y = 2x.^2 + 3x.^5 - 2x.^8


we can resort to broadcasting the dot operator, like this

y = @. 2x^2 + 3x^5 - 2x^8