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;
}
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;
}
...
}
This is kind of a toy program, just trying to practice some stuff. The question exceeds that fact. Maybe I should rephrase to "how to handle errors without (OOP) exceptions".
I'm also considering testing n
before doing the call, but don't like it as much.
Any thoughts?