ILE C/C++ Language Reference


return Statement

A return statement ends the processing of the current function and returns control to the caller of the function.

A return statement has one of two forms:

>>-return--+------------+--;-----------------------------------><
           '-expression-'
 
 

A value-returning function must include an expression in the return statement. A function with a return type is void cannot contain an expression in its return statement.

For a function of return type void, a return statement is not strictly necessary. If the end of such a function is reached without encountering a return statement, control is passed to the caller as if a return statement without an expression were encountered. In other words, an implicit return takes place upon completion of the final statement, and control automatically returns to the calling function. A function can contain multiple return statements. For example:

void copy( int *a, int *b, int c)
{
   /* Copy array a into b, assuming both arrays are the same size */
 
   if (!a || !b)       /* if either pointer is 0, return */
      return;
 
   if (a == b)         /* if both parameters refer */
      return;          /*    to same array, return */
 
   if (c == 0)         /* nothing to copy */
      return;
 
   for (int i = 0; i < c; ++i;) /* do the copying */
      b[i] = a[1];
                       /* implicit return */
}

In this example, the return statement is used to cause a premature termination of the function, similar to a break statement.

An expression appearing in a return statement is converted to the return type of the function in which the statement appears. If no implicit conversion is possible, the return statement is invalid.

Related References


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]