Sending a value to a function

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Sending a value to a function

Post 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?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Sending a value to a function

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Sending a value to a function

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Sending a value to a function

Post 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?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Sending a value to a function

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Sending a value to a function

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Sending a value to a function

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Sending a value to a function

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Sending a value to a function

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Sending a value to a function

Post by mattallen37 »

Alright, thanks for the info.

I really didn't think anything like that existed, but I thought it would be worth asking.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest