Declaring Variables and Constants

Master the art of reserving memory and naming your data containers — essential skills for programming success!

1

The Act of Declaration

What is Declaration? Before a program can process data, it must first "reserve" space in the computer's memory — a process known as declaration. Think of it like reserving a locker at school: you're telling the computer to set aside a specific amount of memory and give it a name (called an identifier) so you can find it later.

The Three Requirements

To declare something in programming, you typically need:

  • A Name (Identifier): How you'll refer to the data later (e.g., studentName)
  • A Data Type: What kind of data will be stored (Integer, String, Real, Boolean, etc.)
  • An Initial Value: Optional for variables, but mandatory for constants
The Memory Reservation

Enter a name, select a type, and watch the computer reserve memory for you!

Click "Reserve Memory" to allocate a block!
2

Naming Your Identifiers (The Rules)

Case Sensitivity

Whether identifiers are case-sensitive depends on the programming language. In C, C++, and Java, Total and total would be seen as two different variables. However, in Pascal and Visual Basic, they would be considered the same. Always check your language's rules!

Reserved Words

You cannot use words that have special meaning in the language as your variable names. Common reserved words include: if, while, for, print, var, const, begin, end, and many others. The computer needs these words for its own purposes!

Standard Conventions

Programmers follow naming conventions to make code readable:

  • CamelCase: studentAge, totalScore, isMember
  • Snake_Case: student_age, total_score, is_member
Valid or Invalid?

Drag each identifier to the correct category!

studentAge
2nd_Place
is_Member
while
totalScore
my variable
student_age
class

Valid Identifiers

Invalid Identifiers

3

Declaring Constants

What is a Constant? A constant is like a variable, but once you set its value, it can NEVER be changed during the program. Think of it as writing something in permanent marker — it's locked in place forever!

The CONST Keyword

In Pascal and many other languages, you use the CONST keyword to declare constants. You must provide an initial value immediately because you can never change it later.

{ Pascal Constant Declarations } CONST PI = 3.14159; SCHOOL_NAME = 'Greenwood High'; MAX_STUDENTS = 50; TAX_RATE = 0.15;

The Capitalization Habit

Most programmers use ALL CAPS for constants to make them stand out from variables. This is a universal convention that helps you quickly identify which values are locked and which can be changed.

The Permanent Marker

Try to change a constant's value and see what happens!

CONST PI = 3.14159;
PI := 3.14; // Try to change it!
⚠️ COMPILER ERROR: Cannot modify a constant!
4

Declaring Variables

What is a Variable? A variable is a named storage location in memory whose value can change during program execution. Unlike constants, variables are flexible — their values can be modified, updated, and reassigned throughout your program.

The VAR or DECLARE Keyword

In Pascal, you use VAR to declare variables. In some other languages like pseudocode, you might use DECLARE. You must specify the data type, but you don't necessarily have to provide an initial value immediately.

{ Pascal Variable Declarations } VAR count : Integer; price : Real; studentName : String; isEnrolled : Boolean;

Initialization

Giving a variable a starting value is called initialization. It's good practice to initialize variables immediately because uninitialized variables may contain "garbage data" — random values left over from previous memory use.

{ Good Practice: Initialize variables } count := 0; // Start at zero price := 0.00; // Start at zero studentName := ''; // Empty string

Multiple Declarations

You can group variables of the same type together in a single declaration statement:

VAR length, width, height : Real; x, y, z : Integer; firstName, lastName : String;
The Variable Blueprint

Build a declaration line by selecting each component!

Keyword
Name
Type
5

Scope: Global vs. Local

What is Scope? Scope determines where in your program a variable can be "seen" and used. Think of it like visibility — some variables are visible everywhere, while others are only visible in specific sections.

Global Variables

Global variables are declared at the very top of your program, before any procedures or functions. They can be accessed and modified by ANY part of your program. Use them sparingly — too many global variables can make debugging difficult!

Local Variables

Local variables are declared inside a specific module, procedure, or function. They only exist while that section of code is running and "die" (are destroyed) once the procedure finishes. This is called automatic storage duration.

The "Shadowing" Problem

What happens when a local variable has the same name as a global variable? The local variable "shadows" the global one — inside that procedure, the local version takes precedence, and the global becomes temporarily invisible.

The Flashlight Metaphor

Move your mouse over the code to see which variables are "in the beam" (accessible)!

GLOBAL count = 10
PROCEDURE Test()
LOCAL count = 25
OUTPUT count
END PROCEDURE
Global Variable
Local Variable
6

CSEC Exam Tip: Declaration in the SBA

The Variable Listing In your School Based Assessment (SBA), you MUST include a "Variable Listing" or "Data Dictionary" in your documentation. This table explains the purpose of every variable you declared in your program.

Example Variable Listing

Variable Name Data Type Purpose/Description
studentName String Stores the student's full name
testScore Integer Stores the test score (0-100)
TAX_RATE Real (Constant) Fixed tax rate of 15%

Consistency Check

CRITICAL: Ensure the names in your code match the names in your IPO chart exactly. If your IPO chart shows testScore, your code must use testScore — not TestScore, score, or Test_Score. Inconsistent naming will cost you marks!

Find the Mismatch

Compare the IPO Chart and Code — find the inconsistency!

IPO Chart

studentAge (Input)
discountRate (Process)
finalPrice (Output)

Code

studentAge ← Input
discount_Rate ← Calculate
finalPrice ← Output

7

Knowledge Check: The Declaration Auditor

Syntax Fixer

Question: Correct the error in this Pascal declaration:

VAR 5Total : Integer;

Answer: Variable names cannot start with a number! The correct declaration would be:

VAR total5 : Integer; { OR } VAR total : Integer;

Logic Quiz

Click "Start Quiz" to begin!
Score: 0/5

Quiz Complete!

Short Answer Questions

Question 1: Why is it better to declare the Tax Rate as a constant rather than a variable?

Answer: Tax rates don't change during program execution. Using a constant prevents accidental modification and signals to other programmers that this value is fixed. It also makes the code more maintainable — if the rate changes, you only update it in one place.

Question 2: Explain the difference between declaring a variable and initializing a variable.

Answer: Declaration tells the computer to reserve memory and assign a name and type. Initialization gives the variable its first value. A variable can be declared without being initialized, but it's best practice to initialize to avoid garbage data.

Declaration

Reserving memory and assigning a name and data type

Initialization

Giving a variable its first (initial) value

Scope

Where in the program a variable can be accessed

Constant

A value that cannot be changed after declaration

Summary: Key Takeaways

  • Declaration reserves memory for your data before you can use it
  • Identifiers must follow rules: no numbers first, no spaces, no reserved words
  • Constants are permanent — their values cannot change once set
  • Variables can change — they hold data that may be modified
  • Scope determines visibility — global vs. local access
  • SBA documentation matters — include a complete variable listing
Ready for More?

Would you like to learn about Data Types (the next logical topic), or practice with a Declaration Coding Challenge?

Scroll to Top