Introducing Julia/Functions

Functions
Functions are the building blocks of Julia code, acting as the subroutines, procedures, blocks, and similar structural concepts found in other programming languages.

A function is a collected group of instructions that can return one or more values, possibly based on the input arguments. If the arguments contain mutable values like arrays, the array can be modified inside the function. By convention, an exclamation mark (!) at the end of a function's name indicates that the function may modify its arguments.

There are various syntaxes for defining functions:


 * when the function contains a single expression
 * when the function contains multiple expressions
 * when the function doesn't need a name

Single expression functions
To define a simple function, all you need to do is provide the function name and any arguments in parentheses on the left and an expression on the right of an equals sign. These are just like mathematical functions:

Functions with multiple expressions
The syntax for defining a function with more than one expression is like this:

Here's a typical function that calls two other functions and then ends.

Whatever the value returned by the final expression — here, the  function — that value is also returned by the   function.

You can use the  keyword to indicate a specific value to be returned:

julia> canpaybills(20) true julia> canpaybills(-10) false

Some consider it good style to always use a  statement, even if it's not strictly necessary. Later we'll see how to make sure that the function doesn't go adrift if you call it with the wrong type of argument.

Returning more than one value from a function
To return more than one value from a function, use a tuple (explored in more detail in a later chapter).

julia> doublesix (6, 6)

Here you could write  without parentheses.

Optional arguments and variable number of arguments
You can define functions with optional arguments, so that the function can use sensible defaults if specific values aren't supplied. You provide a default symbol and value in the argument list:

And when you call this function, if you don't provide a third value, the variable  defaults to 0 and uses that value inside the function.

julia> xyzpos(1,2) 1, 2, 0 julia> xyzpos(1,2,3) 1, 2, 3

Keyword and positional arguments
When you write a function with a long list of arguments like this:

sooner or later, you will forget the order in which you have to supply the arguments. For instance, it can be:

or

You can avoid this problem by using keywords to label arguments. Use a semicolon after the function's unlabelled arguments, and follow it with one or more  pairs:

When called, this function expects two arguments, and also accepts a number and a string, labelled  and. If you don't supply the keyword arguments, their default values are used:

