# Calling functions by a string variable?

**URL:** <https://discuss.ocaml.org/t/calling-functions-by-a-string-variable/697>\
**Category:** Learning\
**Created:** [August 11, 2017, 1:40pm UTC](https://discuss.ocaml.org/t/calling-functions-by-a-string-variable/697 "2017-08-11T13:40:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuren-Zhong](https://avatars.discourse-cdn.com/v4/letter/y/7cd45c/32.png) [@Yuren-Zhong](https://discuss.ocaml.org/u/Yuren-Zhong)\
**Post date:** [August 11, 2017, 1:40pm UTC](https://discuss.ocaml.org/t/calling-functions-by-a-string-variable/697/1 "2017-08-11T13:40:08Z")

</div>

Is it possible in Ocaml to call a function by giving a string variable.  
If there is no existing libraries, how can I do this by myself?

Thanks!

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [August 11, 2017, 3:00pm UTC](https://discuss.ocaml.org/t/calling-functions-by-a-string-variable/697/2 "2017-08-11T15:00:45Z")

</div>

You mean like

```auto
let func = "do_something" in
func ()

```

?

The above itself is not possible, but you can achieve something similar by putting functions in a map-like (dictionary) structure and indexing them by their names, e.g.

```auto
let () =
  let funcs = ["do_something", fun () -> print_endline "Doing something"] in
  try (List.assoc "do_something" funcs) ()
  with Not_found -> print_endline "Could not find function"

```
