LoginLogin

DEF - OUT variable name must be different from formal parameter names

Root / SmileBASIC Bug Reports / [.]

SquareFingersCreated:
The code
DEF TEST A$ OUT A$
 A$=A$+"1"
END
will generate an "Uninitialized variable used" error when TEST is used.

Yeah, realistically that should produce a Duplicate Variable error, since it seems that the output variable is overriding the input variable.

Better yet, it should not produce an error at all. When the procedure starts, the value of the actual parameter should be copied to the (local) variable named A$. When the procedure finishes, the value stored in the local variable A$ should be put in the variable named after the "OUT" keyword where the procedure is called.

You could achieve a similar result with something like:
DEF TEST$(A$)
 RETURN A$+"1"
END
However, the advantage of using OUT is that you can have multiple return values. Which if it were handled as you said, you could do something like:
DEF TEST A$,B$ OUT A$,B$
 A$=A$+"1"
 B$=B$+"2"
END

while this does sound bad (should be fixed IMO), it can be easily worked around:
DEF TEST INA$,INB$ OUT A$,B$
 A$ = INA$
 B$ = INB$
 'from now on just use A$ and B$...
 A$ = A$+"1"
 B$ = B$+"2"
END

while this does sound bad (should be fixed IMO), it can be easily worked around:
DEF TEST INA$,INB$ OUT A$,B$
..
END
I like this solution. Thank you! Calling it like so.
TEST A$ B$ OUT A$ B$
Previously I accomplished this by passing in an array as a parameter (it's address) and altering the element values. This modifies the original contents without having to use OUT.
DEF MY_TEST ARR
 ARR[0]=7
 ARR[1]=8
 ARR[2]=9
END

VAR X[3]
X[0]=1
X[1]=2
X[2]=3
? X[0];",";X[1];",";X[2]
MY_TEST X
? X[0];",";X[1];",";X[2]

while this does sound bad (should be fixed IMO), it can be easily worked around:
DEF TEST INA$,INB$ OUT A$,B$
..
END
I like this solution. Thank you! Calling it like so.
TEST A$ B$ OUT A$ B$
Previously I accomplished this by passing in an array as a parameter (it's address) and altering the element values. This modifies the original contents without having to use OUT.
DEF MY_TEST ARR
 ARR[0]=7
 ARR[1]=8
 ARR[2]=9
END

VAR X[3]
X[0]=1
X[1]=2
X[2]=3
? X[0];",";X[1];",";X[2]
MY_TEST X
? X[0];",";X[1];",";X[2]
That's very interesting. I did not know that you can pass arrays as args.

The reason this happens is that, inside the function, there are TWO variables named "A$". Since the one passed to the function was created first, trying to access "A$" will always access that one, and not the OUT variable "A$". Since OUT A$ can never be set, an "Uninitialized variable used" error will be triggered. I agree that this should have caused a duplicate variable error.