F# is a functional programming language that came with Visual Studio 2010. Functional programming is a programming paradigm that treats computations as the evaluations of mathematical functions. A function is a central point of a functional language. Functions are a first class part of the F# type system. It can perform an action, produce a value and define a purpose. Functions play an important role throughout the language; for example the type "char-> int" represents a F# function that takes a char and returns an int.
A Function is a type that gives the name functional programming to F#. Functions can be used in the same way as any other type. They can be given as an argument to other functions or returned from a function as a result and the function's type can be used as a Type argument to generic types. For example you can create a list of Functions. Although unlike .Net delegates, F# functions have the following two important differences:
-
The first difference is that Functions are not Nominal. Any Function that takes a char and returns an int is of Type "char-> int" whereas multiple differently named delegates may be used to represent functions of this signature and are not interchangeable.
-
The second difference is that Functions are designed to efficiently support either partial or full application.
The F# language provides many already created Functions, referred to as built-in Functions. printfn is one of the built-in Functions in F#.
Example-
// Built-in Function Example
printfn "This is showing Built-in Function"
Example
To declare a Function in F# we use the Let keyword.

// Function Example for addition substraction
let add a b = a + b
let sub a b = a - b
let printThreeNumbers number1 number2 number3 =
printfn "number1: %i" number1
printfn "number2: %i" number2
printfn "number3: %i" number3
printThreeNumbers 6 (add 42 6) (sub 42 6)
Output-

Example-






Join the conversation! Your thoughts help the community grow.