[chbot] return value or status from function

Robin Gilks robin at gilks.org
Fri Feb 5 11:37:34 GMT 2016


> Good day all,
>  What is the opinion of this esteemed group on the best/proper/whatever
> way
> to get the measured value from some external input from a micro in C.
> By measured value I mean something like ADC, OW temp sensor, I2C sensor
> state etc.
>  I am considering the following three scenarios in increasing order of
> goodness:
> 1) function returns the measured value indicating error by out of range
> result (i.e 0xFF for  8 bit int)
> i.e:
> ...
> unit8_t readSomePin(void); // 0xFF indicates error
>
> 2) function returns the measured value indicating status of measurement in
> another extern. variable
> ...
> extern uint8_t readSomePinStatus;
> unit8_t readSomePin(void);  // Status in readSomePinStatus
>
> 3) function returns status, the value is stored in an argument variable:
> ...
> unint8_t readSomePin(uint8_t *returnedValue);
>
> So, what is best? Is there a better way? This could no doubt also be done
> with Structs, but seems like overkill to me.

I'd go for option 3 as the function prototype provides all the information
about the size and sign of the parameter as well as the function itself
giving the result of success or failure which will make the code more
maintainable.

-- 
Robin Gilks





More information about the Chchrobotics mailing list