# F# Variables

Throughout this article, we will examine F# variables and learn how they can be used in various programming scenarios and how to make use of them in our programming projects.

We will also explore the various types of variables used in F# and learn about their properties and how they can be used in our code.

F# is a programming language that makes use of variables, which store values and can be changed or updated at any time throughout the program.

There are several types of variables that can be declared in F#, such as integers, floating-point numbers, strings, and so forth.

Variables can be declared by using the let keyword, and they can be of any type.

## F# Variable Declaration

A unique feature of the variable declarations in F# is their immutability by default, which means once a value is assigned to a variable, it cannot be changed after it has been assigned.

Unlike other programming languages where variables have the ability to be changed and updated throughout the program, this is a distinct difference in F#.

Amongst the many features of functional programming in F# immutability is one of the most important, since it makes it easier to reason about code and avoid bugs caused by the unexpected modification of values in the code.

A variable is declared in F# by using a ‘let’ keyword, followed by the name of the variable and the value that it should hold.

The following example shows how you can declare a variable in F# using the following syntax:

`let a = 5`

A variable ‘a’ is declared and a value of 5 is assigned to it as a value.

The expression can also be assigned to a variable in the following manner:

```let a = 5
let b = 12
let c = a + b```

#### Example:

let a = 5 let b = 12 let c = a + b printfn "Variable a is: %i" a printfn "Variable b is: %i" b printfn "The sum of a + b into a variable c is: %i" c
After executing it, you will see the following results:
```Variable a is: 5
Variable b is: 12
The sum of a + b into a variable c is: 17```

Another example of demonstrating variable are follows:

#### Example:

let a = 2 let b = 3 let c = a * b printfn "Variable a is: %i" a printfn "Variable b is: %i" b printfn "The multiplication of a * b into a variable c is: %i" c
In order to demonstrate immutability, we will use the following example.

#### Example:

let a = 5 let b = 12 let c = a + b printfn "Variable a is: %i" a printfn "Variable b is: %i" b printfn "The sum of a + b into a variable c is: %i" c let a = 12 let b = 5 let c = a + b printfn "Variable a is: %i" a printfn "Variable b is: %i" b printfn "The sum of a + b into a variable c is: %i" c
```Duplicate definition of value a
Duplicate definition of value b
Duplicate definition of value c```

## Variable Type Declaration

By defining variable types explicitly, it can prevent programming errors and improve the readability of the code as well. Furthermore, it can also assist the F# compiler in producing more efficient code, because it can detect the type of data more clearly.

Variable type declarations indicate how much and where storage should be allocated for a variable.

In F#, the type of a variable can be inferred based on the variables usage, so type declarations are not always required. A type declaration is generally good practice when dealing with complex or ambiguous code in order to ensure clarity and accuracy.

Here is an example of how a variable definition specifies a data type and contains one or more variables of that type.

#### Example:

let a:int32 = 12 let b:int32 = 5 let c:int32 = a – b printfn "Value of a with type int32 is: %d\n" a // '\n' is use for new line printfn "Value of b with type int32 is: %d\n" b printfn "Value of subtraction in c with type int32 is: %d\n" c let d:float = 6.45 let e:float = 5.33 let f:float = d * e printfn "Value of d with type float is: %g" d printfn "Value of e with type float is: %g\n" e printfn "Value of f with type float is: %g" f
After compiling the program, you should be able to see the following results:
```Value of a with type int32 is: 12
Value of b with type int32 is: 5
Value of subtraction in c with type int32 is: 7

Value of d with type float is: 6.45
Value of e with type float is: 5.33
Value of f with type float is: 34.3785```

The following is another example of a variable type declaration:

#### Example:

let x:int32 = 7 let y:float = 34.3785 let text:string = "String variable" printfn "The value of x with type of int32 is: %d \n" x printfn "The value of y with type of float is: %g \n" y printfn "The value of text with type of string is: %s" text
```The value of x with type of int32 is: 7
The value of y with type of float is: 34.3785
The value of text with type of string is: String variable```

## Mutable Variables

There are times when you need to change the values that are stored in a variable.

When it comes to declaring a variable that may change in value in an later part of the program, F# provides the mutable keyword to specify that the value of the variable can change at some point.

The keyword mutable allows you to declare and assign variables with mutable values that can be changed at any time.

The mutable keyword allows you to declare mutable variables, and it allows you to assign values to them.

By using the let keyword, you are able to create some initial value and assign it to a mutable variable.

A new value can be assigned to it by using the a ← operator.

For example:

```let mutable a = 2
a → 4```

Here is an example that clarifies the concept of a mutable variable in a simple way:

#### Example:

let a = 7 let mutable b = 12 let mutable c = a + b printfn "Original Values:" printfn "\nValue of a: %i" a printfn "Value of mutable b: %i" b printfn "Sum of mutable c: %i" c printfn "\nWe'll change the value of b, which will change the value of c as well" b <- 8 c <- a + b printfn "\nNew Values after changing:" printfn "\nValue of a after change: %i" a printfn "Value of b after change: %i" b printfn "Sum of c after change: %i" c

Output:

```Original Values:

Value of a: 7
Value of mutable b: 12
Sum of mutable c: 19

We'll change the value of b, which will change the value of c as well

New Values after changing:

Value of a after change: 7
Value of b after change: 8
Sum of c after change: 15```

### Example Explanation

In above example we have defined three variables: ‘a‘, ‘b‘, and ‘c‘. The variable ‘a’ is assigned 7 and is not mutable, which means it cannot be changed later. The variables ‘b’ and ‘c’ are declared mutable, which means their values can be changed later.

The value of ‘b’ is initially set to 12, and the value of ‘c’ is set to the sum of ‘a’ and ‘b’ (i.e., 7 + 12 = 19).

The next few lines of code use the printfn function to print out the original values of ‘a’, ‘b’, and ‘c’.

The code then changes the value of ‘b’ to 8, which means its new value is 8. The value of ‘c’ is then updated to be the sum of ‘a’ and ‘b’ again (i.e., 7 + 8 = 15).

The last few lines of code use the ‘printfn’ function again to print out the new values of ‘a’, ‘b’, and ‘c’.

So, when the code is executed, it prints the original values of ‘a’, ‘b’, and ‘c’ followed by the updated values of ‘a’, ‘b’, and ‘c’ after changing the value of ‘b’.

If you liked this article and found it informative regarding F# functional programming language, you can leave your feedback by reacting below.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0 