OCaml - Function - Creating an easy function

In this tutorial we will see how to create a function in OCaml language.
It will be an easy function to understand how it works.

We will create a function that returns an int + 1.
Here the code:

# let myFunc myVar = myVar + 1;;
Notice that you can also create a function like this:
# let myFunc = fun myVar -> myVar + 1;;
Press enter, it will display:
val myFunc : int -> int = <fun>
It means that the myFunc value is the name of the function, the first int is the argument (in our case the myVar value).
The second int, with an arrow (->) before, means that the return value will be of type int.
And the last element, "= <fun>" tells us that this is a function.
So to use this function, simply type it:
# myFunc 4;;
- : int = 5
Well done, it was your first function in OCaml! wink


Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.