When designing a new function, developers often need a mechanism to indicate that the function can't accomplish its work because of some kind of error. It might be invalid, an unexpected result being received from a peripheral device, or a resource allocation issue.
One of the most traditional and widespread ways to report an error condition is through error codes. This is an efficient and ubiquitous mechanism that does not depend on the programming language or the operating system. Due to its efficiency, versatility, and ability to cross various platform boundaries, it is highly used in embedded software development.
Designing a function interface that returns either a value or an error code may be tricky, especially if the value and the error code have different types. In this recipe, we will explore several approaches to designing...