What is a Programming Variable?

Variables have a defined role in programming. The use of variables is independent of the algorithm. To know more about this, read this article till the end.

Variables in programs are used as and when required. Variables in the program are used randomly. The most common use of variables is their use as counter variables and temporary variables. The value decides the behaviour of variables and events that influence variables.

The existing variables whose value does not change, these types of variables are called fixed variables. The variables also maintain logical relations between programming constructs. The values of the variable are updated as the execution of the program continues.

In Object-Oriented Programming, objects can also behave like a variable. Thus an object can be of type String or Integer. Thus the object can attain a single value and at like a variable. When the object behaves like a variable, it is said to have its attribute value only.

In functional programming, there is no concept of variables. Instead, the variables in functional programming are used as parameters, and the return values have erratic behaviour. 

The variables are used in procedural programming, parameters and return values are used in functional programming and Object-Oriented programing use variables, attributes and objects.

The use of variables is different in different programming paradigm.

Variables in Computer Programming

In computer programming, a variable is a memory location that is identified by a symbolic name. This memory location contains a value that can be useful in programming or a garbage value. Thus a variable can contain a set of bits of a specific type such as integer, float or string and have a specific address. The name of the variable is used to refer to it. The name of the variable and its content are independent, and both can have independent values.

The value of the variable is may or may not change during the execution of the program. The value of the variable during the program execution depends on the logic of the program.

The variables in computer programming are given a name that should specify their use in the programming. The programming language compiler replaces the name of the variable with its memory address. The variable’s name, data type, and memory location remain fixed throughout the program, but its value changes as the program execution proceeds. The memory allocated to the variable is deallocated as soon as the program terminates.

Effects on Variables

In programming, the state of the variable is not fixed. Instead, it changes as the program execution proceeds. Therefore, the variable value can be accessed or changed as required. This is the case with imperative programming.

In the case of functional languages, the value of the variable remains fixed throughout the program. Thus, the variable’s value is bound to a particular expression, which does not change.  The same functionality is achieved using the constant variable in Imperative Programming language.

A variable can store a value of a particular data type, which depends on the programming language’s specified grammar. Therefore, variables are considered as placeholders to store the specified values of a particular data type.

The lifetime of the variable depends on the type of the variable and programming language grammar. There are two types of variables – first, Automatic Variable and Second, External Variable.

Automatic variables are local variables. The lifetime of these variables is within the scope of the function. The variable is created when the function is called and get extinguished when the function gets terminated. 

The External variables are those variables that can be accessed by all the defined functions in the program. The lifetime of the variable is throughout the program and gets extinguished as soon as the program terminates. However, the values assigned to the external variable is retained till the program is under the execution phase.

The variable can also be referenced using an identifier. An identifier is used to access the value of the variable, modify the value of the variable, or make changes in the access permission of the variables.

If a variable is named “to_use” and has the value 67, the same variable is referred to by the identifier “use”. If using the identifier “use”, the value of the variable “to_use” is changed to 88, then the variable “to_use” will yield the value 88, not 67.

If the variable is accessed using the identifier, then the identifier is also called the variable’s name. Thus, it can be said that “to_use” and “use” is the name of the same variable.

Lifetime of Variable

The lifetime of a variable is the extent to which the value of the variable can be accessed. A variable can be accessed in different sections of the program. The variable is accessible only if the variable is alive.

The lifetime of the variable is also called the scope of the variable. The extent of the variable is the value the variable attains during the execution of the program. Thus, the scope of the variable has its influence on the extent of the variable. 

The section of the program in which the variable is accessible is called the variable’s scope. Thus, till the variable is accessible, the variable is visible. When the program control enters the program section in which the variable is accessible, the variable’s scope begins. When the program control exits the section, and the variable becomes inaccessible, the variable’s scope terminates.

A variable can have a lexical scope; it can only be accessed within a particular function or subroutine or an expression or a particular set of statements. A variable is said to have a dynamic scope if it can be resolved at run time. If the variable is accessible only within the function, it is said to be a local variable. If the variable is accessible throughout the program, then the variable is said to be a global variable.

The extent of the variable refers to the extent up to which the variable refers the same value or memory location. 

The memory allocated to the variable must be freed if it is not done, leading to a memory leak. However, this can be overcome by using the garbage collection technique. Garbage collection is a technique that is offered by the language. 

Garbage collection is used to gain memory efficiency. To improve memory utilisation, a variable is allocated memory as soon as the variable is declared. And the allocated memory is freed up when the variable goes out of the scope of the memory. To further improve memory utilisation, the compiler of the language issues warns about all the unused declared variables.

If the scope of the declared variables is narrow, it avoids accidental changes that can be made to the declared variable. This can be done using a modular programming approach or declaring a variable as private.

Each variable declared in the program has a type. For example, a variable declared as an integer type cannot store string values. Some languages infer the type of the variable by its value. This indicates that values are also categorised as types. These values are inferred at runtime. 

Variables are also used as parameters in functions. When variables are used in the function calls, they are called formal parameters.

The memory allocation to variables and their implementation depends on the programming language in which they are implemented. Therefore, memory is allocated to variables according to the language’s grammar in which they are declared. The scoop of this variable is within the function in which they are declared. When the use of the function is over, then the allocated memory is deallocated. Memory is allocated to variables from the heap.

