Understanding Variables In Programming



Understanding Variables In Programming


"A variable is the corner stone for creating algorithms"(Mark Anthony Graham)

 Hello folks, in today's article I am going to talk about variables in software engineering terms. I simply cannot just imagine a system sourcecode without a variable or how a system can be developed without including variables into the system. Unless of cause it is such a basic utility for doing a minimal dummy task. But any developer who wants to build a professional system would use variables one way or the other. Let learn more...

What is a variable?

In programming, a variable is a container that holds value that can be change, depending on conditions or an information passed to the program. Typically, a program consists of instructions that tell the computer what to do and data that the program uses when it is running. And these data are stored within the variables. A variable is a named storage location used to store data in a computer program. Variables can store different types of data such as numbers, strings, Boolean values, and objects. The value stored in a variable can be changed during the execution of the program.

In a way it means that, variables grants a computer programmer the ability to store data temporarily in the PCs memory. And this set variable can be changed in any given time as the program runs.

Variables helps to build system logics

Making use of variables in programmimg helps a programmer to implement ideas from their minds. This is because variables could be overwritten, used as a condition, used in mathematical operations, infacts variables could even hold information the developer would like to share with his / her end users. It like almost very coding logic is built on variables. 

A simple calculator program would require variables to be used inorder to work correctly. Infact im thinking about creating a calculator program without a variable... How would that be even possible. Because programmatically the data that would be set by the user would have to be stored on memory for further mathematical operation and output.

Sample calculator program in python this program makes use of variables.

def add(x, y): # x and y are all variables return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice(1/2/3/4): ") # choice is a variable num1 = int(input("Enter first number: ")) #num1 is a variable num2 = int(input("Enter second number: ")) #num2 is a variable if choice == '1': print(num1,"+",num2,"=", add(num1,num2)) elif choice == '2': print(num1,"-",num2,"=", subtract(num1,num2)) elif choice == '3': print(num1,"*",num2,"=", multiply(num1,num2)) elif choice == '4': print(num1,"/",num2,"=", divide(num1,num2)) else: print("Invalid input")


Point to note is that variables plays a big role in building the logics for your program.

How to create a variable in ?


Python

Variable syntax in Python is a way of referring to a value stored in a variable. The syntax of a variable in Python uses the name of the variable followed by an = sign to assign a value to thevariable. The value can be a number, string, list, or any other data type. For example, to assign the string "Hello World" to a variable called greeting, the syntax would be:

greeting = "Hello World"

PHP
$variable_name = value; //For example, $name = "John";

JavaScript

Variable syntax in JavaScript is the way in which variables are declared and used in the code. Variables are declared using the var keyword, and they can be given any value, such as a number, string, array, object, and more. The syntax for declaring a variable is: 

var variableName = value;

// or

let variableName = value;

const variableName = value;

For example, if you wanted to create a variable called age, and set its value to 25, you would write the following code:

var age = 25;

C++

Variable syntax in C++ is the way that a variable is declared and used in the program. Variables are declared using a data type, followed by the name of the variable, and then, optionally, an assignment. For example, the following code declares an integer variable x and assigns it the value 10:

int x = 10;

Rust

Variable bindings in Rust are declared using the let keyword, followed by the variable name, the colon (:), the type of the value to be bound, and finally the value itself.

Example:

let x: i32 = 5;

Conclusion

So these are ways that you can initiate a variable. And more importantly it is to be noted that variables helps in OOP (Object Oriented Programming). Since variables becomes the building blocks for classes and methods.

Comments

followers

Popular posts from this blog

The Beauty Of Using Flutter With Dart For Mobile Development

Building a Fortified Database Connection in PHP

Mastering Error Handling in PHP: A Deep Dive into `@` and `try...catch`