🎓 A1Learner
🔍
0 of 54 lessons completed

Haskell Function Arguments

A function argument is a value passed to a function for one of its inputs. A function can take several arguments, one for each input.

sum3 adds three arguments

The definition lists three inputs. The call passes three arguments in the same order.

sum3 x y z = x + y + z
main :: IO ()
main = print (sum3 1 2 3)

The first argument fills the first input

minus subtracts its second input from its first input. Here 3 is subtracted from 10.

minus x y = x - y
main :: IO ()
main = print (minus 10 3)

Parentheses group a call as one argument

The inner call add 2 3 is grouped in parentheses. Its result 5 becomes the argument to double.

add x y = x + y
double x = x * 2
main :: IO ()
main = print (double (add 2 3))

Example

add x y = x + y
main :: IO ()
main = print (add (-2) 5)

Key points

  • Argument: an argument is a value passed to a function at a call.
  • Several arguments: a definition can list several inputs before its equals sign.
  • Order: the arguments in a call fill the inputs in the same order.
  • Spaces: spaces separate the arguments in a call.
  • Negative numbers: parentheses around a negative number make it one argument.
  • Function calls: parentheses around a call make its result one argument.

In this example, add takes two arguments, the parentheses make -2 one argument, and adding -2 and 5 gives 3, which print writes.

Note

a Haskell call uses no commas between its arguments.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Haskell Playground

Exercise

For the definition minus x y = x - y, what does print (minus 4 9) print?