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

PHP: Variable empty or not set or what?

What is the difference between these four PHP statements? if (isset($data)) { if (!empty($data)) { if ($data != '') { if ($data) { Do they all do the same?
caw
  • 30,999
  • 61
  • 181
  • 291
11
votes
3 answers

Static Variable initialisation for Classes in C++, why include data type?

I've been learning C++, and I've come across static variable (I have prior knowledge from C89), and in the resource i'm using, they've declared a static variable in a class such as: class nameHere { public: static int totalNum; } int…
Cail Demetri
  • 2,138
  • 3
  • 22
  • 24
11
votes
1 answer

R- how to dynamically name data frames?

I have two types of files in my directory. Each type has either the text "Drug_Rep" in it or if its not there it means its a control file. Drug data has replicates which can vary in number and so does the control. I am reading the files in a for…
RnD
  • 1,172
  • 4
  • 15
  • 25
11
votes
3 answers

How to view the type of a variable in PL/SQL?

Is there a function in PL/SQL to show a variable's exact type, like the DUMP function in SQL? I've tried the following DECLARE l_variable INTEGER := 1; BEGIN DBMS_OUTPUT.PUT_LINE (DUMP (l_variable)); END; But it gives the following…
csadam
  • 401
  • 1
  • 5
  • 15
11
votes
3 answers

Why is declaration of a string variable in Java capitalized?

In Java, when one declares a string variable the word "String" is capitalized, yet it isn't in any of the other types I've run across (e.g. "int" or "double"). Why is this? Was it just some weird arbitrary decision by the designers?
Raiden Worley
  • 394
  • 1
  • 4
  • 14
11
votes
2 answers

Changing instance variables

I have this code: class Yes: def __init__(self): self.a=1 def yes(self): if self.a==1: print "Yes" else: print "No, but yes" class No(Yes): def no(self): if self.a==1: …
user2154113
  • 401
  • 1
  • 6
  • 12
11
votes
3 answers

how can I echo/print specific lines from a bash variable

I am trying to print specific lines from a multi-line bash variable. I've found the following: while read line; do echo LINE: "$line"; done <<< "$x" where x would be the variable, but that simply prints out all lines instead of just a single one…
lacrosse1991
  • 2,972
  • 7
  • 38
  • 47
11
votes
6 answers

Is it possible to return more than one value from a method in Java?

I am using a simulator to play craps and I am trying to return two values from the same method (or rather I would like to). When I wrote my return statement I simply tried putting "&" which compiled and runs properly; but I have no way of accessing…
Michael MacDonald
  • 413
  • 2
  • 6
  • 17
11
votes
5 answers

How to store local variables in jQuery click functions?

I'm trying to figure out how to store external variable values in the functions created during jQuery's click() event. Here's a sample of the code I'm working with now. for(var i=0; i<3; i++){ $('#tmpid'+i).click(function(){ var gid =…
Geuis
  • 41,122
  • 56
  • 157
  • 219
11
votes
7 answers

Actionscript3: Does variable exist?

I'm a little bit new to Actionscript, but I can't figure this one out. I've done a lot of searching on this topic and haven't found a clear answer. I've tried the following solutions that people posted online but none of them work. All the following…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
11
votes
3 answers

PHP Using $this->variable as Class Method Parameter Default Value

Ok so this seems like a pretty dumb question but PHP Is telling me I can't do this, or rather my IDE... In the below example its telling me I can't use $this->somevar as the default value for the method. ie... class something { public somevar =…
Brian Patterson
  • 1,615
  • 2
  • 15
  • 31
11
votes
3 answers

MYSQL variables in php query

The solution to this problem might be a simple over sight of mine. I am trying to run a MYSQL query stored as a string in PHP. The query runs fine using DBM tool like Navicat but returns false in my PHP development enviorment. Is there something…
David.LPower
  • 1,083
  • 2
  • 10
  • 14
11
votes
8 answers

Why are symbols in Ruby not thought of as a type of variable?

New to programming and to Ruby, and I hope this question about symbols is in line. I understand that symbols in Ruby (e.g., :book, :price) are useful particularly as hash keys, and for all-around doing a lightweight, specific subset of the things…
allenemilyh
  • 113
  • 6
11
votes
2 answers

Assign query results to MySQL variable

I'm querying a big mysql database with only read privileges, and I'd like to set some slow query results to a variable, 'foo', so I can use them again in other queries. Basically, I want to have a variable for a cumbersome subquery, so I can reuse…
5un5
  • 211
  • 1
  • 2
  • 5
11
votes
3 answers

How can I make button label dynamic using javascript?

What I want to do is be able to change the text inside of a button by making the text inside an actual variable. Something like this: but instead of a string, it's a variable: var status = 'on';
Casa_De_Agua
  • 149
  • 1
  • 1
  • 5
1 2 3
99
100