This is a more correct way to return from a function:
void function() {
OR
void function() {
Justification for the second way:
- It more clearly expresses the intentions of the developers.
- This helps to detect the end of the function at compile time:
Suppose you have a scenario like this: you have a bunch of functions, and you have to enter some code at the end of these functions. But for some reason you do not want / cannot change such a huge number of functions. What can you do about this? Return and macro , for example:
#include<stdio.h> #define MAX_LINES 1000 #define XCAT(a,b) a##b #define CAT(a,b) XCAT(a,b) #define return returns[__LINE__] = 1;\ if (returns[__LINE__])\ {printf("End of function on %d line.\n",__LINE__);}\ int CAT(tmp,__LINE__); \ if ((CAT(tmp,__LINE__)=returns[__LINE__], returns[__LINE__] = 0, CAT(tmp,__LINE__)))\ return static int returns[MAX_LINES]; void function1(void) { return; } void function2(void) { return; } int main() { function1(); function2(); return 0; }
c function void
Agnius Vasiliauskas Jan 25 '12 at 13:11 2012-01-25 13:11
source share