# The 'do'-keyword when it doesn't create an anonymous function and pass it as the first argument to a function call

**URL:** https://discourse.julialang.org/t/the-do-keyword-when-it-doesnt-create-an-anonymous-function-and-pass-it-as-the-first-argument-to-a-function-call/94471
**Category:** New to Julia
**Created:** [February 11, 2023, 2:52pm UTC](https://discourse.julialang.org/t/the-do-keyword-when-it-doesnt-create-an-anonymous-function-and-pass-it-as-the-first-argument-to-a-function-call/94471 "2023-02-11T14:52:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kingoslo](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@kingoslo](https://discourse.julialang.org/u/kingoslo)
#### Post date: [February 11, 2023, 2:52pm UTC](https://discourse.julialang.org/t/the-do-keyword-when-it-doesnt-create-an-anonymous-function-and-pass-it-as-the-first-argument-to-a-function-call/94471/1 "2023-02-11T14:52:40Z")

</div>

Hello there,

I am trying to learn Julia, and I came by the do keyword in a setting that I do not understand. So I am asking for some help to work out what happens here.

```julia
HDF5.h5open(fname, "r") do fd
#=
lots of stuff involving fd
=#
end

```

I can see here that the [do keyword makes an anonymous function](https://docs.julialang.org/en/v1/base/base/#do) and pass it as the first argument to the function on the left hand side of the do keyword. But the documentation says that the [HDF5.h5open](https://juliaio.github.io/HDF5.jl/stable/#Opening-and-closing-files) function accepts exactly two arguments, and neither should be a function. So in this case the `do`-keyword must have some other meaning.

Could you please explain?

Kind regards

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [February 11, 2023, 3:00pm UTC](https://discourse.julialang.org/t/the-do-keyword-when-it-doesnt-create-an-anonymous-function-and-pass-it-as-the-first-argument-to-a-function-call/94471/2 "2023-02-11T15:00:58Z")

</div>

It seems more likely that the `h5open` documentation is incomplete than the `do` keyword does something undocumented. Based on how the standard `open` function works, I would expect the above to effectively do

```julia
fd = HDF5.h5open(fname, "r")
#=
lots of stuff involving fd
=#
close(fd)

```

with the twist that the final `close(fd)` is run also when `lots of stuff involving fd` is interrupted by an error.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 11, 2023, 3:09pm UTC](https://discourse.julialang.org/t/the-do-keyword-when-it-doesnt-create-an-anonymous-function-and-pass-it-as-the-first-argument-to-a-function-call/94471/3 "2023-02-11T15:09:07Z")

</div>

> <https://github.com/JuliaIO/HDF5.jl/blob/bbb6b269f01a1f7dc1512a6e299dc59d94aad9cb/src/file.jl#L80-L91>

---

<div class="post-metadata">

### Author: ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)
#### Post date: [October 29, 2024, 9:49pm UTC](https://discourse.julialang.org/t/the-do-keyword-when-it-doesnt-create-an-anonymous-function-and-pass-it-as-the-first-argument-to-a-function-call/94471/4 "2024-10-29T21:49:44Z")

</div>

> [@kingoslo](#):
>
> the do keyword in a setting that I do not understand.

“do” comes from julia itself:

```julia
help?> do
search: do Docs download DomainError @doc Cdouble stdout @ __dot__ ReadOnlyMemoryError pseudos fieldoffset isreadonly dropdims istaskdone fdio

  do

  Create an anonymous function and pass it as the first argument to a function call. For example:

  map(1:10) do x
      2x
  end

  is equivalent to map(x->2x, 1:10).

  Use multiple arguments like so:

  map(1:10, 11:20) do x, y
      x + y
  end

```

When you are unsure about something in julia, do not hesitate to use the help by entering `?` in a julia REPL.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 29, 2024, 9:54pm UTC](https://discourse.julialang.org/t/the-do-keyword-when-it-doesnt-create-an-anonymous-function-and-pass-it-as-the-first-argument-to-a-function-call/94471/5 "2024-10-29T21:54:59Z")

</div>

I would prefer if you don’t quote me partially to completely change the meaning of what I wrote.
