Questions tagged [variables]

THIS IS AMBIGUOUS; USE SPECIFIC-LANGUAGE TAGS WHENEVER APPLICABLE. A variable is a named data storage location in memory. Using variables, a computer program can store numbers, text, binary data, or a combination of any of these data types. They can be passed around in the program.

A variable is a named data storage location in memory. Using variables a computer program can store numbers, textual data, , or a combination of any of these data types. The data can be passed around in a program by copying it from variable to variable or by referencing variables, i.e. defining from which variable a receiving code part is to take the contained data.

Variables which are only accessible within a certain function are termed "local variables". A "global variable", or one with indefinite scope, may be referred to anywhere in the program.

In some , variables are constrained by a specific data type. Data types may vary across languages, but share many commonalities.

Primitive data types usually include:

  • character, char, string, varchar (text)
  • byte, short, int, tinyint, integer, long (whole numbers)
  • double, decimal, float (real numbers)
  • bit, boolean (true/false)
  • date, datetime (date and time values)
  • object (any value, including composite types)
  • binary, raw, varbinary (that store stream of system data in binary form)

Composite data types consist of usually more than one of the primitive types and/or even other composite types.

# an example composite type, in pseudo code
Person(
    'First name'  : string,
    'Surname'     : string,
    'Birthday'    : date,
    'CanProgram'  : boolean
)

Some languages contain extra primitives: Tuples (Python), Linked Lists (Lisp), Hash Tables (Lisp, Perl, Python, Lua, D).

Some programming languages allow variables that store functions, that can be stored in data structures, passed as parameters to other functions, or returned as a result from other functions.

Memory allocation

The specifics of variable allocation and the representation of their values vary widely, both among programming languages and among implementations of a given language. Many language implementations allocate space for local variables (whose extent lasts for a single function call) on the stack, and their memory is automatically reclaimed when the function returns. More generally, in name binding, the name of a variable is bound to the address of some particular block (contiguous sequence) of bytes in memory, and operations on the variable manipulate that block. Referencing is more common for variables whose values have large or unknown sizes when the code is compiled. Such variables reference the location of the value instead of storing the value itself, which is allocated from a pool of memory called the heap.

More information and reference material on Wikipedia.

53839 questions
458
votes
6 answers

const vs constexpr on variables

Is there a difference between the following definitions? const double PI = 3.141592653589793; constexpr double PI = 3.141592653589793; If not, which style is preferred in C++11?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
423
votes
3 answers

Printing all global variables/local variables?

How can I print all global variables/local variables? Is that possible in gdb?
cpuer
  • 7,413
  • 14
  • 35
  • 39
418
votes
35 answers

Getting the name of a variable as a string

I already read How to get a function name as a string?. How can I do the same for a variable? As opposed to functions, Python variables do not have the __name__ attribute. In other words, if I have a variable such as: foo = dict() foo['bar'] = 2 I…
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
408
votes
21 answers

Declaring multiple variables in JavaScript

In JavaScript, it is possible to declare multiple variables like this: var variable1 = "Hello, World!"; var variable2 = "Testing..."; var variable3 = 42; ...or like this: var variable1 = "Hello, World!", variable2 = "Testing...", variable3…
Steve Harrison
  • 121,227
  • 16
  • 87
  • 72
408
votes
9 answers

How do I put a variable’s value inside a string (interpolate it into the string)?

I would like to put an int into a string. This is what I am doing at the moment: num = 40 plot.savefig('hanning40.pdf') #problem line I have to run the program for several different numbers, so I'd like to do a loop. But inserting the variable like…
Gish
  • 4,159
  • 4
  • 17
  • 6
407
votes
12 answers

JavaScript OR (||) variable assignment explanation

Given this snippet of JavaScript... var a; var b = null; var c = undefined; var d = 4; var e = 'five'; var f = a || b || c || d || e; alert(f); // 4 Can someone please explain to me what this technique is called (my best guess is in the title of…
chattsm
  • 4,651
  • 5
  • 19
  • 20
400
votes
15 answers

python: how to identify if a variable is an array or a scalar

I have a function that takes the argument NBins. I want to make a call to this function with a scalar 50 or an array [0, 10, 20, 30]. How can I identify within the function, what the length of NBins is? or said differently, if it is a scalar or a…
otmezger
  • 10,410
  • 21
  • 64
  • 90
397
votes
10 answers

Viewing all defined variables

I'm currently working on a computation in python shell. What I want to have is Matlab style listout where you can see all the variables that have been defined up to a point (so I know which names I've used, their values and such). Is there a way,…
Rook
  • 60,248
  • 49
  • 165
  • 242
397
votes
7 answers

What's the scope of a variable initialized in an if statement?

This could be a simple scoping question. The following code in a Python file (module) is confusing me slightly: if __name__ == '__main__': x = 1 print x In other languages I've worked in, this code would throw an exception, as the x…
froadie
  • 79,995
  • 75
  • 166
  • 235
386
votes
16 answers

Pass variables by reference in JavaScript

How do I pass variables by reference in JavaScript? I have three variables that I want to perform several operations to, so I want to put them in a for loop and perform the operations to each one. Pseudocode: myArray = new Array(var1, var2,…
BFTrick
  • 5,211
  • 6
  • 24
  • 28
386
votes
32 answers

What is the difference between var and val in Kotlin?

What is the difference between var and val in Kotlin? I have gone through this link: KotlinLang: Properties and Fields As stated on this link: The full syntax of a read-only property declaration differs from a mutable one in two ways: it starts…
Akshar Patel
  • 8,998
  • 6
  • 35
  • 50
365
votes
7 answers

@ variables in Ruby on Rails

What's the difference between @title and title? Since both of them can be variable names. Also, how do I decide which kind of variable I should use? With @ or not?
OneZero
  • 11,556
  • 15
  • 55
  • 92
362
votes
11 answers

How to use a variable inside a regular expression

I'd like to use a variable inside a regex, how can I do this in Python? TEXTO = sys.argv[1] if re.search(r"\b(?=\w)TEXTO\b(?!\w)", subject, re.IGNORECASE): # Successful match else: # Match attempt failed
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
361
votes
18 answers

Check if object exists in JavaScript

How do I verify the existence of an object in JavaScript? The following works: if (!null) alert("GOT HERE"); But this throws an Error: if (!maybeObject) alert("GOT HERE"); The Error: maybeObject is not defined.
Yarin
  • 173,523
  • 149
  • 402
  • 512
361
votes
15 answers

Non-static variable cannot be referenced from a static context

I've written this test code: class MyProgram { int count = 0; public static void main(String[] args) { System.out.println(count); } } But it gives the following error: Main.java:6: error: non-static variable count cannot be…
Greg
  • 3,643
  • 3
  • 16
  • 3