variables and gosubs???


Closed Thread
Results 1 to 39 of 39

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    14

    Default variables and gosubs???

    Hi guys,

    i've never used varibles in pbp before, so im trying to do 2 things here:


    what i want to do is this:

    Code:
    main:
    
    i = 0
    while(i < 10){
    
    Variable = i
    
    
    gosub ("subName" + Variable)
    
    i = i  +1
    
    }
    
    goto main
    
    
    subName1:
    
    return
    
    
    
    subName2:
    
    return
    
    
    subName3:
    
    return
    
    
    
    subName4:
    
    return

    its a bit of a mix of picbasic, and php, but im sure someone will get what im trying to do

    Is it possible?

    Thanks very much.

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


    Did you find this post helpful? Yes | No

    Default

    Hi guest_05,

    You can use the BRANCH or BRANCHL command for that.

    Something like this...
    Code:
    i        VAR BYTE
    Variable VAR BYTE
    
    main:
        i = 0
        while(i < 5)
            Variable = i
            gosub SubSwitcher
            i = i  +1
        wend
    goto main
    
    
    SubSwitcher:
        BRANCHL  variable,[subName0, subName1, subName2, subName3, subName4]
    return
    
    
    subName0:
    
    return
    
    
    subName1:
    
    return
    
    
    subName2:
    
    return
    
    
    subName3:
    
    return
    
    
    subName4:
    
    return
    DT

  3. #3
    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

  4. #4
    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.

  5. #5
    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

  6. #6
    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.

  7. #7
    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

  8. #8
    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

  9. #9
    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

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