BBS: Inland Empire Archive Date: 03-28-93 (21:04) Number: 245 From: QUINN TYLER JACKSON Refer#: NONE To: ALL Recvd: NO Subj: Quik_Bas Faq2.1 2/ Conf: (2) Quik_Bas
>>> Continued from previous message STATIC BeenHere IF NOT BeenHere THEN Low = LBOUND(Array) High = UBOUND(Array) BeenHere = -1 END IF DIM Partition AS STRING ' Change STRING to any BASIC data type ' for this QuickSort routine to work with ' things other than strings. IF Low < High THEN ' Only two elements in this subdivision; swap them if they are out of ' order, then end recursive calls: IF High - Low = 1 THEN ' we have reached the terminating condition! IF Array(Low) > Array(High) THEN SWAP Low, High BeenHere = 0 END IF ELSE ' Pick a pivot element at random, then move it to the end: RandIndex = INT(RND * (High - Low + 1)) + Low SWAP Array(High), Array(RandIndex) Partition = Array(High) DO ' Move in from both sides towards the pivot element: I = Low: J = High DO WHILE (I < J) AND (Array(I) <= Partition) I = I + 1 LOOP DO WHILE (J > I) AND (Array(J) >= Partition) J = J - 1 LOOP ' If we haven't reached the pivot element, it means that two ' elements on either side are out of order, so swap them: IF I < J THEN SWAP Array(I), Array(J) END IF LOOP WHILE I < J ' Move the pivot element back to its proper place in the array: SWAP Array(I), Array(High) ' Recursively call the QuickSortSTR procedure (pass the smaller ' subdivision first to use less stack space): IF (I - Low) < (High - I) THEN QuickSortSTR Array(), Low, I - 1 QuickSortSTR Array(), I + 1, High ELSE QuickSortSTR Array(), I + 1, High QuickSortSTR Array(), Low, I - 1 END IF END IF END IF END SUB =======>8 SAMPLE 1.0 ENDS HERE 8<========= Q1.5 So that's how to use recursion! That's great! I think I'm starting to get a hang of things with QuickBASIC now, thanks. But, how is it possible for it to call itself over and over like that without all those variables interfering with each other? I mean, I'm kind of used to GW-BASIC, and well, I just can't figure out why all those High and Low variables don't just write over one another. My docs say something about local and global scope, but it's all kind of confusing. What's the real difference between local, STATIC, COMMON, SHARED, COMMON SHARED, and all other flavors of variables? A1.5 Beginners with QuickBASIC sometimes have a hard time decrypting all of the different types of variable scope. Microsoft hasn't really helped anything with all the funny names for variable scope. GLOBAL would have made more sense than SHARED for most. Okay, let's look at how the QuickBASIC program is inevitably structured: 1. First, there is the 'module' level. That is the main part of the QuickBASIC program, the part where execution starts, and most programmers declare their constants, and put their main documentation. 2. Second, there is the SUB and FUNCTION level. Each SUB and FUNCTION could be thought of as a miniprogram unto itself. That's why SUBs are called that: subprogram. 3. Third, if you write bigger programs, you may actually have two or more modules, each one having its own SUBs and FUNCTIONs. Okay, then, any variable used at the modular level, or level 1, is accessible, or in the 'scope' of the modular level. If there is a variable called Foo at the modular level, with a value of 7, then any Foo at the SUB or FUNCTION level could also be called Foo, without interfering with the modular Foo. Think of each module level variable and each SUB and FUNCTION variable as being on different continents. They can have the same name with no problem. But, suppose you want a SUB or FUNCTION to have access to the Foo that was declared at the modular level. This is where the SHARED declarator comes in. In the SUB somesubprog, to have access to the Foo that was declared at the modular level, just add the declaration: SHARED Foo Any SUB or FUNCTION that doesn't want to have access to the modular Foo doesn't have to declare it as SHARED. This is a powerful feature, once you get the hang of it and feel confident enough to use it wisely. Now, suppose that you want a number of your SUBs or FUNCTIONs to have access to a common group of variables. At the modular level, the declaration would be: DIM SHARED Foo This would give ALL of the SUBs and FUNCTIONs of a given module access to the variable Foo. Any access of Foo at any level will alter the global variable. Now, suppose you have a multimodule program that has FIRST.BAS and SECOND.BAS linked together. Suppose you want them to communicate with one another via a common global variable. This is where COMMON SHARED comes in. Now that we've covered this, there is the issue of the STATIC declarator. Normally, variables at the SUB and FUNCTION level are dynamic, which means they disappear when the routine returns to the place that it was called from. By declaring a variable STATIC, we can be assured that whatever the variable's value was when we left, it will be when we return. To declare only a few of the variables as STATIC, use the form: SUB FooSub () STATIC Variable1, Variable2, etc. : : END SUB >>> Continued to next message * OLX 2.1 TD * Post all questions in this box --> []
Books at Amazon:
Back to BASIC: The History, Corruption, and Future of the Language
Hackers: Heroes of the Computer Revolution (including Tiny BASIC)
Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution
The Advent of the Algorithm: The Idea that Rules the World
Moths in the Machine: The Power and Perils of Programming
Mastering Visual Basic .NET