# 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 brewcoffee function — that value is also returned by the breakfast function.
You can use the return keyword to indicate a specific value to be returned:
Some consider it good style to always use a return 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).
Here you could write 6, 6 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 z defaults to 0 and uses that value inside the function.

### 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 keyword=value pairs:
When called, this function expects two arguments, and also accepts a number and a string, labelled r and s. If you don't supply the keyword arguments, their default values are used:
If you supply a keyword argument, it can be anywhere in the argument list, not just at the end or in the matching place.
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 rtol keyword can appear anywhere in the list of arguments or it can be omitted:
A function definition can combine all the different kinds of arguments. Here's one with normal, optional, and keyword arguments:

### 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:
and so on.
Here's another example. Suppose you define a function that accepts two arguments:
You can call this in the usual way:
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).
The use of the ellipsis or 'splat' is also referred to as 'splicing' the arguments:
You can also do this:

### Local variables and changing the values of arguments

Any variable you define inside a function will be forgotten when the function finishes.
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:
Although the x inside the function is changed, the x 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 x, rather than change the value of the variable x:
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 map, and in list comprehensions.
The syntax uses ->, like this:
which defines a nameless function that takes an argument, calls it x, and returns x^2 + 2x - 1.
For example, the first argument of the map function is a function, and you can define an one-off function that exists just for one particular map operation:
After the map finishes, both the function and the argument x have disappeared:
If you want an anonymous function that accepts more than one argument, provide the arguments as a tuple:
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:

### Map

If you already have a function and an array, you can call the function for each element of the array by using map. This calls the function on each element in turn, collects the results, and returns them in an array. This process is called mapping:
map returns a new array but if you call map! , you modify the contents of the original array.
Often, you don't have to use map to apply a function like sin to every member of an array, because many functions automatically operate "element-wise". The timings of the two different versions are similar (sin. has the edge perhaps, depending on the number of elements):
map 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 foreach:
and ans is nothing (ans == nothing is true).

#### Map with multiple arrays

You can use map 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 zip 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:
(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 map, 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 map function collects the results of some function working on each and every element of an iterable object, such as an array of numbers. The reduce 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 reduce to sum the numbers in an iterable object (which works like the built-in function sum):
Internally, this does something similar to this:
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:
This can be used to find the longest word in a sentence by working through the string, pair by pair:
"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 l like this:
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 [1, 2, 3, 4, 5, 6...] and returns [1 * 2, 2 * 3, 3 * 4, 4 * 5...], multiplying adjacent elements.

#### Folding

Julia also offers two related functions, foldl and foldr. These offer the same basic functionality as reduce. 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 reduce 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 reduce started at the end and worked towards the front. If it's important, use foldl for left to right, and foldr 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 offers other functions in this group: check out mapreduce, mapfoldl, and mapfoldr.
If you want to use reduce and the fold- functions for functions that take only one argument, use a dummy second argument:
which is equivalent to calling the sqrt function four times:

### 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 newfunction 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 create_exponent_function function.
Now we can construct lots of exponent-making functions. First, let's build a squarer function:
and a cuber function:
While we're at it, let's do a "raise to the power of 4" function (called quader, although I'm starting to struggle with the Latin and Greek naming):
These are ordinary Julia functions:
The definition of the create_exponent_function 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 return 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.
Here's another example of making functions. To make it easier to see what the code is doing, here is the make_counter function written in a slightly different manner:

### 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 \circ). For example, the sqrt and + functions can be composed like this:
which adds the numbers first, then finds the square root.
This example composes three functions.
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:
where the total produced by sum is passed to the sqrt function. The equivalent composition is:
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:

## 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 check_longitude_1 function. If you call this function and supply a number, it works fine.
But what happens when you type in a longitude in, say, the format seen on Google Maps:
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 check_longitude_1 function did start executing, though. The argument loc 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 loc argument is defined as 'being of type Real'. Let's start again, define a new function, and do it properly:
Now this check_longitude function doesn't even run if the value in loc 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 applicable function to test this. applicable 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:
The false indicates that you can't pass a string value to the check_longitude function because there is no method for this function that accepts a string:
Now the body of the function isn't even looked at — Julia doesn't know a method for calling check_longitude function with a string argument.
The obvious next step is to add another method for the check_longitude 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 check_longitude 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 loc is a string.
You can use the built-in methods function to find out how many methods you have defined for a particular function.
An instructive example is to see how many different methods the + function has:
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 check_longitude, we have two specific methods, but we could define a more general method:
This method of check_longitude is called when the loc 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:
The test method automatically extracts the type of the single argument a passed to it and stores it in the 'variable' T. For this function, the definition of T 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 T, 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 a must belong to the Real number supertype, so this test method doesn't apply when a isn't a number, because then the type of the argument isn't a subtype of Real:
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:
but can't be used for arrays of real numbers:
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.