Izzo
Izzo

Reputation: 4928

Why does C let you ignore returned values from functions?

Let's say I have the following C code:

int return_one()
{
    return 1;
}

int main(void)
{
    return_one();
}

Within the main function, I call function return_one() and ignore the return value. The compiler has no issue with me ignoring this value.

What is the logic as to why this okay? Was it an arbitrary design choice from the C creators? Or is there a practical reason for not requiring the calling function to use the return value?

Upvotes: 0

Views: 766

Answers (4)

Dimonium Anonimo
Dimonium Anonimo

Reputation: 1

I've actually stumbled upon this post while researching to see if I could ignore the returned value of a function and if so, how to set it up. Here's the reason:

I'm working in SPI communications. The master needs to send a command to the slave. It does so by storing the command in a register, ready to send. The master does this by initiating the same sequence of events, except we don't need to place any specific data in the register this time. The clock pulses a few times, and when it's done, the contents of the master's register are now in the slave's register where the slave can read them and interpret/enact the command. But the contents that used to be in the slave's register are now in the master's register. I don't really care what the slave sent me, it's probably garbage anyway since I haven't even told it to do anything yet. (Well, I just did, but it can't have implemented it in the data it just sent)

So the command I sent was actually a request to read some data from a specific memory address in the slave. The slave interprets the command, fetches the data in question, and stores it in the register, waiting for the master to initiate that transfer. Then the clock pulses a few times, and the registers have swapped again. This time, I do care what comes back.

So I have 2 options: either write 2 functions that do the exact same thing except one returns the final value in the register, while the other ignores it. Or I can write the exact same function, and ignore the returned value upstream.

In the embedded world, there are a lot of decisions that feel arbitrary, but I bet all of them served a very specific purpose at some point in time. And many still do.

Upvotes: -1

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

I think the main reason is the usual one — history.

Before the C standard, there was no option to use void to indicate 'no return value'. Functions returned an int unless you specified that they returned some other type, but that other type couldn't be void (it didn't exist). So functions for which the return value was immaterial didn't return a value — even though the function was implicitly returning type int. (Usually, the return type was omitted — the function was implicitly returning an int.) You got UB if the calling code tried to use a value but the called function didn't return a value.

All this meant that it was commonplace to ignore the return value of functions — especially functions that nominally returned an int but actually didn't return any value. There wasn't a better way of dealing with it. Nowadays, with void return types, there are better ways to deal with it. Nevertheless, it remains true that the return value is often of limited interest. How often do you check the return value of printf() or one of its friends? How often do you use the return value of strcpy() et al?

C90 had to allow old code to run still, so it allowed the old, pre-standard behaviour. C99 tightened the rules — functions were no longer implicitly of type int and had to be declared (with an explicit return type, possibly void) before they could be used.

Upvotes: 3

sleske
sleske

Reputation: 83609

tl;dr: You can write code such that the return values are always relevant - in that case ignoring them would be a bug. However, some (particularly older) code does not work that way, there ignoring return values may be reasonable.

What is the logic as to why this okay? Was it an arbitrary design choice from the C creators?

It is not just a choice by the C creators, many other languages also allow this - as a matter of fact, I cannot think of a language where ignoring the return value is considered an error.

The practical motivation for this is mainly that the return value of a function is often used for reporting errors, or reporting details of what the function did. Sometimes, you are not interested in these details, so you ignore them. A common example of this is the C function printf, which returns the number of characters printed. While this may sometimes be useful, it is usually not, so the return value of printf is usually ignored.

This is arguably a bad design (either in your code, or in the function that returns stuff noone wants), but it is established practice, so C (like other languages) supports this.

Or is there a practical reason for not requiring the calling function to use the return value?

No, there is no practical reason for ignoring return values - unless you do not want them.


While the above is the historical practice, many people now consider ignoring return values a problem (or a symptom of a deeper problem):

  • If the return value is for error reporting, ignoring it will work usually, but cause problems if there really is an error. This is now usually considered a bad idea.
  • Generally, if you can ignore the return value, that means the function is causing side-effects (otherwise calling it would be pointless). Many people think that it is better to have functions only do one thing - either cause a side effect (and return nothing), or return a value (and have no side effects). The latter is often called a pure function (with the additional condition of the output only depending on the input). This separation makes it easier to understand software - and if you use it, ignoring a return value is necessarily a mistake, because functions returning a result do nothing else, so if you ignore the result, calling it is pointless.

So in other words: If you follow certain conventions, there should be no situation where you want to ignore return values, in that case ignoring them is usually a mistake. Without these conventions, there may be good reasons for ignoring them, so there's no general rule.

Upvotes: 0

ShadowRanger
ShadowRanger

Reputation: 155363

Because people don't care about a lot of return values, so why force them to use them?

A ton of standard C functions return values that are essentially never looked at. Think about all the C code you've ever looked at. Have you ever seen someone do anything with the return value from the printf family of functions? Because they all have one, but you'd never know it to look at real world code. Would it be improved if every single call to printf had to prefix it with an explicit "I don't care about the return value" bit of syntax (e.g. (void)), because 99.99% of the time, you don't actually care how many bytes you printed, but printf computes and returns it anyway? Basically, you're allowed to not use the return value because there's was no need to force you to do so, and it's often not needed.

Upvotes: 3

Related Questions