Variable in Programming

 

What we mean a Variable in Programming :

A designated storage place is frequently required in programming in order to store data or values. We can save the data in our software and retrieve it later by using variables. Programming variables, including their types, declarations, initialization, naming conventions, etc., will be covered in this article.


What Does a Programming Variable Mean?
In programming, a variable is a designated storage place that contains data or a value. Because these values are subject to change while a program is running, they are given the label "variable." For computer programs to store and manipulate data, variables are necessary. The fundamental unit of a program is a variable, which can be utilized in expressions to replace the value it stores.

Declaration of Variable in Programming:

In programming, a variable's type and name must be specified before it may be used in the program. This is known as parameter declaration. Different Programming languages may differ slightly in syntax, but the basic idea is always remains the same.

Let me show an example for a variable declaration in our C++ Programming Languages as: 

#include <iostream>

using namespace std;

int main() {  

    // Syntax if a variable_name;

    int user_id;

    double cost_price;

    char grade;

    return 0;

}

Types of Variable In Programming:

1. Global Variables: 

In programming languages, global variables are those  variables available across the whole codebase and declared outside of any function or block. To put it another way, global variables are accessible from any section of the program, including blocks, functions, and modules.

Let me show an example for a global variable declaration in our C++ Programming Languages as: 

#include <iostream>

using namespace std;

int glvariable = 5;

void globalFnc() { 

    cout << glvariable << endl;

 }

int main() {

   cout << glvariable << endl;

   globalFnc();

    return 0;

}

Output of program :  5

2. Local Variable:

In programming, local variables are declared inside a certain function, block, or scope and are only available within that constrained environment. Put more simply, local variables cannot be immediately accessed outside of the block or function in which they are declared.

Let me show an example for a local variable declaration in our C++ Programming Languages as: 

#include <iostream>

using namespace std;

int main() {

    int localvariable  = 13;

   cout << localvariable<< endl;

    return 0;

}

Output of program:  13

Naming Guidelines: 
Variable naming guidelines Assist in keeping code consistent and readable across programming projects. Although particular norms may differ among programming languages, there are several standard practices that are commonly adhered to. The following general rules apply for naming variables:

Descriptive and Meaningful: 
Pick names that convey the variable's function or substance. Code readability and comprehension are enhanced by descriptive names.
Words in a variable name can be separated using either camel case (myVariableName) or underscores (my_variable_name). Decide on a single look and maintain it constantly.

Steer Clear of Single Letter Names: 
Steer clear of single-letter variable names, with the exception of loop counters and well-known standards (i for an index, for example). Make use of names that express the function of the variable.

Follow Language Conventions: 

Use the naming conventions specified by the programming language you're using. For example, Java commonly employs camel case (myVariableName), whereas Python frequently uses underscores (my_variable_name).

Avoid Reserved terms: Variable names should not contain reserved terms or programming language keywords.

Avoid Abbreviations: 

Use full words instead of abbreviations, unless generally recognized or for standard terminology (e.g., maximum, minimum).

Scope of a Variable

The scope of a variable in programming refers to the area of the program where it can be accessed or changed. It specifies the visibility and lifetime of a variable within a program. There are usually two forms of variable scope:

Local Scope:

  • A variable declared inside a function, block, or loop has a local scope.
  • It can only be accessed within the specific function, block, or loop where it is declared.
  • Its lifetime is limited to the duration of the function, block, or loop.

Global Variable:

Variables declared outside of a function or block have global scope.
It can be accessible from any location in the program, including within functions.
Its lifespan spans throughout the entire program.

Value Assignment for Variables
To give the stated variables values, we utilize the = symbol. The value to be stored will be on the right, while the variable name and type will be on the left.
As an illustration, int score = 55;


Additionally, you can use user input techniques to obtain the value that will be saved in the variable.
For instance, in the C++ language,
 
int main()

{
    int x;

    cout << “Enter  your number :”;

    cin >> a;

    cout << “User entered a number which is ” << a;

}  

The output of above Program is as: 

Enter  your number: 55.

User entered a number which is 55.

Again we see types of variables as :

Variable Types
The programming language has five distinct sorts of variables. They are as follows:

Constant Variables 
Data that doesn't need to be altered during the program is stored in constant variables. In this kind of variable, the data cannot be changed.

Global Variables
Declared outside of a function, global variables are accessible from anywhere in the program until it is executed. While Python uses the "global" keyword to make a variable global, Ruby uses the $ symbol before the variable name.

Class Variables by Class
Only within the class may these variables be accessed. It is necessary to declare class variables at the class level. Because each class variable can only exist in one copy per class, they are also known as static variables.

Instance 
Variables 
Although they are specified outside of a method, instance variables are part of the class.

Local Variables
These variables, which are declared in methods, classes, and instances, are the most often used. You can access these variables from within a block or section of code.


Q01. Regarding variable names, which of the following is accurate?

A Variable's name cannot begin with a number.
B. Any length of variable is possible 
C. Reserved words can be used as variable names 
D. Both special and alphanumeric letters can be present
Respond Choice A.

Q02: What is an external variable?

A. Another name for a global variable
B. All functions can access it.
C. Makes a statement 
D. Does all of these answers.
Respond  Choice D.

Q03. Out of the following, which one is not a legitimate declaration for a variable name?

A. int number;
 
B. float speed; 
C. double rate;
D. int 2_main.
Respond  Choice D.

Q04. Which of the data types has a changeable size?

A. int
B. float
C. double 
D. struct

Respond  Choice D

Last Points: 

You may have come to the conclusion that variables in programming and their importance while developing code or a web application or a Framework for development purpose, now more than just creating code and getting things to work after reading the aforementioned rules and working on certain apps, there are numerous aspects to keep in mind regarding variable definition, declaration etc. Remember all of the aforementioned rules about the variables and adhere to them as you progress.


Comments

Post a Comment

Popular posts from this blog

How to build a Web Application using PHP and other Tools of Web App Development

Bootstrap a CSS framework