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
    Commented Mar 31, 2021 at 12:59
  • 3
    $\begingroup$ Assembly language only has global variables (registers). $\endgroup$ Commented Mar 31, 2021 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$
    – minseong
    Commented Mar 31, 2021 at 17:27
  • $\begingroup$ There's usually more than one way to accomplish a programming task. $\endgroup$ Commented Mar 31, 2021 at 18:47

1 Answer 1

2
$\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$ Commented Apr 1, 2021 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$
    – minseong
    Commented Apr 1, 2021 at 10:12
  • 1
    $\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.
    Commented Apr 1, 2021 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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