Author Topic: No substitute for subs  (Read 4310 times)

0 Members and 1 Guest are viewing this topic.

gooner

  • Guest
No substitute for subs
« on: July 28, 2008 »
Hello everybody as you probably know i had some problems with sub routines.Mainly where and when to put them in.I've just done a simple routine and would appreciate any advice as to where i've put my sub routines and whether i needed to do so many and any other shortcuts i could have used in this code

Code: [Select]
' NO SUBSTITUTE
'
OPTION EXPLICIT

    SCREEN 19,,2,
    SCREENSET 1,0
DIM SHARED as INTEGER X,Y,B,B1,B2,B3,B4,B5,X1,XX,XXG,YYG,YY,Y1,X2,X3,X4,X5,X6,X7,X8,Y3,Y4,Y2
DIM SHARED AS DOUBLE A,C,D,E,F,G'ADD MOVEMENTS FOR CIRCLE

   
   
   
   
    X1=10'RECT 1
    X=300'BLUE CIRCLE
    XX=120'RED CIRCLE
    YY=120'RED CIRCLE
    XXG=480'WHITE CIRCLE
    YYG=400'WHITE CIRCLE
    Y=80'BLUE CIRCLE
    B=0'HORIZ MOVEMENT BLUE
    B1=0'VERT MOVENENT BLUE
    B2=0'HORIZ MOVE RED
    B3=0'VERT MOVE RED
    B4=0'HORIZ MOVE WHITE
    B5=0'VERTICAL MOVE WHITE
    X2=10'RECT1
    X3=30'RECT1
    Y1=20'RECT1
    Y2=580'RECT1
    Y3=40'TOP REC
    Y4=560'BOT REC
    X4=760'RECT2
    X5=780'RECT 2
    X6=60'TOP REC
    X7=700'TOP&BOTT REC
     DECLARE SUB DOUBLE_BUFFER()
     DECLARE SUB MOVE_CIRCLE()
    DECLARE SUB DRAW_RECT1()
    DECLARE SUB DRAW_RECT2()
    DECLARE SUB DRAW_RECT3()
    DECLARE SUB DRAW_RECT4()
    DECLARE SUB DRAW_CIRCLE()
   
   
    FOR A= 0 TO 1
    NEXT
    FOR C=0 TO 1.7'''''MOVEMENT FOR CIRCLES ?
    NEXT
    FOR D=0 TO 2'
    NEXT
    FOR E=0 TO 3
    NEXT
    FOR G=0 TO 2.7
        NEXT
SUB MOVE_CIRCLE()
   IF B=0 THEN X=X+A
   IF X>X4-39 THEN B=1
   IF B=1 THEN X=X-D
   IF X<X1+63 THEN B=0
   
    IF B1=0 THEN Y=Y+A
    IF Y>Y4-39 THEN B1=1
    IF B1=1 THEN Y=Y-G
    IF Y<Y1+60 THEN B1=0
   
    IF B2=0 THEN XX=XX+C
    IF XX>X4-39 THEN B2=1
    IF B2=1 THEN XX=XX-E
    IF XX<X1+63 THEN B2=0
   
    IF B3=0 THEN YY=YY+D
    IF YY>Y4-39 THEN B3=1
    IF B3=1 THEN YY=YY-A
    IF YY<Y1+60 THEN B3=0
   
   IF B4=0 THEN XXG=XXG+E
   IF XXG>X4-39 THEN B4=1
   IF B4=1 THEN XXG=XXG-D
   IF XXG<X1+63 THEN B4=0
   
   IF B5=0 THEN YYG=YYG+G
   IF YYG>Y4-39 THEN B5=1
   IF B5=1 THEN YYG=YYG-A
   IF YYG<Y1+60 THEN b5=0

 END SUB

DO
    DOUBLE_BUFFER()
    DRAW_RECT1()
    DRAW_RECT2()
    DRAW_RECT3()
    DRAW_RECT4()
    DRAW_CIRCLE()
    MOVE_CIRCLE()
   

LOOP UNTIL INKEY$<>""
END
 
'-------------------------------------------------------------------------------
SUB DOUBLE_BUFFER()

    SCREENCOPY
    CLS
    SLEEP 1
END SUB
SUB DRAW_RECT1()

    line(X1,Y1)-(X2,Y2)
    LINE(X1,Y1)-(X3,Y1)
    LINE (X3,y1)-(X3,Y2)
    LINE(X3,Y2)-(X2,Y2)

END SUB

SUB DRAW_RECT2()
    LINE (X4,Y1)-(X4,Y2)
    LINE (X4,y1)-(X5,y1)
    LINE (X5,Y1)-(X5,y2)
    LINE(X5,Y2)-(X4,y2)
END SUB
SUB DRAW_RECT3()
    LINE (X6,Y1)-(X7,Y1)
    LINE (X6,Y1)-(X6,Y3)
    LINE (X6,Y3)-(X7,Y3)
    LINE (X7,Y3)-(X7,Y1)
END SUB
SUB DRAW_RECT4()
    LINE (X6,Y4)-(X7,Y4)
    LINE(X6,Y4)-(X6,Y2)
    LINE(X6,Y2)-(X7,Y2)
    LINE(X7,Y2)-(X7,Y4)
END SUB
SUB DRAW_CIRCLE()
    CIRCLE(X,Y),40,1,,,1,F
    CIRCLE(XX,YY),40,4,,,1,F
    CIRCLE(XXG,YYG),40,15,,,1,F
END SUB

Thanks Gooner
 :)
« Last Edit: September 20, 2009 by Shockwave »

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: No substitute for subs
« Reply #1 on: July 28, 2008 »
Code: [Select]
SUB DRAW_RECT2()
    LINE (X4,Y1)-(X4,Y2)
    LINE (X4,y1)-(X5,y1)
    LINE (X5,Y1)-(X5,y2)
    LINE(X5,Y2)-(X4,y2)
END SUB
SUB DRAW_RECT3()
    LINE (X6,Y1)-(X7,Y1)
    LINE (X6,Y1)-(X6,Y3)
    LINE (X6,Y3)-(X7,Y3)
    LINE (X7,Y3)-(X7,Y1)
END SUB
SUB DRAW_RECT4()
    LINE (X6,Y4)-(X7,Y4)
    LINE(X6,Y4)-(X6,Y2)
    LINE(X6,Y2)-(X7,Y2)
    LINE(X7,Y2)-(X7,Y4)
END SUB

This is not really the best way of approaching the problem, you see sub-routines accept parameters so you would be better off with a sub that is called like this
Code: [Select]
rect(x1,y1,x2,y2)

The declaration for the sub should look something like this (someone better versed in FB make corrections if needed)
Code: [Select]
Declare sub as integer rect(by_val as integer A, by_val as integer B, by_val as integer C, by_val as integer D)
and the sub itself should look like this (Note that the parameters are local variables and are invisible outside this sub)
Code: [Select]
SUB DRAW_RECT(A,B,C,D)
    LINE (A,B)-(C,B)
    LINE(C,B)-(C,D)
    LINE(C,D)-(A,D)
    LINE(A,D)-(A,B)
END SUB
« Last Edit: July 28, 2008 by rain_storm »

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: No substitute for subs
« Reply #2 on: July 28, 2008 »
What you'd generally do Is first Declare your subroutines and / or Functions first at the top of you program where you have your variables.

Also what I would do if I were you is make a SUBroutine something like this: DrawRect( ByVal StartX As Integer, ByVal StartY As Integer, ByVal EndX As Integer, ByVal EndY As Integer )

You'd Declare it first with Declare Sub DrawRect( ByVal StartX As Integer, ByVal StartY As Integer, ByVal EndX As Integer, ByVal EndY As Integer )

Then this would save you on creating lots of duplicate rectangle subs. As what you'd do in your main program loop is just call DrawRect(value1,value2,value3,value4) Value1...4 being the info for drawing your rectangle.
 
And The difference between a sub and a function is that with a function you can return a result.

I hope this somewhat helps you dude in your quest. if not please repost. And Im sure one of us will help you out mate

[Sorry Rain didnt notice your post at the same time as mine]

Cheers,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: No substitute for subs
« Reply #3 on: July 28, 2008 »
With respect Rain Storm, Gooner is learning about the *positioning* of the subs, your reply *is* going to confuse him.

BTW the syntax you used in your sub definitions is wrong too Rain, sorry :(

I havent shown him how to pass parameters between the different parts of the program yet.

You are totally correct of course, but I don't think it's a good idea for him to be worrying about passing variables between different subs at the moment. I need to have an hour with him to explain how it works properly.

There are a couple of things in the code that need to be pointed out better though, I'll write it up in a moment.

The code is pretty clean though, well done Gooner!
« Last Edit: July 28, 2008 by Shockwave »
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: No substitute for subs
« Reply #4 on: July 28, 2008 »
Ever so slightly changed your code.

Code: [Select]
' NO SUBSTITUTE
'
OPTION EXPLICIT

    SCREEN 19,,2,
    SCREENSET 1,0
DIM SHARED as INTEGER X,Y,B,B1,B2,B3,B4,B5,X1,XX,XXG,YYG,YY,Y1,X2,X3,X4,X5,X6,X7,X8,Y3,Y4,Y2
DIM SHARED AS DOUBLE A,C,D,E,F,G'ADD MOVEMENTS FOR CIRCLE

   
   
   
   
    X1=10'RECT 1
    X=300'BLUE CIRCLE
    XX=120'RED CIRCLE
    YY=120'RED CIRCLE
    XXG=480'WHITE CIRCLE
    YYG=400'WHITE CIRCLE
    Y=80'BLUE CIRCLE
    B=0'HORIZ MOVEMENT BLUE
    B1=0'VERT MOVENENT BLUE
    B2=0'HORIZ MOVE RED
    B3=0'VERT MOVE RED
    B4=0'HORIZ MOVE WHITE
    B5=0'VERTICAL MOVE WHITE
    X2=10'RECT1
    X3=30'RECT1
    Y1=20'RECT1
    Y2=580'RECT1
    Y3=40'TOP REC
    Y4=560'BOT REC
    X4=760'RECT2
    X5=780'RECT 2
    X6=60'TOP REC
    X7=700'TOP&BOTT REC
     DECLARE SUB DOUBLE_BUFFER()
     DECLARE SUB MOVE_CIRCLE()
    DECLARE SUB DRAW_RECT1()
    DECLARE SUB DRAW_RECT2()
    DECLARE SUB DRAW_RECT3()
    DECLARE SUB DRAW_RECT4()
    DECLARE SUB DRAW_CIRCLE()

'    THESE LOOPS ARE NOT NEEDED;
'   
'    FOR A= 0 TO 1
'    NEXT
'    FOR C=0 TO 1.7'''''MOVEMENT FOR CIRCLES ?
'    NEXT
'    FOR D=0 TO 2'
'    NEXT
'    FOR E=0 TO 3
'    NEXT
'    FOR G=0 TO 2.7
'    NEXT

