1
1
# [ Functions] (@id man-functions)
2
2
3
3
In Julia, a function is an object that maps a tuple of argument values to a return value. Julia
4
- functions are not pure mathematical functions, in the sense that functions can alter and be affected
4
+ functions are not pure mathematical functions, because they can alter and be affected
5
5
by the global state of the program. The basic syntax for defining functions in Julia is:
6
6
7
7
``` jldoctest
@@ -35,7 +35,7 @@ julia> f(2,3)
35
35
```
36
36
37
37
Without parentheses, the expression ` f ` refers to the function object, and can be passed around
38
- like any value:
38
+ like any other value:
39
39
40
40
``` jldoctest fofxy
41
41
julia> g = f;
@@ -241,7 +241,7 @@ as arguments. A classic example is [`map`](@ref), which applies a function to ea
241
241
an array and returns a new array containing the resulting values:
242
242
243
243
``` jldoctest
244
- julia> map(round, [1.2,3.5,1.7])
244
+ julia> map(round, [1.2, 3.5, 1.7])
245
245
3-element Array{Float64,1}:
246
246
1.0
247
247
4.0
@@ -254,7 +254,7 @@ situations, the anonymous function construct allows easy creation of a single-us
254
254
without needing a name:
255
255
256
256
``` jldoctest
257
- julia> map(x -> x^2 + 2x - 1, [1,3, -1])
257
+ julia> map(x -> x^2 + 2x - 1, [1, 3, -1])
258
258
3-element Array{Int64,1}:
259
259
2
260
260
14
@@ -317,15 +317,19 @@ The components of tuples can optionally be named, in which case a *named tuple*
317
317
constructed:
318
318
319
319
``` jldoctest
320
- julia> x = (a=1, b=1+1)
321
- (a = 1, b = 2)
320
+ julia> x = (a=2, b=1+2)
321
+ (a = 2, b = 3)
322
+
323
+ julia> x[1]
324
+ 2
322
325
323
326
julia> x.a
324
- 1
327
+ 2
325
328
```
326
329
327
330
Named tuples are very similar to tuples, except that fields can additionally be accessed by name
328
- using dot syntax (` x.a ` ).
331
+ using dot syntax (` x.a ` ) in addition to the regular indexing syntax
332
+ (` x[1] ` ).
329
333
330
334
## Multiple Return Values
331
335
@@ -363,7 +367,7 @@ julia> y
363
367
6
364
368
```
365
369
366
- You can also return multiple values via an explicit usage of the ` return ` keyword:
370
+ You can also return multiple values using the ` return ` keyword:
367
371
368
372
``` julia
369
373
function foo (a,b)
@@ -508,8 +512,9 @@ call will fail, just as it would if too many arguments were given explicitly.
508
512
509
513
## Optional Arguments
510
514
511
- In many cases, function arguments have sensible default values and therefore might not need to
512
- be passed explicitly in every call. For example, the function [ ` Date(y, [m, d]) ` ] ( @ref )
515
+ It is often possible to provide sensible default values for function arguments.
516
+ This can save users from having to pass every argument on every call.
517
+ For example, the function [ ` Date(y, [m, d]) ` ] ( @ref )
513
518
from ` Dates ` module constructs a ` Date ` type for a given year ` y ` , month ` m ` and day ` d ` .
514
519
However, ` m ` and ` d ` arguments are optional and their default value is ` 1 ` .
515
520
This behavior can be expressed concisely as:
@@ -522,11 +527,11 @@ function Date(y::Int64, m::Int64=1, d::Int64=1)
522
527
end
523
528
```
524
529
525
- Observe, that this definition calls another method of ` Date ` function that takes one argument
526
- of ` UTInstant{Day} ` type .
530
+ Observe, that this definition calls another method of the ` Date ` function that takes one argument
531
+ of type ` UTInstant{Day} ` .
527
532
528
533
With this definition, the function can be called with either one, two or three arguments, and
529
- ` 1 ` is automatically passed when any of the arguments is not specified:
534
+ ` 1 ` is automatically passed when only one or two of the arguments are specified:
530
535
531
536
``` jldoctest
532
537
julia> using Dates
@@ -792,8 +797,8 @@ julia> sin.(A)
792
797
```
793
798
794
799
Of course, you can omit the dot if you write a specialized "vector" method of ` f ` , e.g. via ` f(A::AbstractArray) = map(f, A) ` ,
795
- and this is just as efficient as ` f.(A) ` . But that approach requires you to decide in advance
796
- which functions you want to vectorize .
800
+ and this is just as efficient as ` f.(A) ` . The advantage of the ` f.(A) ` syntax is that which functions are vectorizable need not be decided upon
801
+ in advance by the library writer .
797
802
798
803
More generally, ` f.(args...) ` is actually equivalent to ` broadcast(f, args...) ` , which allows
799
804
you to operate on multiple arrays (even of different shapes), or a mix of arrays and scalars (see
0 commit comments