answersLogoWhite

0


Best Answer

The return statement has only one purpose: to return control back to the calling function.

Although return statements are not required in void functions, return statements can be placed anywhere in any function, void or not. For example:

void f (int x) { // x must be non-zero!

if (!x) return;

// ...

}

In the above example, a precondition is stated in a comment but compilers do not read comments therefore we must test that precondition at runtime. If the precondition fails, then there is no point in continuing any further, so it makes sense to return control to the caller. In a real-world example, we'd set an error condition before returning so that the caller can determine what went wrong.

Note that if we do not put a return statement in a void function, the function automatically returns at the end of the function. This is not the case for functions that return a value; we must explicitly return that value through a return statement. However, we can place the return statement anywhere that is convenient and we can include as many return statements as required. For example:

int g (int x) {

if (!x) return 0;

if (x<=10) return 1;

if (x<=20) return 2;

return 3;

}

In the above example we return the value 0 when x is zero, return 1 when x is in the range 1 to 10, return 2 when x is in the range 11 to 20 and return 3 in all other cases.

The previous example can also be written more succinctly (less verbosely) using the ternary operator:

int g (int x) { return !x ? 0 : (x<=10) ? 1 : (x<=20) ? 2 : 3;

}

However, a good compiler will produce the exact same machine code regardless of how we write the function, therefore it makes sense to use the version that is more readily understood by the reader. After all, code that is easy to understand is generally much easier to maintain.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are uses of return () statements?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Describe the uses of financial accounting?

describe various uses of financial statements


What are some prophetic statements about Jesus Christ?

one is that he will return.


How do you attach statements to your IRS return?

By stapling the attachments to the back of the income tax form.


Is it possible to place a return statement anywhere in 'C' program?

No. Return statements can only appear within a function body, but they can be placed anywhere within that body. If the function returns a value, then the return statement must also return a value of the same type.


Method of reasoning that uses three related statements?

a method of reasoning using three related staterments is


What is it called when a reader uses clues or statements from a story to form an opinion to draw a conclusion?

making an inference


What phrase best describes hyperbole?

Figurative language that uses exaggeration to make a point


What are applications and uses of slotted lever quick return mechanism?

shaper


How many arithmetic statement in c?

Only one: expression. Yes, in C expression is one of the statements. Some other statements are: if, do, goto, while, for, switch, break, continue, return, NULL-statement, compound-statement.


What statements about the difference between formal and informal reasoning is correct?

Formal reasoning uses algorithms rather than heuristics.


How does Buffy return?

Willow uses black magic and a couple rare artifacts.


What are the MySQL statements and their uses?

There are a large number of mysql statements, all with different results and outcomes. I'd recommend looking through a tutorial like http://www.roughguidetophp.com/category/php-with-mysql to get a good idea of how to use them