-2

Theoretically, is it possible to write programs without using variables? What will the most complex program look like? Does the answer vary depending on the language? Would you be able to use functions? Control flow?

closed as too broad by gnat, dagnelies, Pieter B, David Arno, Christophe Mar 29 '17 at 19:37

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    How do you define a variable? For example if you assign a variable only once, is it really a variable? And what about function parameters? Those are a kind of local variable in many languages. You can use them together with tail call recursion to emulate ordinary local variables. – CodesInChaos Mar 26 '17 at 13:02
  • @CodesInChaos those are great questions which I didn't think about: probably my question wasn't clear enough. My background is Python, so my current mental model of variable is a label for primitives and a reference for non-primitives and a variable can be mutated. – Runnick Mar 26 '17 at 16:38
  • If you're using Python you're going to have trouble with those damn global variables. python -c "print(dir())" proves that even the simplest Python program (ie. a zero byte excluding shebang implementation of /bin/true), is too complex to not have any variables. – Oskar Skog Mar 27 '17 at 22:03
9

A language doesn't need variables to be Turing-complete; therefore, every program that can be written at all, can also be written without variables.

Some notable languages which don't have variables, yet are Turing-complete, are:

Also related is the concept of Tacit Programming and writing code in point-free style.

In general, by thinking about how data flows through a system and is transformed and reduced in the process instead of mutating state, you can get rid of variables, and express computation as a series of combinations of functions. Think of a Unix shell pipeline, for example.

But the really obvious example is: your computer. It doesn't have variables, yet it executes all your programs.

  • 1
    This answer saves this question. I didn't know this was even possible. – Greg Burghardt Mar 26 '17 at 14:05
  • 1
    @rwong: Thanks. Your comment led me to realize that I missed the most obvious example of all: my computer! It has registers, it has RAM, it has cache, it has a CPU (several of them in fact), but you know what it doesn't have? Named state, i.e. variables. – Jörg W Mittag Mar 27 '17 at 11:21
  • 1
    A "variable" isn't something with the name "variable", it is something that uses the semantic context of a variable. A register that is being used to store a counter or even a specific spot on a turing machine's tape being used to store temp values while doing it's processing may not have the name variable, but they are pretty much acting like those. Same goes for memory addresses - using a memory space to store something is using a variable, even if you do it by hand, as you would do with brainfuck. Variables are mental models, not "physical things". – T. Sar Mar 27 '17 at 13:03
  • 1
    In other words - every single cell on the tape of a Turing Machine is a variable that can be read or written to. So, no, none of those examples you provided are actually "without variables". – T. Sar Mar 27 '17 at 13:08
  • 2
    @COMEFROM Any Turing-Complete language is dependent on variables, on any form it may appear. The Turing Machine itself is defined as a set of operations over a tape with cells that can hold symbols. Any given cell is a variable. The mathematical concept of a variable is a intrinsic part of any turing machine! – T. Sar Mar 27 '17 at 15:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.