Kevin
Kevin

Reputation: 2050

Why use int functions over void?

I was looking over some example functions and methods (I'm currently in a C++ class), and I noticed that there were a few functions that, rather than being void, they were something like

int myFunction() {
   // ...;
   return 0;
}

Where the ellipses is obviously some other statement. Why are they returning zero? What's the point of returning a specific value every time you run a function?

I understand that main() has to be int (at least according to the standards) because it is related (or is?) the exit code and thus works with the operating system. However, I can't think of a reason a non-main function would do this.

Is there any particular reason why someone might want to do this, as opposed to simply making a void function?

Upvotes: 3

Views: 934

Answers (7)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

If that's really what they're doing, returning 0 regardless of what the function does, then it's entirely pointless and they shouldn't be doing it.

In the C world, an int return type is a convention so that you can return your own "error code", but not only is this not idiomatic C++ but if, again, your programmer is always returning 0, then it's entirely silly.

Specifically:

I understand that main() has to be int (at least according to the standards) because it is related (or is?) the exit code and thus works with the operating system. However, I can't think of a reason a non-main function would do this.

I agree.

Upvotes: 8

itsols
itsols

Reputation: 5582

In certain programming languages you find procedures and functions. In C, C++ and similar languages you don't. Rather you only have functions.

In practice, a procedure is a part of a program that performs a certain task. A function on the other hand is like a procedure but the function can return an answer back.

Since C++ has only functions, how would you create a procedure? That's when you would either create a void function or return any value you like to show that the task is complete. It doesn't have to be 0. You can even return a character if you like to.

Take for example, the cout statement. It just outputs something but not return anything. This works like a procedure.

Now consider a math function like tan(x). It is meant to use x and return an answer back to the program that called it. In this case, you cannot return just anything. You must return the value of the TAN operation.

So if you need to write your own functions, you must return a value based on what you're doing. If there's nothing to return, you may just write a void function or return a dummy value like 0 or anything else.

In practice though, it's common to find functions returning 0 to indicate that 'all went off well' but this is not necessarily a rule.

here's an example of a function I would write, which returns a value:

float Area ( int radius) 
{
float Answer = 3.14159 * radius * radius;
return Answer;

}

This takes the radius as a parameter and returns the calculated answer (area). In this case you cannot just say return 0.

I hope this is clear.

Upvotes: 0

iavr
iavr

Reputation: 7637

One more issue that was not touched by other answers. Within the ellipses may be another return statement:

int myFunction() {
   // ...;
   if (error)
       return code;
   // ...;
   return 0;
}

in which case myFunction is not always returning 0, but rather only when no error has occurred. Such return statements are often preferred over more structured but more verbose if/else code blocks, and may often be disguised within long, sloppy code.

Upvotes: 2

David Hammen
David Hammen

Reputation: 33126

Is there any particular reason why someone might want to do this, as opposed to simply making a void function?

Why does your mother cut the ends off the roast before putting it in the oven? Answer: Because that's what her grandmother did. However, her grandmother did that for a simple reason: Her roast pan wasn't big enough to hold a full-sized roast.

I work with a simulation tool that in its earliest incarnations required that all functions callable by the simulation engine must return a success status: 0=success, non-zero=failure. Functions that could never fail were coded to always returned zero. The simulation engine has been able to accommodate functions that return void for a long, long, time. That returning an integer success code was the required behavior from some previous millennium hasn't stopped cargo cult programmers from carrying this behavior of writing functions that always returning zero forward to the current day.

Upvotes: 1

WiSaGaN
WiSaGaN

Reputation: 48127

Most of the time function like this should be returning void.

Another possibility is that this function is one of a series of closed-related functions that have the same signature. The return int value may signal the status, say returning 0 for success, and a few of these functions always succeed. To change the signature may break the consistency, or would make the function unusable as function objects since the signature does not match.

Upvotes: 1

Jason Champion
Jason Champion

Reputation: 2750

It's a convention, particularly among C programmers, to return 0 if the function did not experience any errors and return a nonzero value if there was an error.

This has carried over into C++, and although it's less common and less of a convention due to exception handling and other more object-oriented-friendly ways of handling errors, it does come up often enough.

Upvotes: 2

Keith Thompson
Keith Thompson

Reputation: 263497

There's a common convention of int functions returning 0 for success and some non-zero error code for failure.

An int function that always returns 0 might as well be a void function if viewed in isolation. But depending on the context, there might be good reasons to make it compatible with other functions that returning meaningful results. It could mean that the function's return type won't have to be changed if it's later modified so it detects errors -- or it might be necessary for its declaration to be compatible with other int-returning functions, if it's used as a callback or template argument.

I suggest examining other similar functions in the library or program.

Upvotes: 7

Related Questions