C functions can return any type other than arrays (excluding array pointers.
The return type is not specified. Int is returned by default in the previous C language versions. Currently, c99 and C ++ are not supported.
Void indicates that no return value is returned, and a return statement is not required. If other statements are used, a return statement must exist.
In the main function (only the main function), if you forget return, some compilers will automatically add return 0;
1. Every time you execute the Return Statement, the function will exit:
# Include <stdio. h> int main (void) {printf ("111 \ n"); printf ("222 \ n"); Return 0; the statement after/* is not executed */printf ("333 \ n"); printf ("444 \ n"); getchar (); Return 0 ;}
2. If the function does not return a value, you can use the return statement without a parameter to jump out:
# Include <stdio. h> void PRN (void); int main (void) {PRN (); getchar (); Return 0;} void PRN (void) {printf ("111 \ n"); printf ("222 \ n"); return;/* return */printf ("333 \ n") without parameters "); printf ("444 \ n ");}
3. return is not followed by "equal sign", but the return value can also be written in brackets:
# Include <stdio. h> long mysqr (int x); int main (void) {int I = 9; I = mysqr (I); printf ("% d \ n", I ); getchar (); Return 0;} long mysqr (int x) {return (x * X);/* return value in brackets */}
4. Differences between return and exit:
When the main function ends, Program Return 0; or return (0); Return 0 to the system indicates normal exit.
If a non-zero value is returned, return 1; or return (1); indicates that the exception ends.
In a general function, return only exits the current function;
However, exit (1); is the exit program, where 1 is the return value to the system, indicating an abnormal exit;
You can also use abort () for abnormal exit. Both of these functions (exit and abort) are declared in stdlib. h.
# Include <stdio. h> # include <stdlib. h> void PRN (void); int main (void) {PRN (); getchar (); Return 0;} void PRN (void) {printf ("ABC \ n"); exit (1);/* exit the program abnormally */}
5. Every time you execute the Return Statement, the function will exit:
# Include <stdio. h> int main (void) {printf ("111 \ n"); printf ("222 \ n"); Return 0; the statement after/* is not executed */printf ("333 \ n"); printf ("444 \ n"); getchar (); Return 0 ;}
6. Pay attention to the type of the returned value:
# Include <stdio. h> float average (int A, int B, int C); int main (void) {printf ("% G \ n", average (2,6, 9 )); getchar (); Return 0;} float average (int A, int B, int c) {return (A + B + C)/3.0;/* if no. 0 will not return the correct result */}