TExcelTAD
Home › OCaml › OCaml Values and Functions

OCaml Values and Functions

A value is the result of evaluating an expression: 42, "hi", [1; 2; 3], or a function. let gives a value a name. Names are bindings, not boxes: once bound, x always refers to the same value.

ocaml · try it yourself
let width = 8
let height = 5
let area = width * height

let () = Printf.printf "area = %d\n" area
area = 40

In the toplevel you would see the inferred type of each binding:

ocaml
# let width = 8;;
val width : int = 8
# let area = width * height;;
val area : int = 40

Defining functions

A function definition lists its parameters after the name, separated by spaces:

ocaml · try it yourself
let square x = x * x
let add a b = a + b
let describe name age = name ^ " is " ^ string_of_int age

let () =
  print_int (square 9);
  print_newline ();
  print_int (add 2 3);
  print_newline ();
  print_endline (describe "Grace" 85)
81
5
Grace is 85

There is no return keyword: the body is an expression and its value is the result. The inferred types are:

ocaml
val square : int -> int
val add : int -> int -> int
val describe : string -> int -> string

Read int -> int -> int as "takes an int, then another int, and returns an int". The compiler worked this out from the *, + and ^ operators, which only work on ints and strings.

Anonymous functions

fun creates a function without a name. let square x = x * x is just shorthand for let square = fun x -> x * x:

ocaml · try it yourself
let cube = fun x -> x * x * x

let () =
  print_int (cube 3);
  print_newline ();
  List.iter (fun word -> print_endline ("- " ^ word)) [ "map"; "filter"; "fold" ]
27
- map
- filter
- fold

Anonymous functions are most useful as arguments to other functions, like List.iter above, which calls the function once for each list element.

Functions are values

Because functions are ordinary values, you can store them, pass them and return them:

ocaml · try it yourself
let twice f x = f (f x)
let increment n = n + 1
let shout s = s ^ "!"

let () =
  Printf.printf "%d\n" (twice increment 10);
  Printf.printf "%s\n" (twice shout "hey")
12
hey!!

twice works with any function whose input and output types are the same. Its inferred type is ('a -> 'a) -> 'a -> 'a, where 'a ("alpha") is a type variable meaning "any type". You will meet these in the polymorphism lesson.

Functions with no arguments

A function must take at least one argument. When there is nothing useful to pass, take (), the only value of type unit:

ocaml · try it yourself
let counter_start () = 100
let say_hello () = print_endline "hello"

let () =
  say_hello ();
  print_int (counter_start ());
  print_newline ()
hello
100

Note: let start = 100 is a value computed once; let start () = 100 is a function that computes it each time it is called. The difference matters when the body has side effects, such as printing or reading a clock.

Common mistakes

Mistake Fix
add(2, 3) add 2 3 (parentheses with a comma build a tuple)
square -4 square (-4)
print_int add 2 3 print_int (add 2 3)
Forgetting () when calling say_hello say_hello ()

Test yourself

What is the type of let add a b = a + b?

  1. int * int -> int
  2. int -> int -> int
  3. 'a -> 'a -> 'a
  4. int -> int

Which expression is equivalent to let double x = x * 2?

  1. let double = fun x -> x * 2
  2. let double = x * 2
  3. let double () = x * 2
  4. fun double x = x * 2

How do you call let greet () = print_endline "hi"?

  1. greet
  2. greet ()
  3. greet() is invalid
  4. call greet