PDA

View Full Version : variable passing / returning



edwards_jt
- 8th August 2004, 22:31
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

Bruce
- 9th August 2004, 15:25
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.

edwards_jt
- 9th August 2004, 17:12
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

Melanie
- 9th August 2004, 17:25
There are ways around everything... and the easiest to to have intermediate variables assigned for your subroutine... so, using your example...


myVar as byte
myVar2 as byte

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

what you would do is...


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

Bruce
- 9th August 2004, 18:29
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

edwards_jt
- 10th August 2004, 06:04
:) couldn't see the forest for the trees.

Thanks


james