Best practice for compute the function return value

前端 未结 5 2250
你的背包
你的背包 2020-11-29 09:57

Often I built functions, in C, that checks some parameters and return an error code.

Which is the best approach to stop the values checking when I found an error?

5条回答
  •  Happy的楠姐
    2020-11-29 10:22

    Funny nobody noticed, that the above 2nd example demonstrates, why the MISRA rule exists in the first place: it leaves out a default return value for all cases where the if clauses do not match.

    So what happens, if (foo == bar) && (foo1 == bar1) && (foo2 == bar2) ?

    Moreover, in the 1st example for me it's easier to grasp, in which special case there is a non-default return value.

提交回复
热议问题