julia> f(1,2) p is 1 q is 2 "r => 4, s => hello" julia> f("a", "b", r=pi, s=22//7) p is a q is b "r => π = 3.1415926535897..., s => 22//7"

If you supply a keyword argument, it can be anywhere in the argument list, not just at the end or in the matching place.

julia> f(r=999, 1, 2) p is 1 q is 2 "r => 999, s => hello" julia> f(s="hello world", r=999, 1, 2) p is 1 q is 2 "r => 999, s => hello world" julia>

When defining a function with keyword arguments, remember to insert a semicolon before the keyword/value pairs.

Here's another example from the Julia manual. The  keyword can appear anywhere in the list of arguments or it can be omitted:

julia> isapprox(3.0, 3.01, rtol=0.1) true julia> isapprox(rtol=0.1, 3.0, 3.01) true julia> isapprox(3.0, 3.00001) true

A function definition can combine all the different kinds of arguments. Here's one with normal, optional, and keyword arguments:

julia> f(1) normal argument: 1 optional argument: 2 keyword argument: foo julia> f(key=3, 1) normal argument: 1 optional argument: 2 keyword argument: 3 julia> f(key=3, 2, 1) normal argument: 2 optional argument: 1 keyword argument: 3

Functions with variable number of arguments
Functions can be defined so that they can accept any number of arguments:

The three dots indicate the famous splat. Here it means 'any', including 'none'. You can call this function with any number of arguments:

julia> fvar you supplied 0 arguments julia> fvar(64) you supplied 1 arguments argument 64 julia> fvar(64,65) you supplied 2 arguments argument 64 argument 65 julia> fvar(64,65,66) you supplied 3 arguments argument 64 argument 65 argument 66

and so on.

Here's another example. Suppose you define a function that accepts two arguments:

You can call this in the usual way:

julia> test(12, 34) x 12 y 34

If you have the two numbers, but in a tuple, then how can you supply a single tuple of numbers to this two argument function? Again, the answer is to use the ellipsis (splat).

julia> test((12, 34) ...) x 12 y 34

The use of the ellipsis or 'splat' is also referred to as 'splicing' the arguments:

julia> test([3,4]...) x 3 y 4

You can also do this:

julia> map(test, [3, 4]...) x 3 y 4

Local variables and changing the values of arguments
Any variable you define inside a function will be forgotten when the function finishes.

julia> test(1,2,3) 6 julia> subtotal LoadError: UndefVarError: subtotal not defined

If you want to keep values around across function calls, then you can think about using global variables.

A function can't modify an existing variable passed to it as an argument, but it can change the contents of a container passed to it. For example, here is a function that changes its argument to 5:

julia> x = 3 3 julia> set_to_5(x) 5 julia> x 3

Although the  inside the function is changed, the   outside the function isn't. Variable names in functions are local to the function.

But a function can modify the contents of a container, such as an array. This function uses the  syntax to access the contents of the container , rather than change the value of the variable  :

julia> x = collect(1:10); julia> fill_with_5(x) 5 julia> x 10-element Array{Int64,1}: 5 5 5 5 5 5 5 5 5 5

You can change elements of the array, but you can't change the variable so that it points to a different array. In other words, your function isn't allowed to change the binding of the argument.

Anonymous functions
Sometimes you don't want to worry about thinking up a cool name for a function. Anonymous functions — functions with no name — can be used in a number of places in Julia, such as with, and in list comprehensions.

The syntax uses, like this:

which defines a nameless function that takes an argument, calls it, and returns.

For example, the first argument of the  function is a function, and you can define an one-off function that exists just for one particular   operation:

julia> map(x -> x^2 + 2x - 1, [1,3,-1]) 3-element Array{Int64,1}: 2 14 -2

After the  finishes, both the function and the argument   have disappeared:

julia> x ERROR: x not defined

If you want an anonymous function that accepts more than one argument, provide the arguments as a tuple:

julia> map((x,y,z) -> x + y + z, [1,2,3], [4, 5, 6], [7, 8, 9]) 3-element Array{Int64,1}: 12 15  18

Notice that the results are 12, 15, 18, rather than 6, 15, and 24. The anonymous function takes the first value of each of the three arrays and adds them, followed by the second, then the third.

In addition, anonymous functions can have zero arguments, if you use an 'empty' tuple :

julia> random = -> rand(0:10) julia> random 3
 * 1) 3 (generic function with 1 method)

julia> random 1

Map
If you already have a function and an array, you can call the function for each element of the array by using. This calls the function on each element in turn, collects the results, and returns them in an array. This process is called mapping:

julia> a=1:10; julia> map(sin, a) 10-element Array{Float64,1}: 0.841471 0.909297  0.14112 -0.756802 -0.958924 -0.279415  0.656987  0.989358  0.412118 -0.544021

returns a new array but if you call , you modify the contents of the original array.

Often, you don't have to use  to apply a function like   to every member of an array, because many functions automatically operate "element-wise". The timings of the two different versions are similar ( has the edge perhaps, depending on the number of elements):

julia> @time map(sin, 1:10000); 0.149156 seconds (568.96 k allocations: 29.084 MiB, 2.01% gc time) julia> @time sin.(1:10000); 0.074661 seconds (258.76 k allocations: 13.086 MiB, 5.86% gc time)

collects the result of each application in an array and returns the array. Sometimes you might want the 'mapping' action but you don't want the results returned as an array. For this job, use :

julia> foreach(println, 1:20) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

and  is nothing (  is  ).

Map with multiple arrays
You can use  with more than one array. The function is applied to the first element of each of the arrays, then to the second, and so on. The arrays must be of the same length (unlike the  function, which is more tolerant).

Here's an example which generates an array of imperial (non-metric) spanner/socket sizes. The second array is just a bunch of repeated 32s to match the integers from 5 to 24 in the first array. Julia simplifies the rationals for us:

