|
| 1 | +# Introduction |
| 2 | + |
| 3 | +The [`Basics`][basics] Concept introduced two ways to define a function. |
| 4 | + |
| 5 | +Most generally, the multiline form: |
| 6 | + |
| 7 | +```julia |
| 8 | +function muladd(x, y, z) |
| 9 | + x * y + z |
| 10 | +end |
| 11 | +``` |
| 12 | + |
| 13 | +The "assignment", or "single-line" form for short definitions: |
| 14 | +```julia |
| 15 | +muladd(x, y, z) = x * y + z |
| 16 | +``` |
| 17 | + |
| 18 | +In a third and even shorter form, a short, single-use function can be created without a name: |
| 19 | + |
| 20 | +```julia-repl |
| 21 | +julia> map(x -> 2x, 1:3) |
| 22 | +4-element Vector{Int64}: |
| 23 | + 2 |
| 24 | + 4 |
| 25 | + 6 |
| 26 | +
|
| 27 | +julia> map((x, y) -> x * y, 1:3, 4:6) |
| 28 | +3-element Vector{Int64}: |
| 29 | + 4 |
| 30 | + 10 |
| 31 | + 18 |
| 32 | +``` |
| 33 | + |
| 34 | +In this case, `x -> 2x` is an "anonymous function". |
| 35 | +This is equivalent to what some other languages call a "lambda function". |
| 36 | + |
| 37 | +Note that multiple arguments need parentheses, as in `(x, y) -> x * y`. |
| 38 | + |
| 39 | +Anonymous functions are common in Julia code, especially when combined with higher-order functions such as `map()` and `filter()` (which will be covered in more detail in a later concept). |
| 40 | + |
| 41 | +```julia-repl |
| 42 | +julia> map(x -> x^4, [1, 2, 3]) |
| 43 | +3-element Vector{Int64}: |
| 44 | + 1 |
| 45 | + 16 |
| 46 | + 81 |
| 47 | +``` |
| 48 | + |
| 49 | +## Function arguments |
| 50 | + |
| 51 | +So far in the syllabus, we have only looked at functions which have a precise number of arguments, and require function calls to supply all of them, in the correct order. |
| 52 | +This would be limiting and inconvenient, so there are several other options. |
| 53 | + |
| 54 | +### Optional arguments |
| 55 | + |
| 56 | +Like many languages, Julia allows function definitions to supply default values for individual arguments. |
| 57 | + |
| 58 | +Function call can then either supply a value for that argument, or omit it and rely on the default. |
| 59 | + |
| 60 | +```julia-repl |
| 61 | +julia> f(x, y=10) = x * y |
| 62 | +f (generic function with 2 methods) |
| 63 | +
|
| 64 | +julia> f(2, 3) |
| 65 | +6 |
| 66 | +
|
| 67 | +julia> f(2) |
| 68 | +20 |
| 69 | +``` |
| 70 | + |
| 71 | +All arguments _without_ defaults must come before any arguments _with_ defaults, meaning that `f(x=2, y)` would be invalid. |
| 72 | + |
| 73 | +### Keyword arguments |
| 74 | + |
| 75 | +All the examples so far use `positional arguments`, where values supplied in a function call must match the order of the corresponding arguments in the function definition. |
| 76 | + |
| 77 | +Like many languages, Julia also allows `keyword arguments`. |
| 78 | +Function calls must specify the argument name, but multiple keyword arguments can then be specified in any order. |
| 79 | + |
| 80 | +A distinctive feature of Julia is that the keyword arguments (if any) in the function definition must be preceded by a semicolon `;` to separate them from any positional arguments. |
| 81 | +A function call can use either `;` or `,` between the last positional argument and the first keyword argument. |
| 82 | + |
| 83 | +```julia-repl |
| 84 | +julia> b(x; y) = x + y |
| 85 | +b (generic function with 1 method) |
| 86 | +
|
| 87 | +julia> b(2, y=3) |
| 88 | +5 |
| 89 | +
|
| 90 | +# keyword is required when calling |
| 91 | +julia> b(2, 3) |
| 92 | +ERROR: MethodError: no method matching b(::Int64, ::Int64) |
| 93 | +The function `b` exists, but no method is defined for this combination of argument types. |
| 94 | +``` |
| 95 | + |
| 96 | +Default values can optionally be specified, exactly as for positional arguments. |
| 97 | + |
| 98 | +It is common to end up with syntax like `myarg=myarg` within a function call, when a variable with the same name as the parameter was pre-calculated. |
| 99 | +A shorthand syntax is allowed in this situation: |
| 100 | + |
| 101 | +```julia-repl |
| 102 | +julia> width = 4.0 |
| 103 | +4.0 |
| 104 | +
|
| 105 | +julia> height = √ width |
| 106 | +2.0 |
| 107 | +
|
| 108 | +julia> area(; width, height) = width * height |
| 109 | +area (generic function with 1 method) |
| 110 | +
|
| 111 | +# repetition |
| 112 | +julia> area(; width=width, height=height) |
| 113 | +8.0 |
| 114 | +
|
| 115 | +# shorthand form |
| 116 | +julia> area(; width, height) |
| 117 | +8.0 |
| 118 | +``` |
| 119 | + |
| 120 | +### Splat and slurp |
| 121 | + |
| 122 | +These are the standard names for a useful aspect of Julia syntax, in case you wondered. |
| 123 | +Both refer to the `...` operator. |
| 124 | + |
| 125 | +#### Splat |
| 126 | + |
| 127 | +Splatting is used in function _calls_, to expand collections into individual values required by the function. |
| 128 | + |
| 129 | +This may be easier to demonstrate than to explain: |
| 130 | + |
| 131 | +```julia-repl |
| 132 | +julia> fxyz(x, y, z) = x * y * z |
| 133 | +fxyz (generic function with 1 method) |
| 134 | +
|
| 135 | +julia> xyz = [2, 3, 4] |
| 136 | +3-element Vector{Int64}: |
| 137 | + 2 |
| 138 | + 3 |
| 139 | + 4 |
| 140 | +
|
| 141 | +# Using the vector directly in a function call is invalid |
| 142 | +julia> fxyz(xyz) |
| 143 | +ERROR: MethodError: no method matching fxyz(::Vector{Int64}) |
| 144 | +The function `fxyz` exists, but no method is defined for this combination of argument types. |
| 145 | +
|
| 146 | +# splatting converts the vector to 3 numbers, used as positional argumants |
| 147 | +julia> fxyz(xyz...) |
| 148 | +24 |
| 149 | +``` |
| 150 | + |
| 151 | +Some "function calls" are hidden by syntactic sugar, so splatting can also be used in less obvious ways. |
| 152 | + |
| 153 | +For example, multiple assignment uses a tuple constructor function internally: |
| 154 | + |
| 155 | +```julia-repl |
| 156 | +julia> first, rest... = [1, 2, 3, 4] |
| 157 | +4-element Vector{Int64}: |
| 158 | + 1 |
| 159 | + 2 |
| 160 | + 3 |
| 161 | + 4 |
| 162 | +
|
| 163 | +julia> first |
| 164 | +1 |
| 165 | +
|
| 166 | +julia> rest |
| 167 | +3-element Vector{Int64}: |
| 168 | + 2 |
| 169 | + 3 |
| 170 | + 4 |
| 171 | +``` |
| 172 | + |
| 173 | +Keyword arguments can also be supplied by splatting, typically using a `named tuple`. |
| 174 | +A `Dict` will also work, but the keys must be symbols (strings will not work). |
| 175 | + |
| 176 | +```julia-repl |
| 177 | +# function with 3 keyword arguments |
| 178 | +julia> fabc(; a, b, c) = a + b + c |
| 179 | +fabc (generic function with 1 method) |
| 180 | +
|
| 181 | +# named tuple |
| 182 | +julia> abc_nt = (a=2, b=3, c=4) |
| 183 | +(a = 2, b = 3, c = 4) |
| 184 | +
|
| 185 | +# there are no positional arguments, so need to use ; before kw argument |
| 186 | +julia> fabc(;abc_nt...) |
| 187 | +9 |
| 188 | +
|
| 189 | +# Dict |
| 190 | +julia> abc_dict = Dict(:a=>2, :b=>3, :c=>4) |
| 191 | +Dict{Symbol, Int64} with 3 entries: |
| 192 | + :a => 2 |
| 193 | + :b => 3 |
| 194 | + :c => 4 |
| 195 | +
|
| 196 | +julia> fabc(;abc_dict...) |
| 197 | +9 |
| 198 | +``` |
| 199 | + |
| 200 | +#### Slurp |
| 201 | + |
| 202 | +Slurping is used in the function _definition_, to pack an arbitrary number of individual values into a collection. |
| 203 | + |
| 204 | +```julia-repl |
| 205 | +julia> f_more(i, j, more...) = i + j + sum(more) |
| 206 | +f_more (generic function with 1 method) |
| 207 | +
|
| 208 | +julia> f_more(1, 3, 5, 7, 9, 11) |
| 209 | +36 |
| 210 | +``` |
| 211 | + |
| 212 | +The name of the slurped argument (in this case `more`) is not significant. |
| 213 | +The type of this variable is chosen by the compiler, but for positional arguments is likely to be `tuple` or something similar. |
| 214 | + |
| 215 | +Keyword arguments can also be slurped, giving a `Dict` (or similar). |
| 216 | + |
| 217 | +```julia-repl |
| 218 | +julia> f_kwslurp(x, y; switches...) = :mult in keys(switches) ? x * y : x + y |
| 219 | +f_kwslurp (generic function with 1 method) |
| 220 | +
|
| 221 | +julia> f_kwslurp(5, 6; mult=true) |
| 222 | +30 |
| 223 | +
|
| 224 | +julia> f_kwslurp(5, 6) |
| 225 | +11 |
| 226 | +``` |
| 227 | + |
| 228 | +Any keyword arguments can be used in the call. |
| 229 | +It is for the function definition to decide which keywords to respond to and which to ignore. |
| 230 | + |
| 231 | + |
| 232 | +[basics]: https://exercism.org/tracks/julia/concepts/basics |
0 commit comments