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;;
Result:
- : 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.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.