julia> map(//, 5:24, fill(32,20)) 20-element Array{Rational{Int64},1}: 5//32 3//16  7//32  1//4   9//32  5//16 11//32  3//8  13//32  7//16 15//32  1//2  17//32  9//16 19//32  5//8  21//32 11//16 23//32  3//4

(In reality, an imperial spanner set won't contain some of these strange sizes - I've never seen an old 17/32" spanner, but you can buy them online.)

Applying functions using the dot syntax
As well as, it's possible to apply functions directly to arguments that are arrays. See the section on Dot syntax for vectorizing functions.

Reduce and folding
The  function collects the results of some function working on each and every element of an iterable object, such as an array of numbers. The  function does a similar job, but after every element has been seen and processed by the function, only one is left. The function should take two arguments and return one. The array is reduced by continual application, so that just one is left.

A simple example is the use of  to sum the numbers in an iterable object (which works like the built-in function  ):

julia> reduce(+, 1:10) 55

Internally, this does something similar to this:

((((((((1 + 2) + 3) + 4) + 6) + 7) + 8) + 9) + 10)

After each operation adding two numbers, a single number is carried over to the next iteration. This process reduces all the numbers to a single final result.

A more useful example is when you want to apply a function to work on each consecutive pair in an iterable object. For example, here's a function that compares the length of two strings and returns the longer one:

julia> '''l(a, b) = length(a) > length(b) ? a : b''' l (generic function with 1 method)

This can be used to find the longest word in a sentence by working through the string, pair by pair:

julia> reduce(l, split("This is a sentence containing some very long strings")) "containing"

"This" lasts a few rounds, and is then beaten by "sentence", but finally "containing" takes the lead, and there are no other challengers after that. If you want to see the magic happen, redefine  like this:

julia> l(a, b) = (println("comparing \"$a\" and \"$b\""); length(a) > length(b) ? a : b) l (generic function with 1 method) julia> reduce(l, split("This is a sentence containing some very long strings")) comparing "This" and "is" comparing "This" and "a" comparing "This" and "sentence" comparing "sentence" and "containing" comparing "containing" and "some" comparing "containing" and "very" comparing "containing" and "long" comparing "containing" and "strings" "containing"

You can use an anonymous function to process an array pairwise. The trick is to make the function leave behind a value that will be used for the next iteration. This code takes an array such as  and returns , multiplying adjacent elements.

julia> store 9-element Array{Int64,1}: 2 6 12 20 30 42 56 72 90

Folding
Julia also offers two related functions,  and. These offer the same basic functionality as. The differences are concerned with the direction in which the traversal occurs. In the simple summation example above, our best guess at what happened inside the  operation assumed that the first pair of elements were added first, followed by the second pair, and so on. However, it's also possible that  started at the end and worked towards the front. If it's important, use  for left to right, and   for right to left. In many cases, the results are the same, but here's an example where you'll get different results depending on which version you'll use:

julia> reduce(-, 1:10) -53 julia> foldl(-, 1:10) -53 julia> foldr(-, 1:10) -5

Julia offers other functions in this group: check out,  , and.

If you want to use  and the   functions for functions that take only one argument, use a dummy second argument:

julia> reduce((x, y) -> sqrt(x), 1:4, init=256) 1.4142135623730951

which is equivalent to calling the  function four times:

julia> sqrt(sqrt(sqrt(sqrt(256)))) 1.4142135623730951

Functions that return functions
You can treat Julia functions in the same way as any other Julia object, particularly when it comes to returning them as the result of other functions.

For example, let's create a function-making function. Inside this function, a function called  is created, and this will raise its argument (y) to the number that was originally passed in as the argument x. This new function is returned as the value of the   function.

Now we can construct lots of exponent-making functions. First, let's build a  function:

julia> squarer = create_exponent_function(2)
 * 1) 8 (generic function with 1 method)

and a  function:

julia> cuber = create_exponent_function(3)
 * 1) 9 (generic function with 1 method)

