home | download | demos | platforms | restrictions | help |
What is Structured BASIC ?
Structured BASIC is a form of BASIC in which statments are grouped. In contrast non-structured BASIC uses 'goto' statements and line numbers to overcome the lack of built-in statement grouping.
Non-structured BASIC allows statements to be grouped together into a kind of sub-program known as a subroutine. Such a subroutine, although possibly well defined in the mind of the programmer, is NOT well defined within a non-structured BASIC program.
Structured BASIC also allows statements to be grouped together into a kind of sub-program, but this sub-program is known as a function. Structured BASIC functions differ from non-structed BASIC subroutines primarily in that they are well defined groups of statements and they have their own local variables and a formal parameter list. This helps reduce development time, improves code robustness and promotes code reusability (importing and using self contained functions from other tried and tested programs)
example 1
lack of statement grouping in non-structured BASIC requires inverted condition and statement hoping to simulate 'if..then..else' capabilities inherent in structured BASICnon-structured BASIC if a > b then 100 x = a goto 110 100 x = b 110structured BASIC equivalent if a <= b then x = a else x = b endif
example 2
Nested 'if..then' statements in non-structured BASIC further complicates condition and statement hoping. The structred BASIC version is far simpler to write and much easier to maintain (try comming back to it after a few week and see which is easier to understand and correct if there is a problem).non-structured BASIC if a > b then 100 if a > c then 90 x = a goto 95 90 x = c 95 goto 110 100 if b > c then 105 x = b goto 107 105 x = c 107 110structured BASIC equivalent if a <= b then if a <= c then x = a else x = c endif else if b <= c then x = b else x = c endif endif
example 3
The non-structured BASIC subroutine can be entered at an line. Entering it at line 103 causes all the functionality to be skipped. The non-structured BASIC compiler does not enforce a single entry point or in anyway know which lines belong to the subroutine.The non-structured BASIC subroutine shown here relys on two input values being preloaded into the variables called 'a' and 'b' before the subroutine is called. The non-structured BASIC compiler does not know this requirement and cannot warn the programmer if this is missing. Also the retsult is returned in the variable called 'res'. If the programmer uses a different variable by mistake, the compiler will not be able to warn the programmer because the relationship between the result and the subroutine is not known.
The structured BASIC function on the other hand explicitly specifies formal parameters and a result. The structured BASIC compiler is therefor able to notify the programmer of missuse which the non-structured BASIC compiler cannot.
non-structured BASIC 100 rem subroutine to sum 101 rem two numbers 102 res = a + b 103 return a = 10 b = 20 gosub 100 c = resstructured BASIC equivalent // function to sum two numbers proc int SUM(int a, int b) return a + b endproc c = SUM(10, 20)
example 4
The structured BASIC function also has the advantage of allowing the parameters to supplied in situ and for the result to be used directly in an expression. This greatly increases readability and further reduces the possibility or errors in the programnon-structured BASIC 100 rem subroutine to sum rem two numbers res = a + b return a = 10 b = 20 gosub 100 c = res a = 15 b = 25 gosub 100 c = c + resstructured BASIC equivalent // function to sum two numbers proc int SUM(int a, int b) return a + b endproc c = SUM(10, 20) + SUM(15, 25)
home | download | demos | platforms | restrictions | help |