Thread: this will sound like a stupid question. But what exactly is a Variable?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    this will sound like a stupid question. But what exactly is a Variable?

    I know that this question sounds like when a person who is unaware of Chess asks what a pawn is, but believe it or not I've been writing code for years. I just don't have a proper background in computer science, and that's probably part of the reason why I never learned the proper textbook definitions of all these concepts. (Either that or I was too lazy to make an effort to understand the inner workings of what I was doing.)

    For example, recently, instead of accepting that a reference type variable is just a representation of a memory address, I've been banging my head against the wall trying to understand why authors and other 'authorities' refer to variables (reference type and otherwise) as something that they aren't and in doing so only confuse people who are trying to understand this concept.

    I know that in a strongly-typed language a variable has a type, a name, and a value or a reference to a value. At least that's what I was taught in the few introductory CS courses I have taken. But notice that I listed what a variable has, not what a variable is.

    In some contexts, the authors/lecturers often refer to a variable as a name used to represent data. In other words, if you type "int x = 1", then "x" is the variable. I think that in most cases, most of us would also think of "x" as the variable.

    But the problem I have with this definition of "variable" is that it's essentially saying that the identifier (ie: the name) used to represent the variable is in fact the variable. Some people won't dispute that definition, but in my case (and keep in mind that obviously I have a very weak computer science background) I've always known (except maybe when I was a little kid) that the name given to the thing is not the same as the thing. In other words, if your name is John, then, properly speaking, you are not John, you are just a human being whose name is John.

    By the same token, if I declare a variable of type 'int', and use the identifier/name 'x' to represent it, I still don't know what the variable is, other than the fact that it is a thing of some sort. But in some contexts, when it comes to variables, the authors/lectures make it sound like the 'thing' is the same as the name given to the 'thing'.

    In other words, such people would refer to 'x' as 'the variable'. But, unless I'm completely lost and hopeless when it comes to understanding computer science concepts, I understand that 'x' is not the variable, but the name of the variable, and the name itself is merely a sequence of characters that represent 'something' that represents a memory address that holds a sequence of zeros and ones that is interpreted as data rather than instructions.

    Assuming that I'm on the right track, am I to understand that, at least in the case of reference type variables, the type of the variable, the name given to the variable, the data stored in the memory address represented by the variable, and the representation of the memory address itself, are collectively known as 'the variable'?

    Some people will probably agree with that definition of the term 'variable' (ie: something that has a type, a name, represents a memory address that holds data or actually holds the data). But again, what a thing has and what a thing does, is not the same as what the thing is. For example, a dog has four legs, a dog has hair, a dog barks; but just because a 'thing' has 4 legs, has hair, and barks, doesn't make it a dog (before anyone says that 'bark', by definition, is a noise that a dog makes, I can tell you that at least according to the dictionaries I have consulted, 'bark' is a noise made by some animals such as foxes, wolves, and dogs).

    For the time being I accept that, at least in the case of reference type variables, a variable is 'something' that has a type, a name, and represents a memory address, irrespective of whether the memory address has data. But I still don't know what a variable is.

    Other definitions of 'variable' I've heard is that a variable is a sequence of unicode characters that the compiler transforms into a number that represents a memory address. In other words, if I type 'int myVar = 2', then the variable is the sequence of characters m-y-V-a-r whereas all those characters, collectively, are the variable's identifier. But this is akin to saying that the guy whose name is John, is J-o-h-n!

    Is a variable an abstraction?

    (The term 'abstraction' has a specific meaning in computer science, and I know that I don't entirely understand it, I only know that according to a popular online encyclopedia 'abstraction' is defined as 'the process by which data and programs are defined with a representation similar to its pictorial meaning...', so I don't feel comfortable concluding that something that I barely understand is another thing that I clearly don't understand.)

    Is a variable merely a representation of a memory address? In other words, is it an abstract concept? Is this the bottom line? If this is the bottom line then why do authors and other 'authorities' keep implying that when it comes to variables, the name of 'the thing' is 'the thing'?
    Last edited by y99q; 11-24-2011 at 11:58 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    In other words, if your name is John, then, properly speaking, you are not John, you are just a human being whose name is John.
    why do authors and other 'authorities' keep implying that when it comes to variables, the name of 'the thing' is 'the thing'?
    Well... that "John" always responds if someone calls him "John"...
    Then why call him "The Human being whose name is John"?
    You could also expand Human Being into another phrase....or more, recursively


    Is a variable an abstraction?
    Yes.
    Logically...everything is.

    P.S: These sorts of posts are a sign of boredom...believe me or not !

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by y99q View Post
    I know that this question sounds like when a person who is unaware of Chess asks what a pawn is,
    Not at all... What I see is a classic example of monumentally overthinking a very simple thing.

    If I create a variable named Fred in my program, I refer to that variable as Fred... for all useful purposes that variable IS Fred.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    hi, i think variable is the a place in the memory that you can put everything in your memory and change it, but memory place have rules,you should put something which the memory can take it,it must be in some language and it shouldnt be overcapacity.
    on monday i thought that we were using everywhere < and > but how it works? how can we do that, i asked the asistant she didnt give me answer but,today i got ,it was very simple.
    if (a-b)=positive a>b
    else b>a
    what a simple thing but sometimes... now i think that how can we learn that two variable is equal or not?
    is it same with above? if a-b=0 a=b or is there any algorithms?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rac1 View Post
    now i think that how can we learn that two variable is equal or not?
    is it same with above? if a-b=0 a=b or is there any algorithms?
    When you're in class, do you actually pay attention?

    Seriously, this is day one, butt simple, beginner stuff...
    Code:
    if (a == b)
      puts("*");

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    you didnt understand me ,
    i dont think how the program works with ==, i think how the machine does this?
    for example; think ,there is a ball in the space, and you are finding a ball more which is same with another.
    or is the computer has the rule to find the ball fastly? This algoritm is what?
    i thought that,the machine searching its memory that are using, and firstl it begins with the half memory, and, after it search %25 and after it search %75 after, %12.5 , %37.5 %87.5 , %62.5 ,
    it has no brain, it has algorithms.And always it works on probabilty to do best .

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    y99q, the confusion may stem from the two ways the term "variable" is used. In computer science class the instructor likely mixes the mathematical abstraction of "variable" (e.g. Let X = 5) with the programming language variable. In software the way a sequence of characters (its name) is used in place of the value mimics the mathematical sense. Yet, as you point out, in software / programming language, it can be broken down into more fundamental constituent parts: symbol (symbol table entry), memory address, contents-of, its data being used directly or interpreted as an address of something.

    One can say "X" is the variable or "symbol mnemonic X refers to some value - which is not guaranteed to be fixed during the course of the program's execution".

    Yes, I think you are overthinking it all.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    rac1, since you hijacked the thread into another topic...
    If one examines the machine code used to compare two values you will find in old machines the computer would do a subtract and see if the result is zero. Currently machines have CMP (compare) instructions which essentially do the same thing but the result is thrown away and just flags are set as to the result: zero, non-zero, greater-than, less-than. So a subsequent instruction branches dependent on such flags.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rac1 View Post
    you didnt understand me ,
    i dont think how the program works with ==, i think how the machine does this?
    for example; think ,there is a ball in the space, and you are finding a ball more which is same with another.
    or is the computer has the rule to find the ball fastly? This algoritm is what?
    i thought that,the machine searching its memory that are using, and firstl it begins with the half memory, and, after it search %25 and after it search %75 after, %12.5 , %37.5 %87.5 , %62.5 ,
    it has no brain, it has algorithms.And always it works on probabilty to do best .
    Yes I understood you...
    The simple...
    Code:
    if (a == b)
      ...
    is very straight forward...

    After compiling the computer no longer knows the names of the variables... only their addresses. So testing a against b is a simple matter of loading both into CPU registers and subtracting... if the answer is non-zero they aren't the same.

    But what you don't seem to be getting your head around is that *we don't need to care about that* since the language does it for us. To spend this much time and energy on such a silly thing only detracts from your study and better understanding of the language itself. You become "Lost in the minutia" as they say... Ultimately it doesn't matter and you shouldn't care how the computer sees it... just use the lanugage.
    Last edited by CommonTater; 11-24-2011 at 03:41 PM.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    In a higher level language, we are mainly interested with the contents of a memory
    rather than it's address. So for convenience, the variable name refers to the contents
    of some address and not the address itself.

    When you define a type int varable, 'x' for example, a memory location is reserved to
    store the value of 'x'. That memory location or address is preserved for the scope or
    duration of that variable. Any reference to 'x' will give you the contents of that address.
    The contents of that address can change, therefore the value of 'x' can change.
    'x' refers to whatever happens to be in that memory address.

    So a variable is simply a reference to something, and that something is a value stored
    in memory which can have a "variable" value, i.e. it can change.

    If you wanted the actual address of 'x', the "address of" operator '&' will give you that.
    &x is where x is stored.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Or, if you really want to get right down to it... it's a series of electrical charges stored in a group of tiny little capacitors on the memory chips in your computer.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Or we could invoke Schrodinger's cat and say that an observer forces the computer to decide what the electrons mean.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I always wondered what my programs were doing when I wasn't watching them

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    It's just a shorthand. Everyone uses it because everyone knows what it means. Name of the variable identifies the variable. Therefore, when we refer to the name, everyone knows we are referring to the variable.

    When we say "John", we aren't usually talking about the name John. We are talking about the person we associate the name with. Just like variables.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cyberfish View Post
    When we say "John", we aren't usually talking about the name John. We are talking about the person we associate the name with.
    C'mon now... trashing on John is so mean.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a really stupid question
    By -EquinoX- in forum C Programming
    Replies: 33
    Last Post: 02-02-2008, 11:29 PM
  2. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  3. Stupid Question
    By bam34 in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2004, 04:50 PM
  4. Stupid Question
    By Mornic_Programm in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2004, 07:02 PM
  5. Stupid Math Question....really stupid
    By ToLazytoSignIn in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-16-2003, 07:36 PM