A function is an object that gets a number of arguments (the argument list, arglist
) as the input, then does something with these values in the function body, and returns none, one, or more value(s). Multiple arguments are separated by commas (,
) in arglist
(in fact, they form a tuple, as do the return values; refer to the Tuples section of Chapter 5, Collection Types). The arguments are also optionally typed, and the type(s) can be user-defined. The general syntax is as follows:
function fname(arglist) # function body... return value(s) end
A function's argument list can also be empty, then it is written as fname()
.
Here is a simple example:
# code in chapter 3\functions101.jl function mult(x, y) println("x is $x and y is $y") return x * y end
Function names such as mult
are by convention in lower case, without underscores. They can contain Unicode characters, which are useful in mathematical notations. The return
keyword in the last line is optional...