While we're at it, let's do a "raise to the power of 4" function (called, although I'm starting to struggle with the Latin and Greek naming):

julia> quader = create_exponent_function(4)
 * 1) 10 (generic function with 1 method)

These are ordinary Julia functions:

julia> squarer(4) 16 julia> cuber(5) 125 julia> quader(6) 1296

The definition of the  above is perfectly valid Julia code, but it's not idiomatic. For one thing, the return value doesn't always need to be provided explicitly — the final evaluation is returned if  isn't used. Also, in this case, the full form of the function definition can be replaced with the shorter one-line version. This gives the concise version:

which acts in the same way.

julia> a = make_counter; julia> b = make_counter; julia> a 1 julia> a 2 julia> a 3 julia> a 4 julia> b 1 julia> b 2

Here's another example of making functions. To make it easier to see what the code is doing, here is the  function written in a slightly different manner:

julia> a = make_counter julia> a 1 julia> a 2 julia> a 3 julia> for i in 1:10           a       end julia> a 14
 * 1) 15 (generic function with 1 method)

Function chaining and composition
Functions in Julia can be used in combination with each other.

Function composition is when you apply two or more functions to arguments. You use the function composition operator to compose the functions. (You can type the composition operator at the REPL using ). For example, the  and   functions can be composed like this:

julia> (sqrt ∘ +)(3, 5) 2.8284271247461903

which adds the numbers first, then finds the square root.

This example composes three functions.

julia> map(first ∘ reverse ∘ uppercase, split("you can compose functions like this")) 6-element Array{Char,1}: 'U' 'N' 'E' 'S' 'E' 'S'

Function chaining (sometimes called "piping" or "using a pipe to send data to a subsequent function") is when you apply a function to the previous function's output:

julia> 1:10 |> sum |> sqrt 7.416198487095663

where the total produced by  is passed to the   function. The equivalent composition is:

julia> (sqrt ∘ sum)(1:10) 7.416198487095663

Piping can send data to a function that accepts a single argument. If the function requires more than one argument, you may be able to use an anonymous function:

julia> collect(1:9) |> n -> filter(isodd, n) 5-element Array{Int64,1}: 1 3  5  7  9

Methods
A function can have one or more different methods of doing a similar job. Each method usually concentrates on doing the job for a particular type.

Here is a function to check a longitude when you type in a location:

The message ("generic function with 1 method") you see if you define this in the REPL tells you that there is currently one way you can call the  function. If you call this function and supply a number, it works fine.

julia> check_longitude_1(-182) longitude -182 should be between -180 and 180 degrees julia> check_longitude_1(22) longitude 22 is a valid longitude

But what happens when you type in a longitude in, say, the format seen on Google Maps:

julia> check_longitude_1("1°24'54.6\"W") ERROR: MethodError: `isless` has no method matching isless(::Int64, ::UTF8String)

The error tells us that the function has stopped because the concept of less than, which we are using inside our function, makes no sense if one argument is a string and the other a number. Strings are not less than or greater than integers because they are two different things, so the function fails at that point.

Notice that the  function did start executing, though. The argument  could have been anything - a string, a floating point number, an integer, a symbol, or even an array. There are many ways for this function to fail. This is not the best way to write code!

To fix this problem, we might be tempted to add code that tests the incoming value, so that strings are handled differently. But Julia proposes a better alternative: methods and multiple dispatch.

In the case where the longitude is supplied as a numeric value, the  argument is defined as 'being of type Real'. Let's start again, define a new function, and do it properly:

Now this  function doesn't even run if the value in   isn't a real number. The problems of what to do when the value is a string is avoided. With a type Real, this particular method can be called with any argument provided that it is some kind of number.

We can use the  function to test this. lets you know whether you can apply a function to an argument — i.e. whether there is an available method for the function for arguments with that type:

