THE PUZZLET PAGE


How_Does_It_Work


FUNCTION:  Reverse().  This function takes an integer as its argument and returns the reverse of that integer to the calling routine. If its argument is, say, 123456 it will return 654321.

Declaring the function must be done like this:

declare Reverse(r: int)

Here's the code:

sub Reverse(n)
' returns the reverse of integer n
' example: n = 1234 returns 4321
def i, L: int
def n$, rev$: string
n$ = ltrim$(str$(n))
L = len(n$)
rev$ = ""
for i = L to 1 #-1
    rev$ = rev$ + mid$(n$, i, 1)
next i
return val(rev$)

Variable i is used to iterate over the individual digits of argument n after it's been converted to string format.

Variable L holds the length of argument n after conversion to a string.

Variable n$ holds the string version of n.

Variable rev$ is used to build up the reverse of n$.

I've numbered the important lines of code to assist in a detailed explanation, as shown below.

1
2
3
4
5
6
7

n$ = ltrim$(str$(n))
L = len(n$)
rev$ = ""
for i = L to 1 #-1
    rev$ = rev$ + mid$(n$, i, 1)
next i
return val(rev$)

Now for a line-by-line exegesis of the code.

1

n$ = ltrim$(str$(n)).  The argument n, that is, the integer passed from the calling routine to be reversed, is converted into string format. Normally, the function str$ adds a space at the beginning of the item it converts for formatting purposes. This is going to get in our way, so it's removed with the function ltrim$.

2

L = len(n$).  Now that we have a string to work on (n$) we can find its length. This line does that, and stores the result in variable L.

3

rev$ = "".  We're going to use variable rev$ (short for "reversed string") to build up and hold the reversal we need. This lines initialises it as an empty string, that is, one containing no characters at all.

4

for i = L to 1 #-1.  The most important task can now begin. We have to iterate over the whole of n$, working on one character at a time. This line uses i as iterator, or control. It starts at value L and reduces to value 1, going one decremental step at a time.

5

rev$ = rev$ + mid$(n$, i, 1).  A paraphrase of this line is "take one character from within n$ at position i and add it on to the end of whatever is in rev$ at this time."

Let's take the example where n$ equals "24358". At the beginning of the FOR-NEXT loop, rev$ holds "" (empty string) and i equals 5 (because i initially equals L, and L equals the length of n$). As i iterates down towards 1, rev$ is built up with its characters in reverse to n$. This is shown clearly in the table:

Value of i
Contents of rev$
Initial
""
5
"8"
4
"85"
3
"853"
2
"8534"
1
"85342"

6

next i.  Instructs the FOR-NEXT loop to go around again until i has covered the required range.

7

return val(rev$).  The transformation is complete, and all that remains to be done is to return the result to the calling routine. The argument was originally an integer, so an integer must be returned.  This line does both jobs at once. It converts rev$ to an integer by use of the "val" function, and returns the result to the calling routine.

Note: this function works perfectly well for non-numeric characters. Just change the declaration so that the parameter is a string, then delete the string conversion (line 1) from the code.


MAIN MENU
HOW DOES IT WORK?

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 5th, 2010.