This is an old revision of the document!
Variables
Variables are simple containers for values, and are the core piece of any programming language. Variables can contain a wide variety data of different types. Types define what sort of data is contained inside a variable.
x = 1 // Variables can contain numbers x = 'Hello World' // Text a = 1 b = 2 c = a + b // Or even other variables result = someSubroutine //or Results to a function or subroutine, which we'll discuss in later chapeter.
Before a variable can be used, it must be declared. FreeBASIC provides several ways of declaring variables, but we will focus on
DIM
and
VAR
variables.
DIM
The keyword DIM is the variable declaration you will use the most in FreeBASIC. With DIM, you are required to declare what sort of data the variable will hold. For instance:
dim name as string dim age as integer dim radius as float dim as integer x,y
PITFALL While it may seem that
dim x,y as integer
will work, it will in fact cause a compile time error.
VAR
The second way to declare a variable is use of the keyword VAR. Unlike DIM, VAR does not require the type of data to be declared. While this may seem convenient, it comes with some trade-offs. For instance when mixing types. DIM also places everything into an array, which could be thought of as an ice tray with each cubby holding a bit of information. We'll discuss arrays in a later chapter.
Types
Understanding types is a key concept for understanding strongly typed languages such as FreeBASIC, C, Rust, Pascal, etc. Strongly typed languages require you to declare the type of value you wish to hold. There are a variety of types and each have its own set of characteristics. For the purpose of this tutorial we will primarly be focusing on the following:
Variable types | |
---|---|
integers | Any whole number that is neither a decimal nor a fraction |
float | Any number with a decimal place |
string | A string of characters |
Scope
Variables are said to be scoped depending on where they are within the program. They can either be local or global scoped. Local scoped variables can only be used in the function or subroutine they were initialized in. A global scoped variable can be used anywhere in the program. Don't worry if this doesn't click. We'll revisit this when we discuss functions and subroutines.
Exercise
For this exercise we will be writing a short program to explore variables and their behavior, as well as introduce your first function, print, which prints on the screen.
First, open up your preferred text editor and enter the following statement
dim a as integer dim b as integer dim c as integer a = 5 b = 4 c = a + b print "The number is " & c
Save your file as chap1.bas, and at the unix prompt type 'fbc chap1.bas' to compile the program. Type './chap1' to run the program. That was a lot so let's break down what we did.
First
dim a as integer dim b as integer dim c as integer
declares 3 variables of the integer type.
a = 5 b = 4 c = a + b
assigns values to our 3 variables and
print "The number is " & c
prints our result to the screen. Please note the '&' symbol. This concatenates things of different types together.
Play around with your program! Try changing the operands (+,*,-,/) and see what happens. Change the variable type. Is there any difference? Does it error? Why?
If you have questions, feel free to post them in BBOARD!