This tutorial explains how to declare a variable in F# language
What is a variable in F#?
variables are basic blocks of a programming language
Variables are memory locations to store a value temporarily. It has a datatype that tells the amount of memory required for storage. Variables are immutable, which means once a variable is assigned, can not be changed
What are the types of variables in F#?
Two types of variables - mutable and immutable
- A mutable variable can change its values once the initial value is assigned, Variables are declared using the
mutable
keyword - Immutable variables: These values can not be changed once the variable is assigned with an initialized value. By default variables are immutable
How to Declare a variable in F#?
Variables are declared using the type
and let
keywords.
Variables by default are immutable variables. immutable variable values can not be changed during program execution once the value is assigned.
Use the mutable
keyword to change the variable to mutable.
Syntax:
//Immutable Variables
let variablename: datatype;
//Immutable Variables declared with literal values
let variablename = value;
//Mutable Variables
let mutable variablename: datatype;
//Mutable Variables declared with literal values
let mutable variablename = value;
let variablename = value;
, here no declared, type is inferred from the value assigned.
Let’s see examples
Immutable variables
examples
//int variables
let number1 = 11
let number2: int32 = 125
let result= number1*number2
printfn "result: %d " result
//string variables
let str = "hello"
printfn "%s " str
//float variables
let price: float = 12.11
printfn "%g " price
Compiled and output is
result: 1375
hello
12.11
Next, Change the declared variable value with the assignment, The Variable print the old value only. immutable variables are not assigned with new values.
//int variables
let number1 = 20
printfn "number1: %d " number1 // Prints 20
number1=25
printfn "number1: %d " number1 // Prints 20
Mutable variables
examples
Variables declared with the mutable
keyword.
Mutable variables change their value using the <-
operator, It does not change its value if you assign it with the =
operator.
Mutable variables are assigned its initial value and change its value multiple times during program execution
let mutable number1 = 11
printfn "number1: %d " number1 // prints 11
number1=25
printfn "number1: %d " number1 // prints 11
number1<-35
printfn "number1: %d " number1// prints 11
How to declare multiple variables in F
The let
keyword declares a variable, and assigns its value during the declaration
let number: int32=100
let number1 =101
multiple variables can be declared by a comma-separated with values enclosed in ()
, separated by a comma.
//multiple variables declaration
let n1, n2, n3 = (11, 12, 13)
printfn "number1: %d " n1
printfn "number2: %d " n2
printfn "number3: %d " n3
How to declare and assign function to a variable in F
F# allows you to assign functions to a variable.
Here is a syntax
let
declares a function with arguments.
let functionaname arguments= function body
Here is a function assignment variable
increment is a function that contains one argument number. The function body increments its value by 1 and returns it.
The function is called with an argument and prints the value.
let increment number =
number + 1
let result=increment(12)
printfn "Result: %d " result
If Function contains more than one argument, you can enclose in ()
with arguments separated by a comma.
let add (number1,number2) =
number1 + number2
let result=add(12,23)
printfn "Result: %d " result
Another feature, add argument types and return types in functions. It is also called type annotations.
let add (number1: int,number2: int): int =
number1 + number2
let result=add(12,23)
printfn "Result: %d " result
F# Variable Names
Variable names are human-readable strings that contain alphanumeric strings. It does not start with a number. reserved and keywords are not used as variable names