Julia Language Square a number


This is the easiest syntax to define a function:

square(n) = n * n

To call a function, use round brackets (without spaces in between):

julia> square(10)

Functions are objects in Julia, and we can show them in the REPL as with any other objects:

julia> square
square (generic function with 1 method)

All Julia functions are generic (otherwise known as polymorphic) by default. Our square function works just as well with floating point values:

julia> square(2.5)

...or even matrices:

julia> square([2 4
               2 1])
2×2 Array{Int64,2}:
 12  12
  6   9