answersLogoWhite

0

It's not so much a need but a requirement. Every C function must contain at least one return statement, typically at the end of the function body (before the closing brace). However, a C function may have more than one return path, and each requires its own return statement. Note that it does not matter whether the function returns a value or not; even a function that returns void must have a return statement.
In C++ the rules regarding return statements are more relaxed. Functions that return void do not require a return statement at all; when execution reaches the closing brace of a void function, a return statement is implied. All functions that return a value of any type other than void must have a return statement. The one exception to this rule is the global main function which must always return an int. If the global main function has no return statement then the value 0 is implicitly returned to the calling environment (the value 0 is typically used to indicate no error). However, if we need to return other values (including 0), then we must include a return statement.

Functions that have multiple return paths are considered poor style and should be avoided. Functions are generally much easier to read and maintain when there is only one return path which should logically terminate at the very end of the function. However, eliminating multiple return paths can also produce more efficient machine code, particularly in functions with highly complex return paths. Eliminating multiple return paths needn't be difficult, we simply need to refactor the function such that each unique return path is representing by some function which returns the appropriate value. The calling function simply stores that value and returns it at the end of the function, thus simplifying the overall complexity of the calling function. Refactoring complex functions into smaller, simpler function calls is good style in and of itself; well-named, descriptive function calls result in code that is largely self-documenting and thus more abstract. Although function calls are themselves expensive, small and simple functions can be easily inline-expanded by the compiler's optimisers, so it's a win-win.
User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

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.


List of command used in c language?

It's easy: there are no commands in C, but a few statements (such as: expression, if, else, switch, while, do-while, for, break, continue, return and goto), and countless library functions (like printf, malloc and fopen).


How do you write a program to find the largest of four numbers using nested if else statements?

int largest_of_four (int a, int b, int c, int d) { if (a>b && a>c && a>d) return a; else if (b>c && b>d) return b; else if (c>d) return c; else return d; } Note that the "else" clauses are actually redundant here because we can rewrite the function as follows: int largest_of_four (int a, int b, int c, int d) { if (a>b && a>c && a>d) return a; if (b>c && b>d) return b; if (c>d) return c; return d; }


Four basic instruction in C language?

They are 'statements' to speak strictly, they are: , , if, else, while, for, do, return, switch, break, continue, gotoNote: is zero or more statements between '{' and '}'


Where do you use no argument no return in c function?

Where there is no need to return any type of value from a function


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.


How do you terminate statements in C?

All statements must be terminated with a semi-colon in C.


How do you write a c program without using scan statements?

#include <stdio.h> int main (void) { puts ("What is a 'scan statement'?!"); return 0; }


What are the three required sets of statements for every function that a programmer writes in C plus plus?

There is no requirement for any statement in a C++ function, let alone three sets of statements. For instance, the following is a perfectly valid function: void foo(){} Clearly this does nothing as it has no statements in the function body, but it is nevertheless a valid function. Perhaps you mean something else by "statements". The only requirement of a function is that it have a return type, a valid name, an argument list and a function body. The return type may be void, of course, and the argument list may be empty, but it must include the ellipses. The function declaration need not include the function body, and the argument list need only specify the type of argument (the argument names are optional and need not match those declared in the actual definition). The function name and the arguments define the function signature (the prototype), thus the three required "components" of a function are the return type, the signature and the function body.


What are different types of control statements available in c?

1. goto, break, continue, return 2. if-else, switch-case-default 3. while, for, do-while


Control statement in c plus plus?

Control statements are statements that alter the flow of execution according to the evaluation of an expression (the condition). The C++ control statements are ifstatements, switch statements and the tertiary conditional operator, ?:.


What is the structure of a C program with example?

In C, programs are composed of statements. These statements are terminated with a semi-colon, and are collected in sections known as functions. By convention, a statement should be kept on its own line, as shown in the example below: #include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0; }