Hi everyone,
I have a simple design(?) question.
I'm writing a simple program, that has a couple of functions that look like these.
float foo (float* m,size_t n){
float result;
//do some calculations, for example a sum
return result / n;
}
I have a couple of questions on this, with no intention of re opening some holy war.
Should I add a sanity check on the n
? If so, how should I let the caller know?
Returning -1
looks weird on floats;
float foo(float *m,size_t n){
if (n == 0) return -1f
...
}
My other option is an out parameter
float foo(float *m,size_t n, int *error){
if (n==0){
*error = 1;
return 0f;
}
...
}
update
This is kind of a toy program, just trying to practice some stuff. The question excedes that fact. Maybe I should rephrase to "how to handle errors without (OOP) exceptions".
Also considering testing n
before doing the call, but don't like it as much.
Any thoughts? Thanks in advance.