Metamorphing Machine I rather be this walking metamorphosis
than having that old formed opinion about everything!

Programming like it's 1984

Suppose you need to crack a four-digit password. Suppose also that you live in the 1980s and you know BASIC.
There are two system-provided functions to help you: GetFourDigits and CheckPasswordMatch.
GetFourDigits accepts four integers and joins them into a password.
CheckPasswordMatch returns TRUE if you provided the correct password, FALSE otherwise.

That was the task Bob, from Stranger Things, had to do to save himself and the ones with him. While being chased by demodogs...
We're going to dissect his program in this post. Here is a screenshot from his code.

Although variable declaration is not mandatory in BASIC, he started by declaring one. (I'm using a different dialect and formatting from his:)

10 DIM FourDigitPassword AS INTEGER

Then, he started four loops, so each variable would vary from zero to nine:

20 FOR I = 0 TO 9
30 FOR J = 0 TO 9
40 FOR K = 0 TO 9
50 FOR L = 0 TO 9

Then he passes these four variables to the GetFourDigits function:

60 FourDigitPassword = GetFourDigits(I, J, K, L)

What does this function do? We don't know, but we'll come back to it later.
Then he checks whether he got the correct password using the CheckPasswordMatch function.
If it is a match, he doesn't need to continue guessing the password, so he breaks from the four loops to display the password:

70  IF CheckPasswordMatch(FourDigitPassword) = TRUE THEN
80     GOTO 140
90  END IF
100 NEXT L
110 NEXT K
120 NEXT J
130 NEXT I
140 PRINT FourDigitPassword

Then he ends the program:

150 END

Here we note he used best practices for that time, numbering the lines so he could trace it if he ever needed. In fact, I'm sure he needed.
Now, taking as face value that the password was indeed four digits, what would GetFourDigits look like?
Naively, we could suppose it was along the lines below:

FUNCTION GetFourDigits(D1, D2, D3, D4)
GetFourDigits = D1 * 1000 + D2 * 100 + D3 * 10 + D4
END FUNCTION

The problem is when we feed it with the four variables in their very first iteration, all of them would be zeros, so we would get zero as an answer.
But 0 does not have four digits!

So maybe it returned a string instead of an integer:

FUNCTION GetFourDigits$(D1, D2, D3, D4)
GetFourDigits$ = LTRIM$(STR$(D1)) + LTRIM$(STR$(D2)) + LTRIM$(STR$(D3)) + LTRIM$(STR$(D4))
END FUNCTION

Now, when assigning the returned string to an integer variable, it would be converted to an integer, so the returned value of "0000" would become 0.
Then, when passing 0 to CheckPasswordMatch, if it was expecting to receive a string, it would convert 0 to "0".
The fix is simple: Just change the variable declaration to string:

10 DIM FourDigitPassword AS STRING

As he managed to get the password, we can only suppose it did not start with a zero.
Also, due to his time constraints, I'm sure we can all forgive him for his minor missteps.

Andrej Biasic
2020-05-20