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

Java system properties and environment variables

What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM?
Praveen Sripati
  • 32,799
  • 16
  • 80
  • 117
345
votes
26 answers

Difference between declaring variables before or in loop?

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (quite pointless) example in Java: a) declaration before loop: double…
Rabarberski
  • 23,854
  • 21
  • 74
  • 96
345
votes
4 answers

Set variable in jinja

I would like to know how can I set a variable with another variable in jinja. I will explain, I have got a submenu and I would like show which link is active. I tried this: {% set active_link = {{recordtype}} -%} where recordtype is a variable…
MyTux
  • 3,459
  • 2
  • 15
  • 4
336
votes
9 answers

What's the use/meaning of the @ character in variable names in C#?

I discovered that you can start your variable name with a '@' character in C#. In my C# project I was using a web service (I added a web reference to my project) that was written in Java. One of the interface objects defined in the WSDL had a…
Remko Jansen
  • 4,649
  • 4
  • 30
  • 39
329
votes
6 answers

How to define a variable in a Dockerfile?

In my Dockerfile, I would like to define variables that I can use later in the Dockerfile. I am aware of the ENV instruction, but I do no want these variables to be environment variables. Is there a way to declare variables at Dockerfile scope?
Maxime
  • 9,638
  • 4
  • 17
  • 17
324
votes
12 answers

Convert string to variable name in JavaScript

I’ve looked for solutions, but couldn’t find any that work. I have a variable called onlyVideo. "onlyVideo" the string gets passed into a function. I want to set the variable onlyVideo inside the function as something. How can I do that? (There are…
switz
  • 24,384
  • 25
  • 76
  • 101
309
votes
16 answers

Are PHP Variables passed by value or by reference?

Are PHP variables passed by value or by reference?
cmcculloh
  • 47,596
  • 40
  • 105
  • 130
303
votes
17 answers

Test if number is odd or even

What is the simplest most basic way to find out if a number/variable is odd or even in PHP? Is it something to do with mod? I've tried a few scripts but.. google isn't delivering at the moment.
user1022585
  • 13,061
  • 21
  • 55
  • 75
299
votes
9 answers

Javascript Regex: How to put a variable inside a regular expression?

So for example: function(input){ var testVar = input; string = ... string.replace(/ReGeX + testVar + ReGeX/, "replacement") } But this is of course not working :) Is there any way to do this?
Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
296
votes
16 answers

PHP - concatenate or directly insert variables in string

I am wondering, What is the proper way for inserting PHP variables into a string? This way: echo "Welcome ".$name."!" Or this way: echo "Welcome $name!" Both of these methods work in my PHP v5.3.5. The latter is shorter and simpler but I'm not…
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
290
votes
10 answers

Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects

I'm currently using the iOS 5 SDK trying to develop my app. I'm trying to make an NSString a property, and then to synthesize it in the .m file (I have done this before with no issues). Now, I came across this: "Semantic Issue: Property's…
Noam
  • 3,100
  • 3
  • 16
  • 19
290
votes
20 answers

Dynamic variable names in Bash

I am confused about a bash script. I have the following code: function grep_search() { magic_way_to_define_magic_variable_$1=`ls | tail -1` echo $magic_variable_$1 } I want to be able to create a variable name containing the first argument…
Konstantinos
  • 4,096
  • 3
  • 19
  • 28
289
votes
5 answers

How to create module-wide variables in Python?

Is there a way to set up a global variable inside of a module? When I tried to do it the most obvious way as appears below, the Python interpreter said the variable __DBNAME__ did not exist. ... __DBNAME__ = None def initDB(name): if not…
daveslab
  • 10,000
  • 21
  • 60
  • 86
286
votes
11 answers

How can I pass variable to ansible playbook in the command line?

How can one pass variable to ansible playbook in the command line? The following command didn't work: $ ansible-playbook -i '10.0.0.1,' yada-yada.yml --tags 'loaddata' django_fixtures="tile_colors" Where django_fixtures is my variable.
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
280
votes
9 answers

Formatting a float to 2 decimal places

I am currently building a sales module for a clients website. So far I have got the sale price to calculate perfectly but where I have come stuck is formatting the output to 2 decimal places. I am currently calling this in a variable so that I can…
Callum
  • 3,221
  • 3
  • 19
  • 18