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
7 answers

What can I do with an enum variable?

When I declare a enum variable like this: enum paint_colors { RED, GREEN, BLUE, ...} colors; is the colors variable useful? If so, what can I do with it? Thanks in advance.
drigoSkalWalker
  • 2,742
  • 9
  • 32
  • 45
11
votes
3 answers

Environment variables in eclipse.ini file

Does eclipse.ini or eclipse command line support variable parsing such as $PATH or ${java.home}?
Andrei Pozolotin
  • 897
  • 3
  • 14
  • 21
11
votes
2 answers

Replacing variables in i18next

I have implemented i18next, and after getting help on a couple of issues all works fine. I still have issues understanding parts of the documentation on the support site. I'm trying to replicate this part (found here): // given resources { …
user3727189
11
votes
5 answers

Using a variable in pattern matching in Ocaml or F#

I have a function of the form 'a -> ('a * int) list -> int let rec getValue identifier bindings = match bindings with | (identifier, value)::tail -> value | (_, _)::tail -> getValue identifier tail | [] -> -1 I can tell that identifier is…
Sandy Vanderbleek
  • 1,082
  • 1
  • 12
  • 13
11
votes
4 answers

Creating a javascript alert with php that has a php variable inside?

I'm making a form that is supposed to create a javascript alert when some fields aren't filled out or filled out properly. I want to be able to take the error messages I've put in a php variable and display them in the javascript alert window. The…
MarvinLazer
  • 198
  • 2
  • 5
  • 16
11
votes
5 answers

python: inserting a variable value into a variable name

I would like to insert the value of a variable into the name of another variable in python. In a shell script this would be something like: for n in `more list` do var_$n = some_calculation done but I can't see how to do a similar thing in…
AndyC
  • 111
  • 1
  • 1
  • 3
11
votes
1 answer

How to declare local "variables" as final in Objective-C?

In Java, I can declare local "variables" as final, e.g. void myMethod() { final String foo = "Hello World!"; foo = "Bye-bye..."; // compile error!! return; } When I try to change its value, a get an error from the compiler. I want to…
Yang Meyer
  • 5,409
  • 5
  • 39
  • 51
11
votes
5 answers

Placement of instance variable declarations

I've seen some developers put instance variable declarations at the end of classes though I mostly see them placed at the top. The only reasons I can think of for doing this are stylistic preference or maybe it somehow makes them easier to work…
hydeph
  • 857
  • 1
  • 12
  • 14
11
votes
2 answers

Difference between javascript variable with var and without var?

What is the difference between given below two statements? var temp = 10; temp = 10;
Dixit Singla
  • 2,540
  • 3
  • 24
  • 39
11
votes
3 answers

Microsoft C Compiler: Inline variable declaration?

I'm writing C in Visual Studio 2010. The compiler doesn't seem to want to let me use inline variable declarations. The following code produces an error: unsigned int fibonacci_iterative(unsigned int n) { if (n == 0) { return 0; } if (n == 1) { …
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
11
votes
5 answers

When inside a class, is it better to call its private members or its public properties?

This is something that I've always wrestled with in my code. Suppose we have the following code: public class MyClass { private string _myVariable; public string MyVariable { get { return _myVariable; } set { _myVariable =…
Amberite
  • 1,379
  • 3
  • 14
  • 27
11
votes
6 answers

What is the difference between a local variable, an instance field, an input parameter, and a class field?

What is the difference between a local variable, an instance field, an input parameter, and a class field with respect to a simple Java program?
Jordan McGowan
  • 197
  • 1
  • 1
  • 9
11
votes
4 answers

Logical NAND of two variables in Python

I'm making a function that makes 50 random trees in a 1000 by 1000 area. I need to make sure that Tree 2's x and y both are not the same as Tree 1's x and y. This takes a NAND Gate. I'm okay with one of them being the same, I'm okay with neither…
Jonathan Spirit
  • 569
  • 3
  • 7
  • 14
11
votes
3 answers

declaring empty class member in python

i am trying to read a tree structure file in python. I created a class to hold the tree objects. One of the members should hold the parent object. Since the parentObject member is of the same type as the class itself, I need to declare this as an…
Draetsch
  • 111
  • 1
  • 1
  • 3
11
votes
4 answers

Execute shell script from python with a variable

I have this code: opts.info("Started domain %s (id=%d)" % (dom, domid)) I want to execute a shell script with the parameter domid from above. Something like this: subprocess.call(['test.sh %d', domid]) How does it work? I've tried it…
Vince
  • 1,133
  • 3
  • 17
  • 34