An explanation of how the VAL$ function works and some possible uses
Overview
VAL$ is quoted as 'probably not very useful' in the ZX Spectrum User Manual and indeed there are not many examples of its use in the computer press. It is possible that this is because VAL$ was only given one paragraph in the user manual and it is not really explained what VAL$ could be used for. There are a few examples in the manual which explain how VAL$ strips bounding quotes from a string but does not explain how this may by of use in a program.
VAL$ is explained in the user manual alongside the function VAL and in fact both functions are handled by the BASIC interpreter through the same ROM routine - a calculator subroutine at address 35DEh (13790 decimal). Therefore, to understand VAL$ we can first see how the VAL function works.
The VAL Function
Simply, VAL takes a string or string variable and attempts to convert it to a number, in other wordsPRINT VAL "250" will convert the string "250" to the number 250 and display it on the screen.
This does not look especially useful until we type thisPRINT VAL "10*25".
What we have done is create a string containing a simple sum, 10*25 (or 10 multiplied by 25). The Spectrum will evaluate this string and display the result which is 250.
Now what we can do is instead of having a fixed string like "10*25" which cannot be changed we can use a string variable to hold the sum before evaluation, like this
LET a$="10*25"
PRINT VAL a$
We now have a much more flexible function as we can now change a$ to be "5*3" or "6+2" or "110/4+4-2" or any other numerical expression and because the orignal sum is held in a string we can also change any part of it using string slicing, for instance if we take "100/4+4-2" but we only want the first part "100/4" we can writePRINT VAL a$(1 TO 5) which will return 25.
As you can see using VAL is much more flexible than just using a numeric variable to hold a sum or an equation, you can even represent a numeric variable within a string variable and evaluate it with VAL like this
LET x=10
LET a$="x*4"
PRINT VAL a$
Which displays 40 because x=10 ∴ x*4=40.
We can do much the same with VAL$ except we shall be evaluating a string containing string information instead of a string containing numeric information as in VAL.
The examples from the Spectrum Manual
As explained above VAL evaluates a numeric expression held within a string but VAL$ evaluates a string within a string. Evaluating a string within string doesn't seem very useful at first glance so let us consider the examples of VAL$ (and VAL) from the Sinclair ZX Spectrum Manual (Chapter 9 page 59).
Example i) VAL$ “““Fruit punch”“” = “Fruit punch”
Why does left side equal the right side? To find this out we must break the equation down into steps. Any string constant must be contained within double quotes i.e. "this is a string", these double quotes are called bounding quotes and these bounding quotes are stripped (See Fig.1). Then the double-quote pairs are changed to single quotes leaving us with "Fruit Punch". This is now the string which is returned for the left side of the equation.
Now before we continue with rest of the examples let us considerLET a$="99"
Example ii) PRINT VAL a$ Returns: the number 99
VAL evaluates the string a$ containing "99" as the number 99.
Example iii) PRINT VAL "a$" Returns: Error 'Nonsense in BASIC'
This is because VAL is trying to evaluate the string a$ containing a string instead of a string containing a number.
Example iv) PRINT VAL """a$""" Returns: Error 'Nonsense in BASIC'
Again, VAL is trying to evaluate the string a$ containing a string instead of a string containing a number.
Example v) PRINT VAL$ a$ Returns: Error 'Nonsense in BASIC'
This time VAL$ is trying to evaluate the string a$ which contains a number instead of containing a string.
Example vi) PRINT VAL$ "a$" Returns: the string containing the characters 99
VAL$ evaluates the string containing "a$". Once the quotes of the string are stripped this becomes the string variable a$ which contains the characters '99' (not the number 99).
Example vii) PRINT VAL$ """a$""" Returns: the string containing the characters a$
VAL$ evaluates the string containing """a$""". Once the quotes of the string are stripped we are left with a set of single quotes and this becomes the string "a$" which contains the characters 'a$' (not the variable a$).
Did you notice in the last example how the result was neither 99 nor an error but the characters 'a' and '$'?
This is interesting because VAL$ """a$""" produces the string "a$". If we now apply another VAL$ to that i.e.
PRINT VAL$ VAL$ """a$"""
The first VAL$ would strip the final quotes and leave the string variable a$ leaving us with the string value of "99". Furthermore we can apply VAL to this:
PRINT VAL VAL$ VAL$ """a$"""
This evaluates the "99" string value as a number leaving us with the number 99.
Now let's try this:
LET a$="99"
LET b$="a$"
PRINT VAL$ b$
VAL$ evaluates b$ as the string variable a$ and display the string "99"
Using VAL$
Remember how we manipulated a string before evaluating with VAL? PRINT VAL a$(1 TO 5) Well we can do the same with VAL$
Consider this:
10 LET a$="Fruit punch"
20 LET b$="a$"
30 LET c$="(1 TO 5)"
40 LET d$="b$+c$"
50 PRINT VAL$ d$
The result of this is to process the string variable d$ which gives us "b$+c$". Then the quotes are stripped leaving "a$(1 TO 5)" which is further evaluated as the string "Fruit".
As you can see with VAL$ we can point to any string variable from within a program and evaluate it. In other words we could quite easily change c$ to read "(7 TO)" which would evaluate as "punch" or we could change the 'a' in a$ to another string variable altogether, x for example.
Conclusion
It has been shown that strings can be altered and manipulated before being processed by VAL$. One use for this is within a user-definable function. Using VAL$ within a DEF FN statement we can manipulate a string to calculate how far a recursive function should call itself before terminating. This is only possible by use of the VAL$ function. Another use would be within a spreadsheet program to process cells with column sizes held in a string variable before being passed to VAL$ for ouptut. An example of this appears in the book Information Handling for the ZX Spectrum.
VAL$ is probably one of the least used functions on the ZX Spectrum but if used correctly can be put to good use and could be quite a powerful function in the right program.
References
Sinclair ZX Spectrum BASIC Programming, Steven Vickers (author), Robin Bradbeer (editor), Chapter 9, p.58/59
Information Handling for the ZX Spectrum, C. A. Street (author), McGraw-Hill Book Company (UK) Ltd (publisher), Chapter 3, p.42-45