ZetCode

FreeBasic Integer Keyword

last modified June 21, 2025

The FreeBasic Integer keyword represents a fundamental data type for storing whole numbers. Integers are essential for counting, indexing, and mathematical operations in programming.

Basic Definition

In FreeBasic, Integer is a 32-bit signed integer data type. It can store values from -2,147,483,648 to 2,147,483,647. Integers are stored in two's complement format.

Integer variables are used for calculations that don't require decimal precision. They are faster to process than floating-point numbers and consume less memory than larger numeric types.

Declaring Integer Variables

This example shows how to declare and initialize Integer variables.

integer_declare.bas
Dim age As Integer
Dim count As Integer = 10
Dim temperature As Integer = -5

Print "age: "; age
Print "count: "; count
Print "temperature: "; temperature

Here we declare three Integer variables. The first is uninitialized and contains an undefined value. The others are initialized with positive and negative values. Integer variables default to 0 if declared at module level.

Integer Arithmetic Operations

Integers support standard arithmetic operations like addition and division.

integer_arithmetic.bas
Dim a As Integer = 15
Dim b As Integer = 4

Print "a + b = "; a + b
Print "a - b = "; a - b
Print "a * b = "; a * b
Print "a / b = "; a / b
Print "a Mod b = "; a Mod b

This example demonstrates basic arithmetic with Integers. Note that division of Integers produces an Integer result (truncated). The Mod operator returns the remainder of division.

Integer Overflow

Integer variables can overflow if assigned values outside their range.

integer_overflow.bas
Dim bigNum As Integer = 2147483647
Print "Maximum Integer: "; bigNum

bigNum = bigNum + 1
Print "After overflow: "; bigNum

Adding 1 to the maximum Integer value causes overflow, wrapping around to the minimum value. This demonstrates why range checking is important when working with Integers.

Integer Type Conversion

FreeBasic automatically converts between Integer and other numeric types.

integer_conversion.bas
Dim intVal As Integer = 42
Dim dblVal As Double = 3.14159

Print "Double to Integer: "; CInt(dblVal)
Print "Integer to Double: "; CDbl(intVal)
Print "String to Integer: "; CInt("1234")

This shows explicit conversion between types using CInt and CDbl. When converting from floating-point, the value is rounded. String conversion parses numeric strings.

Integer Arrays

Arrays of Integers are useful for storing sequences of whole numbers.

integer_array.bas
Dim numbers(1 To 5) As Integer

For i As Integer = 1 To 5
    numbers(i) = i * 10
Next

For i As Integer = 1 To 5
    Print numbers(i)
Next

This creates an array of 5 Integers and fills it with multiples of 10. Arrays are zero-based by default, but we specified 1-based indexing here. Integer arrays are memory-efficient for numeric sequences.

Integer with Bitwise Operations

Integers support bit-level manipulation using bitwise operators.

integer_bitwise.bas
Dim a As Integer = &b1100 ' 12 in decimal
Dim b As Integer = &b1010 ' 10 in decimal

Print "a AND b: "; a And b
Print "a OR b: "; a Or b
Print "NOT a: "; Not a
Print "a XOR b: "; a Xor b
Print "a shifted left: "; a Shl 1
Print "b shifted right: "; b Shr 1

This demonstrates bitwise operations on Integers. The results are shown in decimal. Bitwise operations are useful for low-level programming and optimization tasks.

Integer in Function Parameters

Functions can accept and return Integer values.

integer_function.bas
Function Square(n As Integer) As Integer
    Return n * n
End Function

Function Sum(a As Integer, b As Integer) As Integer
    Return a + b
End Function

Print "Square of 5: "; Square(5)
Print "Sum of 3 and 7: "; Sum(3, 7)

These functions demonstrate passing and returning Integer values. Integer parameters are passed by value by default. The functions perform basic mathematical operations and return Integer results.

Best Practices

This tutorial covered the FreeBasic Integer keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.