'   DO IT LIKE THIS INSTEAD (THESE ARE THE VALUES THAT THE VARIALBES WOULD HAVE AT THE END OF THE LOOPS);

    G=2.7
    E=3
    D=2
    C=1.7
    A=1
' MOVED THE SUB "MOVE_CIRCLE" UNDER THE OTHER SUBS FOR NEATNESS.   

DO
    DOUBLE_BUFFER()
    DRAW_RECT1()
    DRAW_RECT2()
    DRAW_RECT3()
    DRAW_RECT4()
    DRAW_CIRCLE()
    MOVE_CIRCLE()
   

LOOP UNTIL INKEY$<>""
END
 
'-------------------------------------------------------------------------------

SUB MOVE_CIRCLE()
   IF B=0 THEN X=X+A
   IF X>X4-39 THEN B=1
   IF B=1 THEN X=X-D
   IF X<X1+63 THEN B=0
   
    IF B1=0 THEN Y=Y+A
    IF Y>Y4-39 THEN B1=1
    IF B1=1 THEN Y=Y-G
    IF Y<Y1+60 THEN B1=0
   
    IF B2=0 THEN XX=XX+C
    IF XX>X4-39 THEN B2=1
    IF B2=1 THEN XX=XX-E
    IF XX<X1+63 THEN B2=0
   
    IF B3=0 THEN YY=YY+D
    IF YY>Y4-39 THEN B3=1
    IF B3=1 THEN YY=YY-A
    IF YY<Y1+60 THEN B3=0
   
   IF B4=0 THEN XXG=XXG+E
   IF XXG>X4-39 THEN B4=1
   IF B4=1 THEN XXG=XXG-D
   IF XXG<X1+63 THEN B4=0
   
   IF B5=0 THEN YYG=YYG+G
   IF YYG>Y4-39 THEN B5=1
   IF B5=1 THEN YYG=YYG-A
   IF YYG<Y1+60 THEN b5=0

 END SUB


