QBASIC String Pattern printing program (Using SUB/FUNCTION)

3 months ago

QBasic, a variant of the BASIC programming language, is a simple and powerful tool for beginners to dive into the world of programming. In this blog post, we'll explore the creation of a fascinating string pattern program in QBasic that forms a pyramid using asterisks (*). This exercise will not only help you understand basic programming concepts but also introduce you to the art of creating patterns.

CLS
a$ = "*********"
FOR i = LEN(a$) TO 1 STEP -2
     PRINT LEFT$(a$, i)
NEXT i
END

CLS: This command, often found in BASIC languages, clears the screen, setting a clean canvas for our code's output.
String Setup: The variable a$ is assigned the string "*********", a collection of nine asterisks ready to be manipulated.
Looping for Exploration: The FOR loop takes center stage, iterating from the length of a$ down to 1, stepping backward in increments of 2.
Slicing and Printing: Within each loop iteration, the LEFT$(a$, i) function extracts a portion of a$ from its beginning, extending up to the current value of i. The extracted string, a gradually shrinking pattern of asterisks, is then displayed using PRINT.
Loop: The NEXT i statement marks the end of each loop cycle, gracefully guiding us back to the beginning of the loop until the condition is no longer met.
END: The END statement signals completion, concluding the code's execution.

SUB PROCEDURE

DECLARE SUB ptrn(a$)
CLS
a$ = "*********"
CALL ptrn(a$)
END

SUB ptrn (a$)
FOR i = LEN(a$) TO 1 STEP -2
     PRINT LEFT$(a$, i)
NEXT i
END SUB

 

FUNCTION PROCEDURE

DECLARE FUNCTION ptrn()
CLS
d = ptrn(a$)
END

FUNCTION ptrn (a$)
a$ = "*********"
FOR i = LEN(a$) TO 1 STEP -2
     PRINT LEFT$(a$, i)
NEXT i
END FUNCTION

 

 

  443