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
11
votes
2 answers

How to get a JSON value from a variable

suppose I have {"data": {"243232": {"id": "testid","name": "test" } }} so when I do var a = data.243232.id; alert(a); // it gives me testid. but when I do like var c = 243232; var d = data.c.id; alert(d) //it gives as undefined. so…
druveen
  • 1,611
  • 3
  • 15
  • 32
11
votes
5 answers

Are there any declaration keywords in Python?

Are there any declaration keywords in Python, like local, global, private, public etc.? I know that variable types are not specified in Python; but how do you know if the code x = 5 creates a new variable, or sets an existing one?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
11
votes
5 answers

abstract class declarations in c++

Suppose foo is an abstract class in a C++ program, why is it acceptable to declare variables of type foo*, but not of type foo?
Glove
  • 960
  • 6
  • 17
  • 30
11
votes
5 answers

What is the importance of making a variable a constant?

In what situation would you need to make a variable constant? If you wanted a variable to always keep the same value, couldn't you just not change it?
11
votes
2 answers

SASS: Set variable at compile time

Is it possible to set a sass variable at compile time? I basically want to do this: $color: red !default; div#head { background-color: $color; } When I compile to css I want to set $color to "blue" (preferably from the command line). Has anyone…
Kenzic
  • 525
  • 5
  • 15
11
votes
3 answers

reevaluate makefile variables

Is there a way to reevaluate a variable's definition upon each use? For example: MAP_FILES = $(shell find $(TMP) -name "*.map") all: generate_map_files work_with_map_files generate_map_files: ./map-builder work\_with\_map_files: $(MAP_FILES) …
bob
11
votes
2 answers

Is storing any type of function in one variable possible?

I'm trying to make a menu array where each element is a struct that stores variables for text, key that needs to be pressed to select that item and function called on that key press (something like "Quit", 'Q', Quit()). I thought this would make…
Rhyme
  • 121
  • 7
11
votes
1 answer

Getting variable by name in C#

Is there a way to get the value of a variable just by knowing the name of it, like this: double temp = (double)MyClass.GetValue("VariableName"); When I normally would access the variable like this double temp = MyClass.VariableName;
Andreas
  • 6,958
  • 10
  • 38
  • 52
11
votes
10 answers

What's a good way to keep track of class instance variables in Python?

I'm a C++ programmer just starting to learn Python. I'd like to know how you keep track of instance variables in large Python classes. I'm used to having a .h file that gives me a neat list (complete with comments) of all the class' members. But…
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
11
votes
6 answers

Javascript: How do constantly monitor variables value

How do I constantly check a variables value. For example: if(variable == 'value'){ dosomething(); } This would work if I constantly looped it or something, but is there an efficient way of triggering that as soon as the variable is set to that…
Connor
  • 111
  • 1
  • 1
  • 4
11
votes
3 answers

Reading incoming HTTP headers with node.js

Now as example, I'm getting an response which has partially the key/values as an javascript object: status: '200 OK', 'content-encoding': 'gzip' I can easily read out and log the status message by: headers.status but when I try to log the…
M0rph3v5
  • 945
  • 1
  • 11
  • 23
11
votes
1 answer

Global variables in Objective-C - difference in extern and top of .m file declaration

I know you can define a global variable in Objective-C by using "extern", but I just realized that the variables I had declared at the top of my .m file before my first method were also accidentally global (and that was causing some problems). I…
Jackson
  • 3,555
  • 3
  • 34
  • 50
11
votes
3 answers

Why does TypeScript accept value as a data type?

Why does TypeScript accept value as a data type? These scenarios below are accepting and non-acceptable declarations. export class MyComponent{ error: 'test' = 'test'; // accept error: 'test' = 'test1'; // not accept error:…
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
11
votes
7 answers

Retrieving the list of references to an object in Python

All: a = 1 b = a c = b Now I want to get a list of object 1 tagged, which is [a, b, c]. How could I do this? BTW, how to call variable "a" here officially? I know so far it is a "object tag" for the object, but I have no idea what is the term of…
user478514
  • 3,859
  • 10
  • 33
  • 42
11
votes
2 answers

How to skip or execute tasks/steps conditionally using TFS build?

I have a TFS build definition. I wish to run two steps/task that execute two Command Lines, conditionally. Maybe with a variable I could set when I queue the build or something. Mainly I wish to run the build and skip some steps/task if I want to.…
TBogdan
  • 737
  • 7
  • 17
  • 34