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
194
votes
22 answers

Remove an element from a Bash array

I need to remove an element from an array in bash shell. Generally I'd simply do: array=("${(@)array:#}") Unfortunately the element I want to remove is a variable so I can't use the previous command. Down here an…
Alex
  • 1,967
  • 2
  • 12
  • 6
193
votes
12 answers

How can I store a command in a variable in a shell script?

I would like to store a command to use at a later time in a variable (not the output of the command, but the command itself). I have a simple script as follows: command="ls"; echo "Command: $command"; #Output is: Command: ls b=`$command`; echo $b;…
Benjamin
  • 2,718
  • 5
  • 21
  • 20
188
votes
8 answers

Error: "Cannot modify the return value" c#

I'm using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error Message: Cannot modify the return value of…
P a u l
  • 7,805
  • 15
  • 59
  • 92
187
votes
20 answers

What is the use of a private static variable in Java?

If a variable is declared as public static varName;, then I can access it from anywhere as ClassName.varName. I am also aware that static members are shared by all instances of a class and are not reallocated in each instance. Is declaring a…
Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
187
votes
2 answers

Modify alpha opacity of LESS variable

Using LESS, I know that I can change the saturation or tint of a color variable. That looks like this: background: lighten(@blue, 20%); I want to change the alpha opacity of my color, though. Preferably like this: background: alpha(@blue, 20%); Is…
ben
  • 2,037
  • 2
  • 15
  • 16
186
votes
9 answers

Python nested functions variable scoping

I've read almost all the other questions about the topic, but my code still doesn't work. I think I'm missing something about python variable scope. Here is my code: PRICE_RANGES = { 64:(25, 0.35), 32:(13, 0.40), …
Stefan Manastirliu
  • 3,704
  • 3
  • 24
  • 18
186
votes
13 answers

How do you use script variables in psql?

In MS SQL Server, I create my scripts to use customizable variables: DECLARE @somevariable int SELECT @somevariable = -1 INSERT INTO foo VALUES ( @somevariable ) I'll then change the value of @somevariable at runtime, depending on the value that…
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
182
votes
9 answers

How can I reference a file for variables using Bash?

I want to call a settings file for a variable. How can I do this in Bash? The settings file will define the variables (for example, CONFIG.FILE): production="liveschool_joe" playschool="playschool_joe" And the script will use these variables in…
edumike
  • 3,149
  • 7
  • 27
  • 33
181
votes
4 answers

Calling a JavaScript function named in a variable

I have a JavaScript variable which contains the name of a JavaScript function. This function exists on the page by having been loaded in and placed using $.ajax, etc. Can anyone tell me how I would call the javascript function named in the variable,…
Matt W
  • 11,753
  • 25
  • 118
  • 215
179
votes
23 answers

Why are variables "i" and "j" used for counters?

I know this might seem like an absolutely silly question to ask, yet I am too curious not to ask... Why did "i" and "j" become THE variables to use as counters in most control structures? Although common sense tells me they are just like X, which is…
Carlos
  • 5,405
  • 21
  • 68
  • 114
178
votes
6 answers

How to including variables within strings?

So, we all should know that you can include variables into strings by doing: String string = "A string " + aVariable; Is there a way to do it like: String string = "A string {aVariable}"; In other words: Without having to close the quotation marks…
Gray Adams
  • 3,927
  • 8
  • 32
  • 39
176
votes
10 answers

Save current directory in variable using Bash?

What I'm trying to do is find the current working directory and save it into a variable, so that I can run export PATH=$PATH:currentdir+somethingelse. I'm not entirely sure if they have a variable that contains cwd by default. How do I save the…
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
175
votes
5 answers

Mixing a PHP variable with a string literal

Say I have a variable $test and it's defined as: $test = 'cheese' I want to output cheesey, which I can do like this: echo $test . 'y' But I would prefer to simplify the code to something more like this (which wouldn't work): echo "$testy" Is…
Matt McDonald
  • 4,791
  • 2
  • 34
  • 55
175
votes
6 answers

How can I do division with variables in a Linux shell?

When I run commands in my shell as below, it returns an expr: non-integer argument error. Can someone please explain this to me? $ x=20 $ y=5 $ expr x / y expr: non-integer argument
Judking
  • 6,111
  • 11
  • 55
  • 84
173
votes
12 answers

How to remove a newline from a string in Bash

I have the following variable. echo "|$COMMAND|" which returns | REBOOT| How can I remove that first newline?
Matt Leyland
  • 2,149
  • 3
  • 15
  • 16