Tutorial by Examples

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) 100 Functions are objects in Julia, and we can show them in the REPL as with any other objects: julia> square square (generic func...
Simple recursion Using recursion and the ternary conditional operator, we can create an alternative implementation of the built-in factorial function: myfactorial(n) = n == 0 ? 1 : n * myfactorial(n - 1) Usage: julia> myfactorial(10) 3628800 Working with trees Recursive functions are o...
We can use the :: syntax to dispatch on the type of an argument. describe(n::Integer) = "integer $n" describe(n::AbstractFloat) = "floating point $n" Usage: julia> describe(10) "integer 10" julia> describe(1.0) "floating point 1.0" Unlike m...
A long-form syntax is available for defining multi-line functions. This can be useful when we use imperative structures such as loops. The expression in tail position is returned. For instance, the below function uses a for loop to compute the factorial of some integer n: function myfactorial(n) ...
Arrow syntax Anonymous functions can be created using the -> syntax. This is useful for passing functions to higher-order functions, such as the map function. The below function computes the square of each number in an array A. squareall(A) = map(x -> x ^ 2, A) An example of using this fu...

Page 1 of 1