Write a program in qbasic to print the user entered word in reverse order using FUNCTION...END FUNCTION
For i = 1 to 10
Input values!
Next i
For i = 10 to 1
Print values!
Next i
End
Sure! Here's how you can write a program in QBasic to print a user-entered word in reverse order using a FUNCTION.
```qbasic
DECLARE FUNCTION ReverseWord (word AS STRING) AS STRING
CLS
PRINT "Enter a word: ";
INPUT word$
PRINT "Reverse of the word:", ReverseWord(word$)
FUNCTION ReverseWord (word AS STRING) AS STRING
DIM reverseWord AS STRING
FOR i = LEN(word) TO 1 STEP -1
reverseWord = reverseWord + MID$(word, i, 1)
NEXT i
FUNCTION = reverseWord
END FUNCTION
```
Let's break down the code:
1. `DECLARE FUNCTION ReverseWord (word AS STRING) AS STRING`: We declare the `ReverseWord` function that takes a string as input and returns a string as output.
2. `CLS`: It clears the screen.
3. `PRINT "Enter a word: "; INPUT word$`: It prompts the user to enter a word, and stores it in the variable `word$`.
4. `PRINT "Reverse of the word:", ReverseWord(word$)`: It calls the `ReverseWord` function by passing the entered word as an argument, and prints the reverse of the word.
5. `FUNCTION ReverseWord (word AS STRING) AS STRING`: It defines the `ReverseWord` function that takes a string `word` as input and returns a string as output.
6. `DIM reverseWord AS STRING`: It declares a string variable `reverseWord` to store the reversed word.
7. `FOR i = LEN(word) TO 1 STEP -1`: It starts a loop from the length of the word down to 1, with a step of -1.
8. `reverseWord = reverseWord + MID$(word, i, 1)`: It appends each character from the original word to the `reverseWord` variable, effectively reversing the order of the characters.
9. `NEXT i`: It moves the loop to the next iteration.
10. `FUNCTION = reverseWord`: It assigns the reversed word to the function name, which will be returned as the output of the function.
Running this program will prompt the user to enter a word, and then it will print the reverse of the word.