dubnde
dubnde

Reputation: 4441

What is the point of adding a return statement at end of a void function?

I see functions/methods with a void return in the signature that have a return statement at the end of the function. What is the reason for this, and does this apply to other languages?

For all I know, I can use return if I want to exit anywhere else but the end of the function.

A C example:

void function(void)
{
   int x = 1 + 2;

   return;    // what do we need this for, if at all?
}

Upvotes: 6

Views: 7194

Answers (10)

user3502455
user3502455

Reputation:

I don't think you need it, but you can have it. Try taking it out and running the program again. See whats the difference.

Upvotes: 2

Dan Olson
Dan Olson

Reputation: 23397

It is indeed pointless... at least to the compiler, but stylistic, much like many things in C++. An example:

int main ()
{
   DoSomething ();
   return 0;
}

In C++ the return 0 is entirely pointless as far as a conforming compiler is concerned. Yet sometimes people prefer to include redundant or meaningless statements just to be explicit. I'd be hesitant to chalk these kinds of things up to programmer ignorance. It's just stylistic, feel free to get rid of it if you feel like it.

Upvotes: 4

Brian Neal
Brian Neal

Reputation: 32399

For a function that returns void, it doesn't matter, since "falling off the end" of a void function simply returns as you would expect.

However some people like to do this to aid future maintenance. For example, if someone later decides to change the return type of the function to say int, you should get a compiler error if you forget to change that final return statement to make it return an int value. If you failed to have a return statement at all, the return value is undefined, and many older compilers wouldn't warn you about this (it's true - I've seen it!). This isn't much of a issue these days, as most modern compilers will warn about these things.

Upvotes: 8

sykloid
sykloid

Reputation: 101416

I always include a return statement at the end of my functions, even when they don't return anything. My original reasons for it were that I was being explicit, there was no ambiguity about where the function ended, I always got irked by functions that "walked off the end". Afterwards, it was simply a habit, hard to break (if I wanted to).

Upvotes: 7

Pete Kirkham
Pete Kirkham

Reputation: 49331

Someone took a 'a function must have a single return' coding guideline too literally.

Upvotes: 18

sharptooth
sharptooth

Reputation: 170569

It's useless in C/C++ and just reduces readability.

One real language example where this is needed is assembler. But in assembler the return statement is not a part of language. Instead you have to invoke a special instruction (ret in x86) which causes return of control. Unless you do it execution just continues and what will happen next depends on may factors, luck and moonphase included.

Upvotes: 3

Aamir
Aamir

Reputation: 15576

This seems pointless here. But my guess is that this kind of thing can be used to put a breakpoint in IDEs which don't support putting a breakpoint at the closing brace and by putting a breakpoint here, some values can be checked in the watch window etc.

Upvotes: 23

Mykola Golubyev
Mykola Golubyev

Reputation: 59932

My guess it is because function used to return some value and a programmer probably thought that it will again.

Upvotes: 2

Stephan202
Stephan202

Reputation: 61609

As you mention yourself, you only need the return statement if you want to exit the function before the last statement. In the given example the return statement serves no purpose whatsoever.

Upvotes: 4

Related Questions