Functions are the fundamental unit of program execution in any programming language.
You define functions by using the let
keyword, or, if the function is recursive, the let rec
keyword combination. The basic syntax of a function is as follows.
let [inline] function-name parameter-list [ : return-type ] = function-body
// Recursive function definition.
let rec function-name parameter-list = recursive-function-body
function-name
is an identifier that represents the function.parameter-list
consists of successive parameters that are separated by spaces.function-body
consists of an expression that makes up the function body is typically a compound expression consisting of many expressions that culminate in a final expression that is the return value.return-type
is a colon followed by a type and is optional.The following is the simple function definition.
let myFunc x = x + 1
In the above example, myFunc
is the function name, x
is the argument that has type int
, x + 1
is the function body, and the return value is of type int
.
You can specify a type for a parameter, as shown in the following example.
let myFunc (x : int) = x + 1
If you omit the type for the parameter, the parameter type is inferred by the compiler.
A function body can contain definitions of local variables and functions. Such variables and functions are in scope in the body of the current function but not outside it.
let myFunc1 x y =
let z = 3
x*y + z
You can call a function by specifying the function name followed by a space and then any arguments separated by spaces.
let val1 = myFunc1 3 7
let val2 = myFunc1 9 2
Console.WriteLine(val1)
Console.WriteLine(val2)
Recursive functions are functions that call themselves. You define a recursive using the let rec
keyword combination.
The following recursive function computes the nth Fibonacci number.
let rec fib n =
if n < 2 then 1
else fib (n - 1) + fib (n - 2)