SUB DOUBLE_BUFFER()

    SCREENCOPY
    CLS
    SLEEP 1
END SUB
SUB DRAW_RECT1()

    line(X1,Y1)-(X2,Y2)
    LINE(X1,Y1)-(X3,Y1)
    LINE (X3,y1)-(X3,Y2)
    LINE(X3,Y2)-(X2,Y2)

END SUB

SUB DRAW_RECT2()
    LINE (X4,Y1)-(X4,Y2)
    LINE (X4,y1)-(X5,y1)
    LINE (X5,Y1)-(X5,y2)
    LINE(X5,Y2)-(X4,y2)
END SUB
SUB DRAW_RECT3()
    LINE (X6,Y1)-(X7,Y1)
    LINE (X6,Y1)-(X6,Y3)
    LINE (X6,Y3)-(X7,Y3)
    LINE (X7,Y3)-(X7,Y1)
END SUB
SUB DRAW_RECT4()
    LINE (X6,Y4)-(X7,Y4)
    LINE(X6,Y4)-(X6,Y2)
    LINE(X6,Y2)-(X7,Y2)
    LINE(X7,Y2)-(X7,Y4)
END SUB
SUB DRAW_CIRCLE()
    CIRCLE(X,Y),40,1,,,1,F
    CIRCLE(XX,YY),40,4,,,1,F
    CIRCLE(XXG,YYG),40,15,,,1,F
