The Function declaration is fine, the code should work well
Function wkout (k)
do
if Scancode ( ) = k
do
if scancode( ) <>K then exit
loop
exit
endif
loop
endfunction
The error means that the function is declared is within a nest. A nest being some command that is closed by another. If/Endif, While/Endwhile, Repeat/Until, Do/Loop, For/Next, Select/Endselect, Case/Endcase, subroutines, and even Function/Endfunction are all examples of nests. You must do the Function/Endfunction nest outside of any other nest, out in the open code, so to speak.
For example, this use of your function design works.
sync on
sync rate 45
global number as integer
do REM ---------------------- Main loop
cls
REM scancode for "K" is 37
wkout(37)
text 20, 20, str$(number)
sync
loop REM -------------------- End Main Loop
Function wkout(k)
do
if Scancode ( ) = k
do
inc number
if scancode( ) <>K then exit
loop
exit
endif
loop
endfunction
While this one produces the error you're getting.
sync on
sync rate 45
global number as integer
do REM ---------------------- Main loop
cls
REM scancode for "K" is 37
wkout(37)
text 20, 20, str$(number)
sync
Function wkout(k)
do
if Scancode ( ) = k
do
inc number
if scancode( ) <>K then exit
loop
exit
endif
loop
endfunction
loop REM ---------------------- End Main loop
Ensure that the function declaration is outside of all other nests. This is just an explanation behind what the others were saying earlier.
P.S. WOW the number variable racks up quickly considering it increments by 1 at a time per cycle of the loop while K is pressed. LOTS of loops in the split second you hold it down to tap the key. The smallest number of loops I could get from one press was 70,000.
Ask not for whom the bell tolls;
It tolls for ye!