Skip to content

ocaml.org: Function can have multiple arguments #1

Description

@eprochasson

I'm not sure I follow what you say when you write A function always has exactly one parameter, and returns one result.. Functions can have multiple arguments:

(* In the REPL *)
utop# min;;
- : 'a -> 'a -> 'a = <fun>

min takes 2 arguments of type 'a and returns one value of type 'a.
They can then be partially applied:

utop # let my_min = min 2;;
val my_min : int -> int = <fun>

returns a new function that takes one argument and return one value (the smallest one between the argument and 2), that one can use like:

my_min 1
-: int = 1

my_min 3
-: int = 2

More directly

utop # let my_fn s1 s2 = printf "Argument 1: %s, Argument 2: %s\n" s1 s2;;
val my_fn : string -> string -> unit = <fun>

utop # my_fn "hello" "world";;
Argument 1: hello, Argument 2: world
- : unit = ()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions