variables and gosubs???


Closed Thread
Results 1 to 39 of 39

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    DT, you are my teacher and I learn a lot from you here. With all my respect to you, please allow me to make a point;
    BRANCHL will cause the program to jump. Thus, "return" with a "subroutine" will not work.

    I made my point as below.


    Code:
    i   VAR BYTE
    
    main:
        FOR i = 0 to 4
            BRANCHL  i,[subName0, subName1, subName2, subName3, subName4]
            Sayzers_Point:  'continue from where we were.
        NEXT i
    GOTO main
    
    
    subName0:
    
    GOTO Sayzers_Point
    
    
    subName1:
    
    GOTO Sayzers_Point
    
    
    subName2:
    
    GOTO Sayzers_Point
    
    
    subName3:
    
    GOTO Sayzers_Point
    
    
    subName4:
    
    goto Sayzers_Point

    or another example could be as below.

    Code:
    i   VAR BYTE
    
    main:
    
        FOR i = 0 to 4
    
            SELECT CASE i
    
               CASE 0
                    'do something
               CASE 1
                    'do something else
               CASE 2
                    'do something more
               CASE 3
                    'do something interesting
               CASE 4
                    'do something bro
             END SELECT
    
        NEXT i
    
    GOTO main


    ---------------------------------
    Last edited by sayzer; - 18th November 2006 at 18:10.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Nope..it will perfectly work dude and what Darrel did it's really clever.

    When you'll meet the Return, it will automatically RETURN at the main... but not at SubSwitcher.

    It's a kind of On YourVar Gosub.

    Try it...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    That's why the GOSUB was placed inside the while loop.

    That gosub calls the SubSwitcher: which then redirects to the appropriate subroutine. Then the subroutine does the return.

    The return in the SubSwitcher: is only there in case the variable is larger than the number of available subroutines. Otherwise, it would run subName0 which would be in error.

    The subroutines don't return to the SubSwitcher, they return back to the while loop.

    HTH,
    DT

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    How dare you reply exactly at the same time
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    That seems to happen alot with us Steve.

    I should also note here, that Sayzer's examples will work just fine too.

    Just different ways to skin a dog.
    (sorry, I'm a cat person)
    <br>
    DT

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    here we say... every road leads to Roma.

    Why? i don't know... but why skin a cat as well
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Since I am still learning,

    with the information above, will the "return" go back to FOR loop in the example below ?
    I guess it will, but I also do not like to guess.

    Code:
    i   VAR BYTE
    
    main:
    
        FOR i = 0 TO 4
            BRANCHL i,[subName0, subName1, subName2, subName3, subName4]
        NEXT i
    
    GOTO main
    
    
    subName0:
    
    RETURN
    
    
    subName1:
    
    RETURN
    
    
    subName2:
    
    RETURN
    
    
    subName3:
    
    RETURN
    
    
    subName4:
    
    RETURN


    mister_e, you are also my teacher as you know!
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    In your example the return won't work as Branch(l) use Goto. It's just a Select CAse goto.. or kind of

    I wonder why Branch(l) don't use gosub so far... easy to modify the macro but...
    Last edited by mister_e; - 18th November 2006 at 18:34.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e
    In your example the return won't work as Branch(l) use Goto. It's just a Select CAse goto.. or kind of

    I wonder why Branch(l) don't use gosub so far... easy to modify the macro but...

    Did you mean the example in post #9 ?


    -----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  10. #10
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor
    ...

    The subroutines don't return to the SubSwitcher, they return back to the while loop.

    ...

    If there is no typo here, did I misunderstand this statement then ?



    -----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  11. #11
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Question

    I'm a little confused as well. I guess as a test, you can put a statement under the branch instruction to flash an LED or something to see what happens. The way I read it is it will return back to the while loop because there is nothing else in the SubSwitcher routine to execute after the branch statement, just another return back to the while loop.
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

  12. #12
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Ok guys, sit back, take a breath, give me a little while to come up with a better answer.

    All will be revealed. Or further confused. We'll see.
    DT

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I'll try something...

    Code:
    StartPoint:
        Gosub Somewhere
    Label1:
        Pause 100
        Goto Start
    Somewhere:
        Pause 1
        Return
    Nothing hard here right? after the Return... you return to Label1 ... right?

    Now...
    Code:
    StartPoint:
        Gosub Somewhere
    Label1:
        Pause 100
        Goto Start
    Somewhere:
        Goto SomewhereElse
        z: goto z    
    
    SomewhereElse:
        Return
    Same thing happen here...

    This said...

    Code:
    Start:                         
          for routines=0 to 3
    StartPoint:
              gosub subselect
    ReturnPoint:
              pause 200
              next
        goto start
    
    SubSelect:
              branchl routines, [Routine0, Routine1, Routine2, Routine3]    
              
    Routine0:
             return
    Where the Return of Routine0 leads you?... to ReturnPoint

    I know i'm a pretty bad teacher...
    Last edited by mister_e; - 18th November 2006 at 20:02.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    and I am a good student.


    I thought that once the program jumps, then it will forget where it was and will return to an unknown place. So that it will never be 100% precise.


    ---------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts