03-11-2021, 01:51 PM
Hi everyone,
In ZXBASIC, is this a valid program?
I have a variable labelled 'n' in both the top-level program and a function. I believe, in the function, 'n' is a different, locally defined variable, which is deleted when the function ends.
Would the same work, if I replaced the function with a subroutine (accepting I couldn't return an answer in the same way)?
I ask as I am seeing unexpected behaviour in my program (not the one above, a much longer program) and one possibility is that I'm using the same variable names for different entities in both the top-level program and subroutines.
Thanks in advance for any help
In ZXBASIC, is this a valid program?
Code:
function myFunction( name$ as string ) as uinteger
dim length, n as ubyte
dim total as uinteger = 0
length = len(name$)
if length=0 then
return total
end if
for n = 0 to length-1
total = total + code(name$(n))
next n
return total
end function
REM Main program
dim n as ubyte
dim ans as uinteger
n = 14
ans = myFunction("Hello")
print "Answer = " + str$(ans)
print "n = " + str$(n)
stop
I have a variable labelled 'n' in both the top-level program and a function. I believe, in the function, 'n' is a different, locally defined variable, which is deleted when the function ends.
Would the same work, if I replaced the function with a subroutine (accepting I couldn't return an answer in the same way)?
I ask as I am seeing unexpected behaviour in my program (not the one above, a much longer program) and one possibility is that I'm using the same variable names for different entities in both the top-level program and subroutines.
Thanks in advance for any help