March 2019
Intermediate to advanced
208 pages
5h 11m
English
Let’s jump right in and define a function that takes the average of two floating-point numbers and binds that function to the symbol avg:
| | let avg = (a, b) => { |
| | (a +. b) /. 2.0; |
| | }; |
A function definition begins with the parameters in parentheses, followed by a thick arrow =>, then the body of the function enclosed in braces. The last expression in the braces (in this case, the only expression) is the function’s return value.
Once you have avg defined, you can call the function in your code. In fact, ReasonML requires you to define your functions before you use them:
| | let result = avg(3.0, 4.5); |
| | Js.log(result); |
And here it is in action:
| |
Read now
Unlock full access