Subtracting Numbers in F#

Subtracting Numbers in F#

Subtracting numbers in F# is a basic skill, but it plays a very important role in real programs. Any time you calculate what is left, compare values, track changes, or reduce a total, subtraction is involved. Even though it looks simple, learning subtraction in F# helps you understand how expressions, types, and functions work together in a functional programming style.

F# is designed to be clear and safe, which means it often asks you to be very precise about your data. This is a good thing for beginners because it helps prevent mistakes early. In this article, we will gently walk through different ways to subtract numbers in F#, using simple examples and clear explanations that are easy to follow.

Program 1: Subtracting Two Integers

This program shows how to subtract one whole number from another using predefined values. It focuses on the basic syntax used in F#.

let totalMarks = 80
let lostMarks = 15

let finalMarks = totalMarks - lostMarks
printfn "%d" finalMarks

Here, values are created using let, which is how F# defines variables. The minus sign subtracts the second value from the first, and printfn prints the result. This pattern is very common when working with scores, counts, or remaining values.

Program 2: Subtracting Floating-Point Numbers

This program demonstrates subtraction using decimal numbers.

let bankBalance = 250.75
let spentAmount = 45.50

let remainingBalance = bankBalance - spentAmount
printfn "%f" remainingBalance

F# handles floating-point numbers smoothly when both values are decimals. The subtraction works just like integer subtraction, but the output shows decimal precision. This is useful for money-related calculations and measurements.

Program 3: Subtracting Mixed Numbers

This program shows how to subtract a decimal number from an integer.

let totalDistance = 100
let coveredDistance = 37.5

let remainingDistance = float totalDistance - coveredDistance
printfn "%f" remainingDistance

F# requires both values to be of the same type, so the integer is converted to a float using float. This makes the subtraction safe and clear. Beginners quickly learn that explicit type conversion helps avoid hidden errors.

Program 4: Subtracting Numbers Using a Function

This program places the subtraction logic inside a function so it can be reused.

let subtractValues firstValue secondValue =
    firstValue - secondValue

let result = subtractValues 50 20
printfn "%d" result

Functions are central to F#. This function takes two numbers and returns their difference. Using functions makes code cleaner and easier to reuse in larger programs.

Program 5: Subtracting Numbers from User Input

This program allows the user to enter two numbers and subtracts the second from the first.

printf "Enter the first number: "
let firstInput = System.Console.ReadLine() |> int

printf "Enter the second number: "
let secondInput = System.Console.ReadLine() |> int

let difference = firstInput - secondInput
printfn "Result: %d" difference

User input is read as text, so it must be converted into numbers before subtraction. This example uses int to convert the input safely. It is a common pattern in F# console applications and helps beginners understand real user interaction.

Program 6: Subtracting Values Inside the Main Program

This program shows subtraction inside a complete F# console application.

[<EntryPoint>]
let main argv =

    let startingValue = 30
    let reducedBy = 12
    let finalValue = startingValue - reducedBy

    printfn "Final value: %d" finalValue
    0

The main function is where the program starts running. Inside it, subtraction works the same way as in earlier examples. This helps beginners see how simple operations fit into full programs.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about subtracting numbers in F# and clears up confusion.

Q1. Why does F# require type conversion for mixed subtraction?
F# is strongly typed, so it requires both values to be the same type to avoid mistakes.

Q2. Can F# subtract negative numbers?
Yes, F# fully supports negative numbers in subtraction.

Q3. Is subtraction different in functional programming?
The operation is the same, but it is often used inside expressions and functions.

Q4. Can I subtract numbers without variables in F#?
Yes, but using variables makes the code easier to read and understand.

Q5. Is subtraction slower in F# compared to other languages?
No, subtraction is very fast and efficient in F#.

Conclusion

Subtracting numbers in F# is simple once you understand the basics of variables, types, and expressions. In this article, you learned how to subtract integers, floating-point numbers, mixed values, user input, and values inside functions and full programs.

The best way to become comfortable with subtraction in F# is to practice. Try changing the values, experimenting with different types, and writing your own small programs. With regular practice, subtraction will feel natural, and you will be ready to explore more advanced F# concepts with confidence.

Scroll to Top