4

In other languages, we can have a function which takes no arguments. Can we have 0 argument function in ocaml?

UnSat
  • 1,347
  • 2
  • 14
  • 28

3 Answers3

7

Functions in OCaml have exactly one argument (ignoring complications due to optional arguments). So, you can't have a function with no arguments.

As @alfa64 says, you could consider a simple value as a function with no arguments. But it will always have the same value (which, in fact, makes it similar to a pure function).

If you want to write a function that doesn't actually require any arguments (one that has side effects, presumably), it is traditional to use () as its argument:

# let p () = Printf.printf "hello, world\n";;
val p : unit -> unit = <fun>
# p ();;
hello, world
- : unit = ()
# 
Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
4

In OCaml functions always have an arguments. Therefore, we might wonder how to translate the following say_hello C function in OCaml:

void
say_hello()
{
    printf("Hello, world!\n");
}

There is a special type unit in OCaml which has only one value, written as (). While it might look odd and useless, it adds regularity to the language: a function not needing a specific argument can just take an argument of type unit, a function not returning a useful value usually returns a value of type unit. Here is how to translate the above say_hello function to OCaml:

# let say_hello () = print_endline "Hello, world!";;
val say_hello : unit -> unit = <fun>

Incidentally, template based meta-programming would be much easier in C++ if there were no type void but a similar unit type instead. It is quite common to treat separately functions having no arguments in template specialisations.


Object methods, while being similar to functions, do not require an argument.

# class example =
object
  method a =
    print_endline "This demonstrates a call to method a of class example"
end;;
        class example : object method a : unit end
# let x = new example;;
val x : example = <obj>
# x # a ;;
This demonstrates a call to method a of class example
- : unit = ()
Michaël Le Barbier
  • 6,103
  • 5
  • 28
  • 57
  • Could you expand on the object method case? I just wrote a parameterless serial number method, but I am wondering how does it fit into the picture? – Str. Jan 05 '15 at 20:39
  • @Str. I am not sure what you are interested in. Could you be more specific? – Michaël Le Barbier Jan 05 '15 at 23:18
  • 1
    The fact is standing rock solid: every function takes one argument. Methods without arguments "being called" or evaluated just don't fit here, there must be some quite different working behind it. Or "how can it be that a method does not need a single argument"? – Str. Jan 05 '15 at 23:27
  • 1
    @Str. I am still not sure what you are asking. If you are wondering about the apparent inconsistency in language design (why functions need an argument while methods do not), methods not requiring an argument is useful when building aggregates: it is nicer to write `obj # member # action` than `obj # member () # action ()`. – Michaël Le Barbier Jan 15 '15 at 08:06
0

Instead of

let foo n = 55

You just

let foo = 55

And then call foo wherever.

AlfredoVR
  • 4,069
  • 3
  • 25
  • 33
  • Type of first 'foo' is function while type of second one is 'int'. I would like to know if there can be a function which takes no argument. – UnSat Jan 04 '15 at 06:04
  • 1
    Also, that's clearly value-typed, not function-typed. Assuming the right hand side is any kind of effectful expression, the side-effects will be applied at the time of declaration, not at the time of the evaluation of references of `foo`. – Gian Jan 04 '15 at 06:07