User Tools

Site Tools


fb_variables

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
fb_variables [2022/12/03 05:27] – [DIM] praetorfb_variables [2022/12/04 01:39] (current) – [CONST] praetor
Line 10: Line 10:
 c = a + b          // Or even other variables c = a + b          // Or even other variables
  
-result = someSubroutine //or Results to a function or subroutine, which we'll discuss in later chapeter.+result = someSubroutine //or Results to a function or subroutine, which we'll discuss in later chapeters.
 </code> </code>
  
-Before a variable can be used, it must be **declared**. FreeBASIC provides several ways of declaring variables, but we will focus on <code>DIM</code> and <code>VAR</code> variables.+Before a variable can be used, it must be **declared**. FreeBASIC provides several ways of declaring variables, but we will focus on <code>DIM</code><code>VAR</code> and <code>CONST</code> variables.
  
 ==== 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:
  
-<code> +<code freebasic
-dim name as string +dim as string personName 
-dim age as integer +dim as integer personAge 
-dim radius as float+dim as single radius
  
 dim as integer x,y dim as integer x,y
 </code> </code>
- 
-**PITFALL** 
-While it may seem that 
-<code> 
-dim x,y as integer 
-</code> 
-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 single pi = 3.14
 +</code>
  
 === Types === === 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+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 ^ ^ Variable types ^
-| integers | Any whole number that is neither a decimal or a fraction | +| integers | Any whole number that is neither a decimal nor a fraction | 
-float | Any number with a decimal place |+single | Any number with a decimal place (single precision) |
 | string | A string of characters | | string | A string of characters |
  
  
 === 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't click. We'll revisit this when we discuss functions and subroutines.+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't click. We'll revisit this when we discuss functions and subroutines. 
  
 ===== 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
  
-<code> +<code freebasic
-dim as integer +dim as integer a 
-dim as integer +dim as integer b 
-dim as integer+dim as integer c
  
 a = 5 a = 5
Line 66: Line 65:
 </code> </code>
  
-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 lets break down what we did.+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'break down what we did.
  
 First First
-<code> +<code freebasic
-dim as integer +dim as integer a 
-dim as integer +dim as integer b 
-dim as integer+dim as integer c
 </code> </code>
 declares 3 variables of the integer type. declares 3 variables of the integer type.
  
-<code>+<code freebasic>
 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
  
-<code>+<code freebasic>
 print "The number is " & c print "The number is " & c
 </code> </code>
fb_variables.1670045231.txt.gz · Last modified: 2022/12/03 05:27 by praetor