Page 1 of 1

Sending a value to a function

Posted: 03 Apr 2012, 23:55
by mattallen37
I want a way where I can read or write a value from or to a function.

Here is a simple example:

Code: Select all

byte byte_array[20];

safecall bool Byte(unsigned long byte_index){
  return byte_array[byte_index];
}

task main(){
  byte x, y;
  y = (Byte(3) + Byte(16)) * Byte(12);
  x = Byte(7) * y;
  // at this point, x = (byte 3 + byte 16) * byte 12 * y
}
Now, I want to be able to do something like this:

Code: Select all

byte byte_array[20];

safecall bool Byte(unsigned long byte_index){
  return byte_array[byte_index];
}

task main(){
  Byte(2) = Byte(6) / Byte(8);    // How can I do this?
}
How can that be done?

Re: Sending a value to a function

Posted: 04 Apr 2012, 12:35
by afanofosc
A function in NXC cannot be an lvalue so it can't be assigned to like you propose. C++ allows this but only if the function result type is a reference.

Would it work for you to use a macro?

Code: Select all

#define Byte(_idx) byte_array[(_idx)]]
John Hansen

Re: Sending a value to a function

Posted: 04 Apr 2012, 15:44
by mattallen37
What do you mean by "a reference"?

That was only an example. In reality, I want to be able to use the technique on functions that are significantly more involved than that.

Re: Sending a value to a function

Posted: 04 Apr 2012, 16:05
by mattallen37
What about a way to have a macro call a function if it has been passed a value, or else return the result of a different function if the code is requesting a value?

Re: Sending a value to a function

Posted: 04 Apr 2012, 23:27
by afanofosc
I cannot think of any reason why you would want to do what you appear to want to do. What do you think it would buy you?

John Hansen

Re: Sending a value to a function

Posted: 04 Apr 2012, 23:38
by mattallen37
Hopefully the ability to have a byte array, and have each bit addressable. I want to be able to access (read or write) an individual bit, with something like Bit(n), where n is the bit I want to access.

In the mean-time, something like this should work:

Code: Select all

safecall bool Bit(unsigned long bit, byte value = 0xFF){
  if(value == 0xFF){
    return (bit_array[bit / 8] >> (bit % 8)) & 0x01;
  }
  else{
    value = value & 0x01;
    if(value){
      bit_array[bit / 8] |=  (1 << (bit % 8));
    }
    else{
      bit_array[bit / 8] &= ~(1 << (bit % 8));
    }
  }
  return 0;
}
Where I can read a bit using Bit(n), and I can write a bit using Bit(n, value), where n is the bit to read or write, and value is a 1 or 0 to write to the bit.

Re: Sending a value to a function

Posted: 05 Apr 2012, 15:13
by afanofosc
I think you would be better off with GetBit and SetBit. But I think there is really no reason to try to use bits when the smallest native type is a byte. You sacrifice speed for memory usage reduction. It will be much slower than dealing with bytes in a byte array. The only reason for writing bits in a byte array that makes sense to me is when you are dealing with LCD memory or an RIC sprite or some other sort of image format. Or you really need 8 times more boolean values than you can store with bytes in a byte array.

Also, using an unsigned long as the type for the index parameter is bad. There is no way you could address that many bits in the 32k of memory available to a running program. You'd be better off with unsigned int. That would use 8kb of memory and give you 65535 bits.

John Hansen

Re: Sending a value to a function

Posted: 05 Apr 2012, 16:16
by mattallen37
Actually, I did make GetBit and SetBit functions. I completely understand your point. The only reason for trying this is for the fun of it. I know I can easily do the same thing using two functions, or even my combined function with two abilities (the one I posed last).

I also thought about the unsigned long vs. unsigned int. I figured that for testing purposes, it wouldn't hurt to over-do the available range.

Here are the GetBit and SetBit functions:

Code: Select all

safecall bool GetBit(unsigned long bit){
  Acquire(bit_mutex);
  byte result = (bit_array[bit / 8] >> (bit % 8)) & 0x01;
  Release(bit_mutex);
  return result;
}

safecall void SetBit(unsigned long bit, bool value){
  Acquire(bit_mutex);
  if(value){
    bit_array[bit / 8] |= 1 << (bit % 8);
  }
  else{
    bit_array[bit / 8] &= ~(1 << (bit % 8));
  }
  Release(bit_mutex);
}
However, I would still like some way of passing a value to a function through the assign process (e.g. function() = 3 * 4), even if it doesn't apply to this situation.

Re: Sending a value to a function

Posted: 09 Apr 2012, 19:07
by afanofosc
I don't have any plans for adding support for functions that return reference types that can be used as lvalues.

John Hansen

Re: Sending a value to a function

Posted: 09 Apr 2012, 21:56
by mattallen37
Alright, thanks for the info.

I really didn't think anything like that existed, but I thought it would be worth asking.