In Object-Oriented Programming, each Object is allocated memory. These objects are stored in computer memory. Each object declared in the Object-Oriented Programming language function is allocated memory and deallocated when the Object goes out of the function’s scope.

In Object-Oriented Programming language, each object is allocated memory from the heap, and when the use of the Object is over, the allocated memory is deallocated.

In procedural programming languages such as C, the required memory is allocated and deallocated explicitly at run time. If the allocated memory is not freed-up, it leads to a memory leak due to which depletion in a heap takes place.

Different programming languages use different naming conventions to name variables. For example, in some programming languages, the name of the variable or constant can be of multiple characters. On the other hand, in some programming languages, the auxiliary variables may be single characters such as ‘s’, ’ a’ or ‘j’.  In addition, auxiliary variables are often used as indexes of the variable.

The naming convention grammar is defined as the compiler of the language. Thus, for example, in some programming, it is defined that name of the variable can not begin with the digit lying between the range of 0 to 9. It is also defined in the language’s grammar that the name of the variables cannot contain space characters. Some of the languages permit that name of the variable can have an underscore or special symbol. 

Variable names can also be case sensitive. The case-sensitive implementation depends on the grammar of the language. Most of the high-level language is case sensitive. In some of the high-level languages, the name of the variable is kept reserved and are termed as keywords. The keywords are to be used as it is without making any changes.

The variables are used to refer to the location of the memory. If the variables are not used, the memory address must be accessed using the hexadecimal notation. Since the number of variables to be used in the program depends on the logic of the program, and as the logic becomes complex number of variables in the program increases. Therefore, using variables by referring to the hexadecimal notation becomes difficult. Thus, this hexadecimal notation is denoted by a name and is known as the variable name. Thus variable names are used to make the programming logic easy and user-friendly.

The name of the variable must be such that it should specify the usage of the variable. Using a variable name that cannot specify its usage raises difficulty in the readability of the program. Thus the name of the variables must be descriptive. Thus descriptive names are encouraged. 

To increase the readability and understandability of the program, the programmer must use a well-defined naming scheme. Often programmers use the short deceptive names of the variables. Using too descriptive variable names should also be avoided as it increases the length and raises difficulty in the program’s understandability.

Variable Classification

Depending on the lifetime of the variable, variables are classified. For example, suppose the variable is allocated memory before the program’s execution begins, and the same memory space is utilised till the program terminates. In that case, this type of variable is known as a static variable.

Dynamic variables are allocated memory at run time and are used and visible with the function’s scope in which they are declared. However, the memory allocated to the dynamic variable is deallocated as soon as the control goes out of the function’s scope.

Control Variable

A control variable maintains the flow of the program execution. The control variables are assigned values of the next instruction to be executed. Thus, the value assigned to the control variable is the address of the next instruction to be executed.

Implementation of the control variable depends on the programming language. For example, in some programming languages, control variables are the same as the local variable. 

The value stored in the control variable is not fixed and keeps on changing during the program’s execution. The value of the control variable changes during the execution of the loop.

Variable Declaration

A declaration is a programming construct used to bind the variable its name and if required to its value.

The syntax of declaring a variable is different for different programming languages. Almost all programming languages have type declaration, constant declaration, variable declaration, and procedure declaration. 

The variables can be declared at the package level or the class level in some languages. Further, the declaration may also be a collateral declaration, sequential declaration, and recursive declaration.

The programming languages also support type declarations. Using type declarations, the variable name is bound to the data type according to which it holds the value. Type declaration is also capable of binding a variable name to a user-defined type.

If the type declaration is used for existing types, then it is equivalent to structural types. If the type declaration is of a user-defined type, then it is an equivalent name type.

Constant Declaration

A constant declaration is used to bind the variable with the constant value. The variable declared as constant doesn’t change its value during the execution of the program.

Procedure Definition

The procedure definition is used to bind a particular variable to a procedure.  The example of the procedure definition is as follows:

bool check_even (int n)
{
     return (n%2 == 0)
}

In the above code, check_even is the variable name and is used as a procedure function to check whether the variable is even or not. Now, check_even can be used as a variable and is capable of checking whether the variable is holding the even number or not.

Collateral Declarations

A collateral declaration is composed of sub declarations. Each sub declaration is independent of the other. Using Collateral declaration, sub declarations are avoided. Collateral declarations are used in procedural and Object-Oriented programming languages, but they are commonly used in functional and logical languages.

Sequential Declarations

A sequential declaration consists of sub-declarations and is expressed one after another. The sequential declaration is used to merge the sub-declaration.

The sequential declaration is supported by most of the imperative and Object-Oriented Programming languages. An example of sequential declaration is as follows:

var_count : Integer : = 22;

procedure bumping is
begin
count := count+1;
end;

The sequential declaration of the above declaration is as follows:

{
     count -> a character variable, 
     bump -> a proper procedure 
} 

Recursive Declarations

In recursive declarations, variables bind themselves. This is commonly used in recursive types and procedures. 

Recursion is a technique that is supported by languages.