julia> applicable(check_longitude, -30) true julia> applicable(check_longitude, pi) true julia> applicable(check_longitude, 22/7) true julia> applicable(check_longitude, 22//7) true julia> applicable(check_longitude, "1°24'54.6\"W") false

The  indicates that you can't pass a string value to the   function because there is no method for this function that accepts a string:

julia> check_longitude("1°24'54.6\"W") ERROR: MethodError: `check_longitude` has no method matching check_longitude(::UTF8String)

Now the body of the function isn't even looked at — Julia doesn't know a method for calling  function with a string argument.

The obvious next step is to add another method for the  function, only this time one that accepts a string argument. In this way, a function can be given a number of alternative methods: one for numeric arguments, one for string arguments, and so on. Julia selects and runs one of the available methods depending on the types of arguments you provide to a function.

This is multiple dispatch.

Now the  function has two methods. The code to run depends on the types of the arguments you provide to the function. And you can avoid testing the types of arguments at the start of this function, because Julia only dispatches the flow to the string-handling method if  is a string.

You can use the built-in  function to find out how many methods you have defined for a particular function.

julia> methods(check_longitude) check_longitude(loc::Real) at none:2 check_longitude(loc::String) at none:3
 * 1) 2 methods for generic function "check_longitude":

An instructive example is to see how many different methods the  function has:

julia> methods(+) [1] +(x::Bool, z::Complex{Bool}) in Base at complex.jl:276 [2] +(x::Bool, y::Bool) in Base at bool.jl:104 ... [174] +(J::LinearAlgebra.UniformScaling, B::BitArray{2}) in LinearAlgebra at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/LinearAlgebra/src/uniformscaling.jl:90 [175] +(J::LinearAlgebra.UniformScaling, A::AbstractArray{T,2} where T) in LinearAlgebra at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/LinearAlgebra/src/uniformscaling.jl:91 [176] +(a, b, c, xs...) in Base at operators.jl:466
 * 1) 176 methods for generic function "+":

This is a long list of every method currently defined for the  function; there are many different types of thing that you can add together, including arrays, matrices, and dates. If you design your own types, you might well want to write a function that adds two of them together.

Julia chooses "the most specific method" to handle the types of arguments. In the case of, we have two specific methods, but we could define a more general method:

This method of  is called when the   argument is neither a Real number or a String. It is the most general method, and won't be called at all if a more specific method is available.

Type parameters in method definitions
It's possible to work with type information in method definitions. Here's a simple example:

julia> test(2.3) 2.3 is a Float64 julia> test(2) 2 is a Int64 julia> test(.02) 0.02 is a Float64 julia> test(pi) π = 3.1415926535897... is a Irrational{:π}

julia> test(22//7) 22//7 is a Rational{Int64}

julia> test(0xff) 255 is a UInt8

The  method automatically extracts the type of the single argument   passed to it and stores it in the 'variable'. For this function, the definition of  was where T is a subtype of Real, so the type of T must be a subtype of the Real type (it can be any real number, but not a complex number). 'T' can be used like any other variable — in this method it's just printed out using string interpolation. (It doesn't have to be, but it nearly always is!)

This mechanism is useful when you want to constrain the arguments of a particular method definition to be of a particular type. For example, the type of argument  must belong to the Real number supertype, so this   method doesn't apply when   isn't a number, because then the type of the argument isn't a subtype of Real:

julia> test("str") ERROR: MethodError: no method matching test(::ASCIIString) julia> test(1:3) ERROR: MethodError: no method matching test(::UnitRange{Int64})

Here's an example where you might want to write a method definition that applies to all one-dimensional integer arrays. It finds all the odd numbers in an array:

julia> findodds(collect(1:20)) 10-element Array{Int64,1}: 1 3  5  7  9 11 13 15 17 19

but can't be used for arrays of real numbers:

julia> findodds([1, 2, 3, 4, 5, 6, 7, 8, 9, 10.0]) ERROR: MethodError: no method matching findodds(::Array{Float64,1}) Closest candidates are: findodds(::Array{T<:Integer,1}) where T<:Integer at REPL[13]:2

Note that, in this simple example, because you're not using the type information inside the method definition, you might be better off sticking to the simpler way of defining methods, by adding type information to the arguments:

But if you wanted to do things inside the method that depended on the types of the arguments, then the type parameters approach will be useful.