variable passing / returning


Closed Thread
Results 1 to 6 of 6
  1. #1
    edwards_jt's Avatar
    edwards_jt Guest

    Default variable passing / returning

    Let me start out by saying thank you to all those people who respond to questions and pleas for help in the forum, it very valuable, and greatly appreciated.

    I have what I hope is a simple question. Is it possible to pass variables in PBP either by address or by value? and similarily can a subroutine be constructed that has a return type? I have searched and not found any inkling of syntax for this.


    thanks,

    James

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Variables in PBP are all "global". I.E. once a value is placed in a variable, it stays there until you change it or reset the PIC, and any sub-routine can access any variable.

    If you need to know the physical address of a variable, look in the .lst file after compile.

    Note the name given to a variable is actually the RAM address everything is stored in when the variable name is used.
    Last edited by Bruce; - 9th August 2004 at 16:15.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    edwards_jt's Avatar
    edwards_jt Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce, what i am getting at is writing a function that operates on a byte of data, when you want the same operation performed on 50 bytes, if you can pass a variable to a function what you end up with is one chunk of code executing 50 times with different parameters rather than 50 chunks of code each executing once.

    example:

    myVar as byte
    myVar2 as byte

    loop:
    gosub test(myVar)
    gosub test(myVar2)
    goto loop

    test(temp as byte):
    'operations on temp byte passed in
    return

    my other question was about return types and whether or not PBP can use returning functions so that you can have a procedure that is called to test some outside conditions and returns a byte.

    example:

    myVar as byte

    loop:
    myVar = gosub test
    goto loop

    test:
    myVar2 as byte
    'operations on myVar2
    return myVar2

    Even with all variables being global it might still be possible if you use placeholder bytes declared globally and used only in these types of routines. Have you ever seen syntax that would allow this?


    james

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    There are ways around everything... and the easiest to to have intermediate variables assigned for your subroutine... so, using your example...
    Code:
    myVar as byte
    myVar2 as byte
    
    loop:
    gosub test(myVar)
    gosub test(myVar2)
    goto loop
    what you would do is...
    Code:
    myVar as byte
    myVar2 as byte
    testVar as byte
    
    loop:
    testvar=myvar:gosub test
    testvar=myvar2:gosub test
    goto loop
    and your subroutine performs its functions only on the variables (testVar) allocated to it...

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi James,

    OK I think I see what you're getting at. Looks like you have some C experience.

    You want to pass data to a function (sub-routine) similar to how you would in C. Something like this;

    void convert(void)
    {
    temp_data = (temp_data * 9/5)+32; // convert C to F
    temp_r = toBCD(temp_data); // convert to BCD for display
    }

    char toBCD(char bin_val)
    {
    char temp;
    char retval;

    temp = bin_val;
    retval = 0;

    while(1)
    {
    // get tens digit by multiple subtraction
    // of 10 from bin_val
    if (temp >= 10)
    {
    temp -= 10;
    retval += 0x10; // increment tens digit
    }
    else // get ones digit by adding remainder
    {
    retval += temp; // adjusted result
    break;
    }
    }
    return(retval);
    }

    Unfortunately, you can't do it in PBP exactly like you would in C since there are no functions, arguments, etc like you have with C in BASIC.

    Melanies example would work with a slight modification. It's just a little more work in BASIC VS C.

    myVar as byte
    myVar2 as byte
    testVar as byte

    loop:
    testvar=myvar
    gosub test
    myvar=testvar

    testvar=myvar2
    gosub test
    myvar2=testvar

    goto loop
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    edwards_jt's Avatar
    edwards_jt Guest


    Did you find this post helpful? Yes | No

    Default

    couldn't see the forest for the trees.

    Thanks


    james

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Subroutine Variable Passing
    By GoldStar in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 27th May 2009, 07:55
  3. pbp - asm variable passing
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 25th May 2009, 08:49
  4. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  5. WORD vs BYTE variable lengths
    By bartman in forum General
    Replies: 0
    Last Post: - 28th November 2005, 21:16

Members who have read this thread : 1

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