0
$\begingroup$

If a programming language only supports global variables, is it still possible to use asynchronicity, like callbacks, event-driven programming, other kinds of parallelisation?

How can it be done?

$\endgroup$
4
  • $\begingroup$ It could be done with patterns and strong discipline. $\endgroup$
    – Steve
    Mar 31 '21 at 12:59
  • 3
    $\begingroup$ Assembly language only has global variables (registers). $\endgroup$ Mar 31 '21 at 13:13
  • $\begingroup$ @YuvalFilmus so imagine you restricted yourself in JS to not allow non-global scope variables. The only way to do complex tasks is to simulate memory (using a huge array in a global variable ig) and write JS as if it were assembly? $\endgroup$ Mar 31 '21 at 17:27
  • $\begingroup$ There's usually more than one way to accomplish a programming task. $\endgroup$ Mar 31 '21 at 18:47
1
$\begingroup$

Every program that uses local variables can be converted to one that uses only global variables. So, the answer is trivially yes. However, it might not be pleasant for the software developer.

$\endgroup$
3
  • $\begingroup$ There's trickery if we also allow recursive calls, but the trickery is doable so long as the global variables can hold complex data (lists, for example). $\endgroup$ Apr 1 '21 at 8:05
  • $\begingroup$ "can be converted to one that uses only global variables" is this only true if the language supports arrays or similar? $\endgroup$ Apr 1 '21 at 10:12
  • $\begingroup$ @theonlygusti, the language has to be Turing-complete. So, arrays, yes, or something similar. Recursive calls have to be translated appropriately (as Andrej Bauer indicates): a natural way is to emulate the call stack by a data structure (a list or stack). $\endgroup$
    – D.W.
    Apr 1 '21 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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