# Recursive function

**URL:** <https://discuss.ocaml.org/t/recursive-function/5884>\
**Category:** Learning\
**Created:** [May 31, 2020, 1:48pm UTC](https://discuss.ocaml.org/t/recursive-function/5884 "2020-05-31T13:48:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahana123](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sahana123](https://discuss.ocaml.org/u/sahana123)\
**Post date:** [May 31, 2020, 1:48pm UTC](https://discuss.ocaml.org/t/recursive-function/5884/1 "2020-05-31T13:48:19Z")

</div>

I have a function x that sequentially calls two other functions f1 and f1 for eg. , passes the results of f1 to f2 and repeats the process by calling itself.

```
let requestHandler option1 = 
  Printf.printf "Inside RequestHandler : %s\n" option1 ;
  option1
 
let valueStore option2 =
  Printf.printf "Inside valueStore : %s\n" option2
  
let rec main =
  (* print_endline(res); *)
  let vall1 = requestHandler "stringVar" in 
  valueStore vall1 ;
  main

```

But this throws error .

```
Entering directory '/home/ocaml/'
File "/client.ml", lines 13-15, characters 2-6:
13 | ..let vall1 = requestHandler "stringVar" in 
14 | valueStore vall1 ;
15 | main
Error: This kind of expression is not allowed as right-hand side of `let rec'
```

---

<div class="post-metadata">

**Author:** ![drbb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/drbb/32/2177_2.png) [@drbb](https://discuss.ocaml.org/u/drbb)\
**Post date:** [May 31, 2020, 2:06pm UTC](https://discuss.ocaml.org/t/recursive-function/5884/2 "2020-05-31T14:06:41Z")

</div>

You can’t have a function with zero arguments. You must use unit to have this effect.  
Define main as:

```
let rec main () = ...

```

and call it like this:

```
main ()
```

---

<div class="post-metadata">

**Author:** ![sahana123](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sahana123](https://discuss.ocaml.org/u/sahana123)\
**Post date:** [May 31, 2020, 6:23pm UTC](https://discuss.ocaml.org/t/recursive-function/5884/3 "2020-05-31T18:23:04Z")

</div>

After defining the function with unit , it compiles and executes too but doesn’t print anything, and also it terminates which should not happen as it is being called recursively.

---

<div class="post-metadata">

**Author:** ![drbb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/drbb/32/2177_2.png) [@drbb](https://discuss.ocaml.org/u/drbb)\
**Post date:** [May 31, 2020, 6:35pm UTC](https://discuss.ocaml.org/t/recursive-function/5884/4 "2020-05-31T18:35:53Z")

</div>

Most likely you forgot to call main. If you are compiling add this as the last line:

```
let () = main ()
```