END SUB

Well done for making the program display something that has a little bit of design too, K+

See the listing above for my comments.

Other comments?

You didn't put any comments in your listing, thats a really bad habit to get into. You should add descriptions to your listing.

Code is nicely laid out though.
Shockwave ^ Codigos
Challenge Trophies Won:

gooner

  • Guest
Re: No substitute for subs
« Reply #5 on: July 28, 2008 »
Thanks to Rain_storm Clyde and Shockwave for your quick responce.I'm sure once i've learnt about Byvals etc this will become clearer to me but at the moment its left me reaching for the help index in FB and still not really understanding it.Thank you for taking the time to help me this will be something i can refer back to
 :)

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: No substitute for subs
« Reply #6 on: July 28, 2008 »
No probs one thing at a time,

I try to keep the subroutines that will not be edited away from the code that Im working on at the moment and also keep the subroutines that may need editing as close to the code that will be calling them as you can. Saves a little time looking for them especially when the code is becoming very large

For example the DOUBLE_BUFFER() sub probably wont be needing any changes so it may as wel go to the very bottom of the code.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: No substitute for subs
« Reply #7 on: July 28, 2008 »
By all means, read up on byval, but you really, really, really , really , really , really , really , really  don't need it at the moment. Concentrate on the problem that is confusing you, make sure we answer your questions satisfactorally so you fully understand before you move on :)

Shockwave ^ Codigos
Challenge Trophies Won:

gooner

  • Guest
Re: No substitute for subs
« Reply #8 on: July 28, 2008 »
@Shock
Sorry mate i was posting the same time as you.
Thank you for the karma it will be wisely spent.
Basically i just done this because i needed to be sure i was putting the subs in the right 
place so thanks for altering the code for us.
You didn't put any comments in your listing, thats a really bad habit to get into. You should add descriptions to your listing.
Sorry mate this is gunner sound like a really stupid question but can you give me an example.
 :D

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: No substitute for subs
« Reply #9 on: July 28, 2008 »
Sure.

Use the ;

'

or the

rem

command.

anything after ' or rem is ignored by the computer so you can see what the program is meant to do when you look throught the listing :)
Shockwave ^ Codigos
Challenge Trophies Won:

gooner

  • Guest
Re: No substitute for subs
« Reply #10 on: July 28, 2008 »
Cool i knew about the rem command it was what i put in the listings i was after.got it now
 :kewl:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: No substitute for subs
« Reply #11 on: July 28, 2008 »
ive been keeping an eye on your progress gooner and i really must say well done! and although all this might seem a little confusing youll be surprised by how quickly you will learn, just keep chipping away!
Challenge Trophies Won:

gooner

  • Guest
Re: No substitute for subs
« Reply #12 on: July 29, 2008 »
Thanks for them kind words ninogenio it means a lot.
At the moment its a bit like when i was learning to drive ie thinking about every gear change.So eventually by keep plugging away and practising everything will come naturally.I'm now getting to the stage where i'm not frightened to ask what might seem
really stupid questions and the feedback i've had from you guys has been brilliant.
 :)