| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Code FAQ [4 /15 |
'>>> Page 4 of Code FAQ begins here. PRINT " FUNCTION variables must be enclosed in a group parentheses, and" PRINT " we may also use VAL parentheses. First, we will pass by VALue." PRINT : MyStr$ = "This won't be changed.": MySub (MyStr$) PRINT " MyStr$ = "; MyStr$: PRINT PRINT " As you can see, MyStr$ is unchanged" DO: LOOP WHILE INKEY$ = "": CLS PRINT " Now we will pass by SEGment.": PRINT MyStr$ = "This will be changed." MySub MyStr$: PRINT " MyStr$ = "; MyStr$: PRINT PRINT " Since the ADDRESS of MyStr$ was passed, the SUB was" PRINT " able to change the contents of the variable." DO: LOOP WHILE INKEY$ = "": CLS PRINT " Now we will use a FUNCTION and pass by VALue.": PRINT MyStr$ = "This won't be changed.": FuncStr$ = MyFunc((MyStr$)): PRINT " MyFunc$ = "; FuncStr$: PRINT " MyStr$ = "; MyStr$: PRINT PRINT " As a result, MyStr$ is unchanged, and a new string is created." DO: LOOP WHILE INKEY$ = "": CLS PRINT " Now we will pass MyStr$ by SEGment.": PRINT MyStr$ = "This will be changed.": FuncStr$ = MyFunc(MyStr$): PRINT " MyFunc$ = "; FuncStr$: PRINT " MyStr$ = "; MyStr$: PRINT PRINT " We still got the new string, but now MyStr$ has changed!" DO: LOOP WHILE INKEY$ = "": CLS PRINT " Unlike subroutines using GOSUB, variables found in the" PRINT " MAIN module are unknown in the SUB or FUNCTION routines." PRINT " This is known as the SCOPE of a variable. In order for a" PRINT " variable to be VISIBLE to a SUB or FUNCTION, it must be" PRINT " declared as being SHARED, or be passed as an argument." DO: LOOP WHILE INKEY$ = "": PRINT MyStr$ = "This is MyStr$.": MyShare: PRINT " MyStr$ = "; MyStr$: PRINT PRINT " This way, variables are available to your SUB or FUNCTION." DO: LOOP WHILE INKEY$ = "": CLS PRINT " In addition, we may declare variables SHARED in the main" PRINT " module by use of the DIM statement.": PRINT DIM OurStr AS STRING * 32: OurStr = "This is a shared string." MySub OurStr: PRINT " OurStr = "; OurStr: PRINT PRINT " So here we see that this string is shared in the SUB." DO: LOOP WHILE INKEY$ = "": PRINT PRINT " If we were to pass a fixed length string as an argument," PRINT " then the contents of the string would be passed and not the" PRINT " string itself, although, if that string were passed by SEGment," PRINT " then whatever changes were made to the passed string would be" PRINT " stored in the fixed string when we exit the SUB or FUNCTION." DIM FixedStr AS STRING * 10: FixedStr = "A string" PRINT " FixedStr = "; FixedStr: Fixed FixedStr PRINT " FixedStr = "; FixedStr: PRINT PRINT " The SUB string is a temporary dynamic string created by the SUB" PRINT " and truncated to fit into the fixed length string." DO: LOOP WHILE INKEY$ = "": PRINT PRINT " I hope this answers most of your questions concerning" PRINT " the use of SUBs and FUNCTIONs in QBasic and Quick Basic." DO: LOOP WHILE INKEY$ = "": COLOR 2, 0: CLS SUB Fixed (p$) PRINT " (passed string)p$ = "; p$ p$ = "This string is way too big for FixedStr." PRINT " (passed string)p$ = "; p$ END SUB FUNCTION MyFunc$ (p$) PRINT " (passed variable)p$ = "; p$: p$ = "changed" MyFunc$ = "This is an entirely new string!" END FUNCTION SUB MyShare SHARED MyStr$: PRINT " SHARED variable MyStr$ = "; MyStr$ MyStr$ = "This variable, being SHARED, may be changed." END SUB SUB MySub (p$) PRINT " (passed variable)p$ = "; p$: p$ = "changed" END SUB '_|_|_| end SUBTUTOR.BAS [end quote] 3) HOW CAN I PRODUCE A DELAY IN MY PROGRAM INDEPENDENT OF THE CPU SPEED? Bill White answers this question very well: [quote] In the past there has been extensive discussion in this echo of this subject, which brought out some problems. A summary of this discussion follows: The old BASIC statement we used for years: FOR i=1 to 1000: NEXT i is not independent of the CPU speed. '>>> Page 4 of Code FAQ ends here. Continued in next message. * Brought to you by Greg's Little QBasic Auto-Poster * --- Maximus 3.01* Origin: The BandMaster, Vancouver, B.C., Canada (1:153/7715) SEEN-BY: 633/267 270 @PATH: 153/7715 140/1 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.