Structured Programming: QB IS STRUCTURED!!!
- Posted by "Lynn Kilroy" <leks_transportation at hotmail.com> Jan 17, 2006
- 697 views
>Hi > >Ah ha - the goto / gosub/return argument again. > >Short answer no - look through the mailing list archives for all the pro >and con arguments. Assume there are no subroutines - in the qbasic sense. >When you get used to euphoria, its far prettier and easier to read. > >As with most things euphoria, there are work arounds > >functions - return a value >procedures - do not return a value > >both need to to be found in your program (in the way the program is read), >before you actually call them (can use routine_ids, but keep it simple for >now) > >The 'preferred' method is to only use local variables to your >functions/procedured, as this leads to prettier, more re usable code, eg > >}}} <eucode> >function foo(integer x) >integer y >y = x * 2 >return y >end function > >integer z >z = foo(2) ></eucode> {{{ > >now if you used untidy global variables (bad programming practice (hands up >those who NEVER do it)) > >}}} <eucode> >integer y, z > >procedure foo(integer x) >y = x * 2 >end procedure > >foo(4) >z = y ></eucode> {{{ > >yeuch, horrible. > >Is this what you meant. > >Also say goodbye to on x gosub/goto, and switch/case statements (are these >in qbasic?) > >Chris Tried it. Didn't work. Now, since you're so smart, mebbe you can tell me if you can find a single goto / gosub / return in this program which is just full of subroutines. And by the way, I insist you read through the whole thing. I have the buggery that prompted this whole mess in the first place copied neatly in the code. Before you ask, yes, it is in the graphics.e file. I looked. Yes, the include statement is in the beginning of the file, before the call to the included file's subroutine. But I get this error. Mebbe you, who knoweth I do not understandith structured programming, could explain to me what the proper structureth be-ith so I can codeth in the correcteth structureth? Your ignorance of the structured nature of QB astounds me. The only thing that could possibly astound me more is the fact that QB - written probably close to twenty years ago - parses all the subroutines in to their own little windows, so you can edit them individually without having to search through the code to find them, and Ed does not. So much for readability. Love & Friendship & Blessed Be! Lynn Erika Kilroy <!-------------------BEGIN QBASIC SOURCE-------------------> DECLARE SUB ColorLocateInputJustAboutAnything (FGC AS INTEGER, BGC AS INTEGER, CTFGC AS INTEGER, CTBGC AS INTEGER, Row AS INTEGER, Col AS INTEGER, Text AS STRING, VWidth AS INTEGER) DECLARE SUB ColorLocatePrint (FGC AS INTEGER, BGC AS INTEGER, Row AS INTEGER, Col AS INTEGER, Text AS STRING) DECLARE SUB EndProgram () DECLARE SUB Initialize () DECLARE SUB Inkey (a AS STRING) DECLARE SUB MasterControlProgram () DECLARE SUB Prekey (a AS STRING) DECLARE SUB WaitKey (a AS STRING) DIM SHARED LastRow AS INTEGER Initialize MasterControlProgram EndProgram SUB ColorLocateInputJustAboutAnything (FGC AS INTEGER, BGC AS INTEGER, CTFGC AS INTEGER, CTBGC AS INTEGER, Row AS INTEGER, Col AS INTEGER, Text AS STRING, VWidth AS INTEGER) : REM FGC = ForeGroundColor : REM BGC = BackGroundColor : : REM CTFGC = CursorTextForeGroundColor : REM CTFGC = CursorTextBackGroundColor : : REM Row = The Row we Print it on : REM Col = The Column we Print It on : : REM Text = The Text We're Editing. Yes. Editing. What'd you think? This : REM was going to be like INPUT? : : REM VWidgth = ViewWidth, in Characters. : DIM CursorPosition AS INTEGER : DIM VTextStart AS INTEGER : DIM InputMode AS STRING : DIM a AS STRING CursorPosition = LEN(a) + 1 VTextStart = CursorPosition - VWidth InputMode = "OverWrite" WHILE a <> CHR$(13) IF CursorPosition < VTextStart THEN VTextStart = CursorPosition ELSEIF CursorPosition - VWidth > VTextStart THEN VTextStart = CursorPosition - VWidth END IF IF VTextStart < 1 THEN VTextStart = 1 END IF ColorLocatePrint FGC, BGC, Row, Col, SPACE$(VWidth) ColorLocatePrint CTFGC, CTBGC, Row, Col + VWidth, MID$(InputMode, 1, 1) : IF LEN(Text) > VWidth THEN ColorLocatePrint FGC, BGC, Row, Col, MID$(Text, VTextStart, VWidth) ELSE ColorLocatePrint FGC, BGC, Row, Col, Text END IF : ColorLocatePrint CTFGC, CTBGC, Row, Col + CursorPosition - VTextStart, MID$(Text + CHR$(176), CursorPosition, 1) WaitKey a IF LEN(a) = 1 THEN : IF a = CHR$(8) THEN : IF CursorPosition > LEN(Text) THEN Text = MID$(Text, 1, CursorPosition - 2) ELSE Text = MID$(Text, 1, CursorPosition - 2) + MID$(Text, CursorPosition, LEN(Text) - CursorPostion) END IF : CursorPosition = CursorPosition - 1 : ELSEIF a = CHR$(27) THEN : Text = "" CursorPosition = 1 : ELSEIF a > CHR$(31) THEN : IF CursorPosition > LEN(Text) THEN Text = Text + a ELSE IF InputMode = "OverWrite" THEN MID$(Text, CursorPosition, 1) = a ELSEIF InputMode = "Insert" THEN Text = MID$(Text, 1, CursorPosition - 1) + a + MID$(Text, CursorPosition, LEN(Text) - CursorPosition) END IF END IF : CursorPosition = CursorPosition + 1 : ELSEIF a <> CHR$(13) THEN : BEEP : END IF : ELSEIF LEN(a) = 2 THEN : IF a = CHR$(0) + "G" THEN : CursorPosition = 1 : ELSEIF a = CHR$(0) + "K" THEN : CursorPosition = CursorPosition - 1 : IF CursorPosition < 1 THEN CursorPosition = 1 BEEP END IF : ELSEIF a = CHR$(0) + "M" THEN : CursorPosition = CursorPosition + 1 : IF CursorPosition > LEN(Text) + 1 THEN CursorPosition = LEN(Text) + 1 BEEP END IF : ELSEIF a = CHR$(0) + "O" THEN : CursorPosition = LEN(Text) + 1 : ELSEIF a = CHR$(0) + "R" THEN : IF InputMode = "OverWrite" THEN InputMode = "Insert" ELSE InputMode = "OverWrite" END IF : ELSEIF a = CHR$(0) + "S" THEN : IF CursorPosition > LEN(Text) THEN BEEP ELSE Text = MID$(Text, 1, CursorPosition - 1) + MID$(Text, CursorPosition + 1, LEN(Text) - CursorPosition) END IF : END IF END IF WEND END SUB SUB ColorLocatePrint (FGC AS INTEGER, BGC AS INTEGER, Row AS INTEGER, Col AS INTEGER, Text AS STRING) COLOR FGC, BGC LOCATE Row, Col PRINT Text END SUB SUB EndProgram ColorLocatePrint 15, 6, LastRow, 5, "This completes the demonstration program I have written to show how QB" ColorLocatePrint 15, 6, LastRow + 1, 5, "****REALLY**** looks. If anyone sees this and can possibly label it " ColorLocatePrint 15, 6, LastRow + 2, 5, "as sloppy or difficult to understand or as anything other than modular" ColorLocatePrint 15, 6, LastRow + 3, 5, "programming in it's purest form, then I would love for them to prove " ColorLocatePrint 15, 6, LastRow + 4, 5, "it. In the meantime, don't yell at me because you use " + CHR$(34) + "procedure" + CHR$(34) + " " ColorLocatePrint 15, 6, LastRow + 5, 5, "where I use " + CHR$(34) + "subroutine," + CHR$(34) + " and now know that a function is a function " ColorLocatePrint 15, 6, LastRow + 6, 5, "is a function, no matter the language. " ColorLocatePrint 15, 6, LastRow + 8, 5, "Now back to my question. I said 'include graphics.e.' Euphoria said " ColorLocatePrint 15, 6, LastRow + 9, 5, "'O.K.' I said 'Graphics_Mode (260). Euphoria said 'Graphics_Mode has" ColorLocatePrint 15, 6, LastRow + 10, 5, "not been declared.' If I am not mistaken, 'Graphics_Mode' is a " ColorLocatePrint 15, 6, LastRow + 11, 5, "declared Subroutine in Graphics.E? Or do I need a seperate Declare " ColorLocatePrint 15, 6, LastRow + 12, 5, "Procedure statement in the main source code? " END SUB SUB Initialize WIDTH 80, 50 COLOR 14, 4 CLS ColorLocatePrint 14, 6, 5, 36, "Welcome!" ColorLocatePrint 14, 6, 6, 5, "Since you had the nerve to say how poorly restrictive QBasic code was " ColorLocatePrint 14, 6, 7, 5, "writing QBasic Code was, I decided to provide this little sample. " ColorLocatePrint 15, 6, 9, 5, "Please note that all the information for printing these lines is in " ColorLocatePrint 15, 6, 10, 5, "ONE statement. Now we will have you enter some text, and print it out" ColorLocatePrint 15, 6, 11, 5, "for you to read. " ColorLocatePrint 15, 6, 14, 5, "Ready? " ColorLocatePrint 15, 6, 16, 5, "Begin. " END SUB SUB Inkey (a AS STRING) a = INKEY$ END SUB SUB MasterControlProgram DIM a AS STRING DIM b AS INTEGER DIM c AS STRING ColorLocateInputJustAboutAnything 15, 6, 0, 7, 18, 5, a, 70 ColorLocatePrint 15, 6, 20, 5, "I will now attempt to parse your input. " LastRow = 22 WHILE LastRow < 41 AND LEN(a) > 70 b = 71 WHILE b > 1 AND MID$(a, b, 1) <> " " b = b - 1 WEND IF MID$(a, b, 1) = " " THEN c = MID$(a, 1, b - 1) ColorLocatePrint 14, 6, LastRow, 5, c + SPACE$(70 - LEN(c)) a = MID$(a, b + 1, LEN(a) - b) ELSE a = "Truncated: " + a ColorLocatePrint 14, 6, LastRow, 5, MID$(a, 1, 70) a = MID$(a, 71, LEN(a) - 70) END IF LastRow = LastRow + 1 : IF LastRow = 31 THEN IF LEN(a) > 0 THEN a = "***Too Many Lines To Display!***" END IF END IF WEND IF LEN(a) > 0 THEN ColorLocatePrint 14, 6, LastRow, 5, a + SPACE$(70 - LEN(a)) END IF LastRow = LastRow + 2 END SUB SUB Prekey (a AS STRING) a = " " WHILE a > "" Inkey a WEND END SUB SUB WaitKey (a AS STRING) Prekey a WHILE a = "" Inkey a WEND END SUB