Adding two numbers is one of the simplest things you can do in F#, but it is also one of the most important. F# is a functional-first programming language, which means it encourages clean, clear, and expressive code. Learning how basic math works in F# helps you understand how values, expressions, and functions behave, which is essential as you move forward.
You will use addition in many real situations such as calculating totals, scores, prices, measurements, or results from formulas. Even complex programs rely on simple operations like adding numbers. In this guide, we will slowly and clearly explore how to add two numbers in F#, using examples that are easy for absolute beginners to understand.
Program 1: Adding Two Integers
This program shows how to add two whole numbers in F#. The numbers are predefined so you can focus on the syntax and result.
let firstNumber = 10
let secondNumber = 20
let sum = firstNumber + secondNumber
printfn "%d" sumIn this example, values are stored using let, which is how variables are created in F#. The plus sign adds the two integers, and printfn prints the result. This is the most basic and common way to add numbers in F#.
Program 2: Adding Two Floating-Point Numbers
This program demonstrates how to add decimal numbers, also known as floating-point values.
let price = 12.5
let tax = 2.75
let totalAmount = price + tax
printfn "%f" totalAmountF# treats decimal values as floating-point numbers by default. The addition works the same way as with integers, but the output format changes to display decimals. This is useful in real-life programs such as billing systems or measurements.
Program 3: Adding an Integer and a Float Together
This program shows how to add a whole number and a decimal number in F#.
let itemCount = 5
let extraItems = 2.5
let totalItems = float itemCount + extraItems
printfn "%f" totalItemsF# is strict about types, so the integer is converted to a float using float. This helps avoid mistakes and keeps the code safe. Beginners quickly learn how important type conversion is when working with mixed values.
Program 4: Adding Numbers Using a Function
This program places the addition logic inside a reusable function.
let addNumbers numberOne numberTwo =
numberOne + numberTwo
let result = addNumbers 15 25
printfn "%d" resultFunctions are a core part of F#. This function takes two numbers and returns their sum. Using functions makes code reusable and easier to read, which is very helpful as programs grow larger.
Program 5: Adding Numbers from User Input
This program allows the user to enter two numbers and adds them together.
printf "Enter the first number: "
let firstInput = System.Console.ReadLine() |> int
printf "Enter the second number: "
let secondInput = System.Console.ReadLine() |> int
let sumResult = firstInput + secondInput
printfn "Sum: %d" sumResultUser input is read as text, so it must be converted into numbers before addition. This example uses int to convert the input. This approach is very common in console applications and helps beginners understand how programs interact with users.
Program 6: Adding Values Inside a Main Program Block
This program shows how addition fits into a complete F# console application.
[<EntryPoint>]
let main argv =
let a = 8
let b = 12
let sum = a + b
printfn "Result: %d" sum
0The main function is the entry point of the program. Inside it, values are added just like in earlier examples. This helps beginners see how simple operations work inside a full program structure.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about adding numbers in F# and clears up confusion.
Q1. Why does F# require type conversion for mixed numbers?
F# is strongly typed to prevent errors, so it requires you to be clear about number types.
Q2. Can F# add very large numbers?
Yes, F# supports large numeric types, but you must choose the correct type.
Q3. Is addition different in functional programming?
The operation is the same, but it is often used inside expressions and functions.
Q4. Why use functions for simple addition?
Functions make code reusable and easier to test and maintain.
Q5. Can I add numbers in F# without variables?
Yes, F# allows direct expressions, but variables improve readability.
Conclusion
Adding two numbers in F# is simple, clear, and very powerful once you understand the basics. In this guide, you learned how to add integers, floating-point numbers, mixed values, user input, and values inside functions and full programs.
The best way to get comfortable with F# is to practice. Try changing the numbers, using different types, and writing your own small programs. With time and practice, basic addition will feel natural, and you will be ready to explore more advanced features of F# with confidence.




