Variable in Programming

In programming, we often need a named storage location to store the data or values. Using variables, we can store the data in our program and access it afterward. In this article, we will learn about variables in programming, their types, declarations, initialization, naming conventions, etc.

Variables-in-Programming

Variables in Programming

Table of Content

What is a Variable in Programming?

Variable in Programming is a named storage location t hat holds a value or data. These values can change during the execution of a program, hence the term “variable.” Variables are essential for storing and manipulating data in computer programs. A variable is the basic building block of a program that can be used in expressions as a substitute in place of the value it stores.

Declaration of Variable in Programming:

In programming, the declaration of variables involves specifying the type and name of a variable before it is used in the program. The syntax can vary slightly between programming languages, but the fundamental concept remains consistent.

             Java
         Python
                JavaScript

Output

Initialization of Variable in Programming:

Initialization of variables In Programming involves assigning an initial value to a declared variable. The syntax for variable initialization varies across programming languages.

                   Java
               Python
                     " " "  JavaScript

Output

Types of Variable In Programming:

1. Global Variables:

Global variables in programming are declared outside any function or block in a program and accessible throughout the entire codebase . In simpler words, Global variables can be accessed in any part of the program, including functions, blocks, or modules.

                       Java
  Python
                      global variable in the Main method    JavaScript

Output

2. Local Variables:

Local variables in programming are declared within a specific function, block, or scope and are only accessible within that limited context . In simpler words, Local variables are confined to the block or function where they are declared and cannot be directly accessed outside that scope.

                                       Java
  Python
   function named gfgFnc                                        JavaScript

Output
5 10 15

Difference between Variable and Constant :

Characteristic Variable Constant
Definition A variable is a symbol that represents a value that can change during program execution. A constant is a symbol that represents a fixed, unchanging value.
Mutability Can be changed or reassigned during the execution of the program. Cannot be changed once assigned a value.
Declaration and Initialization Must be declared before use, and its value can be initialized at declaration or later in the code. Must be assigned a value at the time of declaration, and its value cannot be changed afterward.
Examples int count = 5; const double PI = 3.14159;
Use Cases Used for storing values that may vary or change during program execution. Used for storing fixed values or parameters that should not be modified.
Memory Allocation Allocates memory space to store the value. Allocates memory space to store the value, similar to variables.
Syntax dataType variableName = value; const dataType constantName = value;

Difference between Local variables and Global Variables:

Characteristic Global Variables Local Variables
Scope 1 Accessible throughout the entire codebase Confined to the block, function, or scope of declaration.
Visibility Accessible by any part of the program, including functions, blocks, or modules Accessible only within the limited context of declaration.
Lifetime Exist for the entire duration of the program It exists only during the execution of the block or function.
Initialization May have a default value, can be initialized outside functions or blocks May not have a default value, and must be explicitly initialized within the scope.
Access from Functions Accessible directly from any function or block Directly accessible only within the declaring function

Naming Conventions:

Naming conventions for variables In Programming help maintain code readability and consistency across programming projects. While specific conventions can vary between programming languages, some common practices are widely followed. Here are general guidelines for naming variables:

Scope of a variable:

The scope of a variable in programming refers to the region of the program where the variable can be accessed or modified. It defines the visibility and lifetime of a variable within a program. There are typically two types of variable scope:

Local Scope:

                       Java
                   Python