Why does a function accept a reference instead of returning a value?

Asked

Viewed 58 times

4

Consider the code snippet below:

int num;
printf("Enter a number: ");
scanf("%d", &num);

I understand that by passing &num to the second argument of scanf(), step the variable reference num, which will be modified by the implementation of scanf().

However, I am in doubt behind the motivation of this type of API. Wouldn’t it be more prone to errors (passing for example a missing reference or one that has already been released)? Why not simply return the entered value of scanf()?

Surely there must be a motivation for this choice. What is it? It has to do with the (probable) impossibility of inferring the type of return?

  • 3

    Why not simply return the scanf typed value? - because you can read several values at once: scanf("%d %s %f", &inteiro, arraydechar, &floatingpoint) - and the return is the amount of items that have been read.

1 answer

4


General reasons:

  • it is more interesting to change an existing storage location than to create a new one, the traditional return performs a copy
  • you need to return more than one value, which is complicated, not economical in some senses, so "return" through a reference is the solution.

You may want to return a number of different values, this is one of the reasons that the scanf() allows this, but it can also be two values and specific form, ie the value itself you want, and information whether it worked or not, or how it worked.

Passing a reference is more common than it seems, in many languages it happens a lot without the person seeing that it is a reference (in object orientation it is always so). So the API is not as out of the ordinary as people might think. In C has always been more gross, it shows you.

If C’s philosophy could pass an object and have all information directly on it, it would be a passage by reference, perhaps implicitly and you would not be "complaining":) But C likes to be explicit, then you need to be able to inform the consumer how many items were read correctly (can be 0), and deliver all the amounts that the consumer said they could receive.

Nothing to do with inference of the type of return, including why it does not occur. Actually specifically with the scanf() has the complication that variables can be of different types. If the compiler is not good, it would be equal dynamic typing languages. Is that today the compilers read the string formatting and trying to bar some error, but it is a special behavior. You have to define the type of all variables anyway, it’s not like some people think it depends on what type the variable changes.

If you wanted to do a function that only reads a data and is guaranteed to work and do not need to report anything else, or consider that a null can already report a problem, then could return a value. But there would be another problem, there should be one scanf_int(), one scanf_str(), one scanf_double(), etc..

The motivation is more general, but the scanf() has more specific motivation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.