Functions
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-
let sqr x = x*x;;
let y= sqr 5;;
printfn "%d"y;;
System.Console.ReadKey(true);
Output-
Variables
A variable is a section of the computer's memory used to temporarily hold a value. Declaring a variable means that the variable has been reserved a portion of memory. The variable can be changed if you rpovide the Mutable keyword for that variable. In F# a Mutable variable has limited scope, either as a field of a type or as a local value. Mutable variables with a limited scope are easier to control and less likely to be modified in an incorrect manner.
To declare a variable, use the let operator, followed by a name for the variable and assign a value to it using the assignment operator "=". The Syntax for declaring a Variable is:
let VariableName= Value
Example-
let a = 2
You can also use a semicolon at the end for termination. A semicolon is optional.
let a = 2;
We declare and initialize a variable in one line, but if you want you can initialize it in next line.
let a =
2
Or you can use semicolan.
let a =
2;
Example-
//Variable Example
let a = 6
let b = 5
let c = a+ b
printfn "a: %i" a
printfn "b: %i" b
printfn "c: %i" c
Output-
Example-
Showing an integer variable:
let x = 5;
printfn "%d"x;;
System.Console.ReadKey(true);
Output-
Summary
In this article I have covered Functions and Variables.