Value of a return Expression and Function Value

If an expression is present on a return statement, the value of the expression is returned to the caller. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

The value of the return statement for a function of return type void means that the function does not return a value. If an expression is not given on a return statement in a function declared with a non-void return type, the complier issues an error message.

You cannot use a return statement with an expression when the function is declared as returning type void.

Examples of return Statements

return;            /* Returns no value            */
return result;     /* Returns the value of result */
return 1;          /* Returns the value 1         */
return (x * x);    /* Returns the value of x * x  */

The following function searches through an array of integers to determine if a match exists for the variable number. If a match exists, the function match returns the value of i. If a match does not exist, the function match returns the value -1 (negative one).

int match(int number, int array[ ], int n)
{
   int i;
 
   for (i = 0; i < n; i++)
      if (number == array[i])
         return (i);
   return(-1);
}

Related References

IBM Copyright 2003