1
votes

I am very new to OCaml and am trying to write a function that takes a string and an int. It prints out "Hello, " + the string passed in and then it returns 1+ the int. I have this so far but it doesn't seem to be working:

let random_func s n = print_string "Hello, "; print_string s;1+n;;

Any ideas what I'm doing wrong?

2

2 Answers

1
votes

Your code works for me exactly as you give it:

# let random_func s n = print_string "Hello, "; print_string s;1+n;;
val random_func : string -> int -> int = <fun>
# random_func "yow" 4;;
Hello, yow- : int = 5

Perhaps the problem is in intepreting the output of the toplevel? The output of the function is immediately followed by the toplevel output.

0
votes

If you are using your code in a program (and not in the toplevel) and you want the output to be immediately visible (for example before getting some input from the user) you may also need to flush the buffer:

let random_func s n =
  print_string "Hello, ";
  print_string s;
  flush stdout;
  1+n
;;

In any case, this has nothing to do with the fact that your function returns an int.