• AVR Freaks

AnsweredHot!Unused returned value - reg.

Author
muralysunam
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2012/11/15 11:28:19
  • Location: 0
  • Status: offline
2020/07/11 13:55:55 (permalink)
0

Unused returned value - reg.

Hi all,
 
I was using two separate functions named SPI_transfer() and SPI_transfer_return(). First one have void return type and second one has uint8_t return type. I would like to remove the first function and use second function alone always.
 
Now
SPI_transfer('A');//sending a value
uint8_t rx_value = SPI_transfer_return(0);//reading a value
 
 
I would like to change to
 
SPI_transfer_return('A');//sending a value
uint8_t rx_value = SPI_transfer_return(0);//reading a value
 
Is this possible and is there any flaws?
 
I am having one more doubt regarding if, while etc...
if(!function()) // this is supposed to execute function and return value will be inverted and if positive, if will be executed
if(function() == 0) //same
Is there any problem if I use the first method? I am using XC8 free compiler.
 
 
Thanks for any help. 
#1
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11930
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Unused returned value - reg. 2020/07/11 14:02:52 (permalink)
+1 (1)
You can't separately read and write SPI, so having two functions makes no sense.
 
if(!function()) // this is supposed to execute function and return value will be inverted and if positive, if will be executed

 
Not if positive, if non-zero.
#2
muralysunam
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2012/11/15 11:28:19
  • Location: 0
  • Status: offline
Re: Unused returned value - reg. 2020/07/11 14:11:47 (permalink)
0
jtemples
You can't separately read and write SPI, so having two functions makes no sense




Sir,
Thank you for your response. Consider I am sending 3 bytes of data to a slave device and receiving one byte from slave device. So I don't need the response from the slave device for the first three bytes ie., I don't need that value. I would like to know what happens if I am not using that returned value in my program.
 
Thanking you,
#3
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11930
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Unused returned value - reg. 2020/07/11 14:17:45 (permalink) ☼ Best Answerby muralysunam 2020/07/11 14:45:29
+1 (1)
Consider I am sending 3 bytes of data to a slave device and receiving one byte from slave device.

 
That's not possible.  If you write 3 bytes, you will get 3 bytes back.  Maybe you only care about one of the bytes, but you will still get 3.
 
I would like to know what happens if I am not using that returned value in my program.

 
Nothing happens.  The compiler will know at compile time that you're not referencing it, and no code will be generated.
 
If you want to make it clear to the reader that you are intentionally discarding the return value, you can do
 
(void)spi_write_read(42);

#4
muralysunam
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2012/11/15 11:28:19
  • Location: 0
  • Status: offline
Re: Unused returned value - reg. 2020/07/11 14:45:59 (permalink)
+1 (1)
jtemples
Consider I am sending 3 bytes of data to a slave device and receiving one byte from slave device.

 
That's not possible.  If you write 3 bytes, you will get 3 bytes back.  Maybe you only care about one of the bytes, but you will still get 3.
 
I would like to know what happens if I am not using that returned value in my program.

 
Nothing happens.  The compiler will know at compile time that you're not referencing it, and no code will be generated.
 
If you want to make it clear to the reader that you are intentionally discarding the return value, you can do
 
(void)spi_write_read(42);



Thanks a lot.
#5
Jump to:
© 2020 APG vNext Commercial Version 4.5