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 an arglist
(in fact, they form a tuple, as do the return values; refer to theTuples section ofChapter 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; in this case, it is written as fname()
.
The following is a simple example:
# code in 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. They can contain Unicode characters, which are useful in mathematical notations. The return
keyword in the last line is optional; we...