Skip to content

Commit 71b5dba

Browse files
mark-summerfieldKristofferC
authored andcommitted
Roll up #29389
1 parent d99a5b4 commit 71b5dba

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

doc/src/manual/functions.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [Functions](@id man-functions)
22

33
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
55
by the global state of the program. The basic syntax for defining functions in Julia is:
66

77
```jldoctest
@@ -35,7 +35,7 @@ julia> f(2,3)
3535
```
3636

3737
Without parentheses, the expression `f` refers to the function object, and can be passed around
38-
like any value:
38+
like any other value:
3939

4040
```jldoctest fofxy
4141
julia> g = f;
@@ -241,7 +241,7 @@ as arguments. A classic example is [`map`](@ref), which applies a function to ea
241241
an array and returns a new array containing the resulting values:
242242

243243
```jldoctest
244-
julia> map(round, [1.2,3.5,1.7])
244+
julia> map(round, [1.2, 3.5, 1.7])
245245
3-element Array{Float64,1}:
246246
1.0
247247
4.0
@@ -254,7 +254,7 @@ situations, the anonymous function construct allows easy creation of a single-us
254254
without needing a name:
255255

256256
```jldoctest
257-
julia> map(x -> x^2 + 2x - 1, [1,3,-1])
257+
julia> map(x -> x^2 + 2x - 1, [1, 3, -1])
258258
3-element Array{Int64,1}:
259259
2
260260
14
@@ -317,15 +317,19 @@ The components of tuples can optionally be named, in which case a *named tuple*
317317
constructed:
318318

319319
```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
322325
323326
julia> x.a
324-
1
327+
2
325328
```
326329

327330
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]`).
329333

330334
## Multiple Return Values
331335

@@ -363,7 +367,7 @@ julia> y
363367
6
364368
```
365369

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:
367371

368372
```julia
369373
function foo(a,b)
@@ -508,8 +512,9 @@ call will fail, just as it would if too many arguments were given explicitly.
508512

509513
## Optional Arguments
510514

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)
513518
from `Dates` module constructs a `Date` type for a given year `y`, month `m` and day `d`.
514519
However, `m` and `d` arguments are optional and their default value is `1`.
515520
This behavior can be expressed concisely as:
@@ -522,11 +527,11 @@ function Date(y::Int64, m::Int64=1, d::Int64=1)
522527
end
523528
```
524529

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}`.
527532

528533
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:
530535

531536
```jldoctest
532537
julia> using Dates
@@ -792,8 +797,8 @@ julia> sin.(A)
792797
```
793798

794799
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.
797802

798803
More generally, `f.(args...)` is actually equivalent to `broadcast(f, args...)`, which allows
799804
you to operate on multiple arrays (even of different shapes), or a mix of arrays and scalars (see

0 commit comments

Comments
 (0)