a variable is a name given to a storage area that our programs can manipulate. each variable has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
variable declaration in f#
the let keyword is used for variable declaration −
for example,
let x = 10
it declares a variable x and assigns the value 10 to it.
you can also assign an expression to a variable −
let x = 10 let y = 20 let z = x + y
the following example illustrates the concept −
example
let x = 10 let y = 20 let z = x + y printfn "x: %i" x printfn "y: %i" y printfn "z: %i" z
when you compile and execute the program, it yields the following output −
x: 10 y: 20 z: 30
variables in f# are immutable, which means once a variable is bound to a value, it can’t be changed. they are actually compiled as static read-only properties.
the following example demonstrates this.
example
let x = 10 let y = 20 let z = x + y printfn "x: %i" x printfn "y: %i" y printfn "z: %i" z let x = 15 let y = 20 let z = x + y printfn "x: %i" x printfn "y: %i" y printfn "z: %i" z
when you compile and execute the program, it shows the following error message −
duplicate definition of value 'x' duplicate definition of value 'y' duplicate definition of value 'z'
variable definition with type declaration
a variable definition tells the compiler where and how much storage for the variable should be created. a variable definition may specify a data type and contains a list of one or more variables of that type as shown in the following example.
example
let x:int32 = 10 let y:int32 = 20 let z:int32 = x + y printfn "x: %d" x printfn "y: %d" y printfn "z: %d" z let p:float = 15.99 let q:float = 20.78 let r:float = p + q printfn "p: %g" p printfn "q: %g" q printfn "r: %g" r
when you compile and execute the program, it shows the following error message −
x: 10 y: 20 z: 30 p: 15.99 q: 20.78 r: 36.77
mutable variables
at times you need to change the values stored in a variable. to specify that there could be a change in the value of a declared and assigned variable, in later part of a program, f# provides the mutable keyword. you can declare and assign mutable variables using this keyword, whose values you will change.
the mutable keyword allows you to declare and assign values in a mutable variable.
you can assign some initial value to a mutable variable using the let keyword. however, to assign new subsequent value to it, you need to use the ← operator.
for example,
let mutable x = 10 x ← 15
the following example will clear the concept −
example
let mutable x = 10 let y = 20 let mutable z = x + y printfn "original values:" printfn "x: %i" x printfn "y: %i" y printfn "z: %i" z printfn "let us change the value of x" printfn "value of z will change too." x <- 15 z <- x + y printfn "new values:" printfn "x: %i" x printfn "y: %i" y printfn "z: %i" z
when you compile and execute the program, it yields the following output −
original values: x: 10 y: 20 z: 30 let us change the value of x value of z will change too. new values: x: 15 y: 20 z: 35