@Hodgey:
Quote: "What's wrong with GOSUB? From my point of view a subroutine is just a function without the scope change"
I have nothing against GOSUB if other people want to use it, but I personally don't like using it because, as I said, we each have preferences and GOSUB is not one of mine. I had a baaad experience with it as a young teen that left me scarred for life. I had 6 months therapy to get over it. Just kidding... Or am I? Anyways, to get back to the question, I personally don't like to use GOSUB or GOTO(in BASIC) because I like to be able to pass data to my calls. I also like the ability to return data from my calls. Then there are aesthetics. I like unification and consistency. So, if I use functions for one thing, I use them for all things. There is nothing you can do with GO-x that you can't do with functions in decently constructed code (as implied by Grog). Here are some examples below (I appologise for my verbosity, as always):
`EXAMPLE: GOSUB to initialise data and GOSUB in gameplay
`SEARCH COMPATIBLE DISPLAY MODE, ETC.
GOSUB InitData
Game()
END
InitData:
Player$ = "Goku"
PlayerLives = 9999
PlayerHair$ = "Ridiculous"
PlayerID = 5
`And a bunch of other stuff
RETURN
wado_Die:
PRINT "You cannot die, you are immortal until I kill you!"
RETURN
wado_Drink:
PRINT "You can't drink that!!"
RETURN
GameOver:
PRINT " "
PRINT "YOU DIED: GAME OVER!"
PRINT "NO CONTINUES! HAHAHAHA!!!"
PRINT "YOU LOST, YOU LOST, C'MON, SMASH THAT KEYBOARD!"
WAIT KEY
END
RETURN
Function Game()
Flag = 0
INPUT "What do you want to do now? ", wado$
CLS
tmp$ = lower$(wado$)
IF left$(tmp$, 3) = "die": GOSUB wado_Die: Flag = 1: ENDIF
IF left$(tmp$, 5) = "drink": GOSUB wado_Drink: Flag = 1: ENDIF
IF Flag = 0: PRINT "You cannot ", wado$, " now.": ENDIF
`^Programming reference: Early text-adventure games' method of handling input outside the spectrum.
PRINT ""
PRINT "...Therefore..."
GOSUB GameOver
EndFunction
`EXAMPLE: Functions only
`SEARCH COMPATIBLE DISPLAY MODE, ETC.
InitData()
Game()
END
Function BadAI(PInput$)
`Hahaha, again with that bad text-adventure AI:
RetVal$ = "You cannot " + PInput$ + " now."
tmp$ = lower$(PInput$)
IF LEFT$(tmp$, 3) = "die": RetVal$ = "You cannot die, you are immortal until I kill you!": ENDIF
IF LEFT$(tmp$, 5) = "drink": RetVal$ = "You can't drink that!!": ENDIF
EndFunction RetVal$
Function Game()
INPUT "What do you want to do now? ", wado$
CLS
tmp$ = BadAI(wado$)
PRINT tmp$
PRINT " "
PRINT "YOU DIED: GAME OVER!"
PRINT "NO CONTINUES! HAHAHAHA!!!"
PRINT "YOU LOST, YOU LOST, C'MON, SMASH THAT KEYBOARD!"
WAIT KEY
END
EndFunction
Function InitData()
Player$ = "Goku"
PlayerLives = 9999
PlayerHair$ = "Ridiculous"
PlayerID = 5
`And a bunch of other stuff
EndFunction
`EXAMPLE: Functions only, initialisations preceeding main program (My Favourite because it's linearest! *pretends "linearest" is a word by adding it to his dictionary*)
GLOBAL World: World = 1
GLOBAL Fish: Fish = 1
DIM AnImg(10): FOR i = 1 TO 10: AnImg(i) = 100 + i: NEXT i
Player$ = "Goku"
PlayerLives = 9999
PlayerHair$ = "Ridiculous"
PlayerID = 5
`SEARCH COMPATIBLE DISPLAY MODE, ETC.
Game()
END
Function BadAI(PInput$)
`Hahaha, again with that bad text-adventure AI:
RetVal$ = "You cannot " + PInput$ + " now."
tmp$ = lower$(PInput$)
IF LEFT$(tmp$, 3) = "die": RetVal$ = "You cannot die, you are immortal until I kill you!": ENDIF
IF LEFT$(tmp$, 5) = "drink": RetVal$ = "You can't drink that!!": ENDIF
EndFunction RetVal$
Function Game()
INPUT "What do you want to do now? ", wado$
CLS
tmp$ = BadAI(wado$)
PRINT tmp$
PRINT " "
PRINT "YOU DIED: GAME OVER!"
PRINT "NO CONTINUES! HAHAHAHA!!!"
PRINT "YOU LOST, YOU LOST, C'MON, SMASH THAT KEYBOARD!"
WAIT KEY
END
EndFunction
Anyways, that's just my preference and I'm weirdo so I dunno how much my opinion counts. I personally find it messy and would personally advise beginners against too much of it (if at all) because I got myself into a mess. But our brains work differently so I don't for one minute deny that there are people who may prefer it. It was just my very humble opinion of what may make a beginner's life easier.
@Darkzombies:
Quote: "If it works, and you understand it. Its good code."
Well, maybe if you're the only one who sees your code and making bad codez doesn't hinder performance... But it's different if some poor other guy has to work with you or debug your code. Certainly, we can all agree, that reading someone else's code can be a nightmare if it is a mess, no matter how well
they understand it