fb_variables
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
fb_variables [2022/12/03 05:55] – praetor | fb_variables [2022/12/04 01:35] – [CONST] praetor | ||
---|---|---|---|
Line 13: | Line 13: | ||
</ | </ | ||
- | Before a variable can be used, it must be **declared**. FreeBASIC provides several ways of declaring variables, but we will focus on < | + | Before a variable can be used, it must be **declared**. FreeBASIC provides several ways of declaring variables, but we will focus on < |
==== DIM ==== | ==== 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: | 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 as string |
- | dim age as integer | + | dim as integer |
- | dim radius | + | dim as float radius |
dim as integer x,y | 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 ==== | ==== 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. | 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. | ||
+ | |||
+ | ==== CONST ==== | ||
+ | CONST variables are constants, that is their assigned value does not change. An example would be | ||
+ | <code freebasic> | ||
+ | const as float pi = 3.14 | ||
+ | </ | ||
=== Types === | === Types === | ||
Line 46: | Line 45: | ||
=== Scope === | === 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' | + | 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. VAR variables are always locally scoped. Don't worry if this doesn' |
===== Exercise ===== | ===== Exercise ===== | ||
Line 53: | Line 52: | ||
First, open up your preferred text editor and enter the following statement | First, open up your preferred text editor and enter the following statement | ||
- | < | + | < |
dim a as integer | dim a as integer | ||
dim b as integer | dim b as integer | ||
Line 69: | Line 68: | ||
First | First | ||
- | < | + | < |
dim a as integer | dim a as integer | ||
dim b as integer | dim b as integer | ||
Line 76: | Line 75: | ||
declares 3 variables of the integer type. | declares 3 variables of the integer type. | ||
- | < | + | < |
a = 5 | a = 5 | ||
b = 4 | b = 4 | ||
Line 83: | Line 82: | ||
assigns values to our 3 variables and | assigns values to our 3 variables and | ||
- | < | + | < |
print "The number is " & c | print "The number is " & c | ||
</ | </ |
fb_variables.txt · Last modified: 2022/12/04 01:39 by praetor