A function communicates back to the caller by "returning" a value.
The type of value returned by a function (i.e. the return-type) is specified when the function is defined. If the return-type is not specified, then it defaults to
A function does not have to return a value. In these cases, the return-type
- Functions can return at most one value.
- A function returns to the caller by executing a
- Functions can have multiple
returnstatements. (Although structured programming purists don't like this.)
- A function returns to the function that called it.
The syntax of the
returnstatement is as follows.return; //used when the return-type of the function is void return EXPR; EXPR is evaluated. The result of the evaluation, if needed, is converted to the return-type of the function. For example, if the return-type of the function is int , then return 3.14; will cause the 3.14 double to be converted to an int; therefore, the calling function will be returned the value 3. Optionally, the return EXPR can be enclosed in parens. return (EXPR); return (3.14); or return 3.14; return (EXIT_SUCCESS); or return EXIT_SUCCESS; return (i + 3 * j); or return i + 3 * j; return (i > 4); or return i > 4;
If a function returns a value, then it is the caller's responsibility to examine the return value.
If a function's return-type is
returnstatements, if any, cannot have any expressions.
If a function is called that returns a value and the return value is ignored by the caller, then the caller should type-cast the function call to be
(void).(void) printf("hello, world\n");