HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 6 Statements

return

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Syntax

return;           /* first form */
return exp;       /* second form */

Arguments

exp

Any valid C expression.

Description

The return statement causes a C program to exit from the function containing the return and go back to the calling block. It may or may not have an accompanying exp to evaluate. If there is no exp, the function returns an unpredictable value.

A function may contain any number of return statements. The first one encountered in the normal flow of control is executed, and causes program control to be returned to the calling routine. If there is no return statement, program control returns to the calling routine when the right brace of the function is reached. In this case, the value returned is undefined.

Return Types

The return value must be assignment-compatible with the type of the function. This means that the compiler uses the same rules for allowable types on either side of an assignment operator to determine allowable return types. For example, if f() is declared as a function returning an int, it is legal to return any arithmetic type, since they can all be converted to an int. It would be illegal, however, to return an aggregate type or a pointer, since these are incompatible types.

The following example shows a function that returns a float, and some legal return values.

float f(void)
{
float f2;
int a;
char c;
f2 = a; /* OK, quietly converts a to float */
return a; /* OK, quietly converts a to float */
f2 = c; /* OK, quietly converts c to float */
return c; /* OK, quietly converts c to float */
}

Pointer Return Types

The C language is stricter about matching pointers. In the following example, f() is declared as a function returning a pointer to a char. Some legal and illegal return statements are shown.

char *f(void)
{
  char **cpp, *cp1, *cp2, ca[10];
  int *ip1, *ip2;

  cp1 = cp2;       /* OK, types match */
  return cp2;      /* OK, types match */
  cp1 = *cpp;      /* OK, types match */
  return *cpp;     /* OK, types match */

/* An array name without a subscript is converted
 * to a pointer to the first element.
 */
  cp1 = ca;     /* OK, types match */
  return ca;    /* OK, types match */

  cp1 = *cp2;   /* Error, mismatched types         */
                /* (pointer to char vs. char)      */
  return *cp2;  /* Error, mismatched types         */
                /* (pointer to char vs. char)      */
  cp1 = ip1;    /* Error, mismatched pointer types */
  return ip1;   /* Error, mismatched pointer types */
  return;       /* Produces undefined behavior     */
                /* should return (char *)          */
}

Note in the last statement that the behavior is undefined if you return nothing. The only time you can safely use return without an expression is when the function type is void. Conversely, if you return an expression for a function that is declared as returning void, you will receive a compile-time error.

Functions can return only a single value directly via the return statement. The return value can be any type except an array or function. This means that it is possible to return more than a single value indirectly by passing a pointer to an aggregate type. It is also possible to return a structure or union directly. HP C implements this by passing the structure or union by reference if the structure or union is greater than eight bytes.

Example

/* Program name is "return_example".
* This program finds the length of a word that is entered.
*/
#include <stdio.h>

int find_length( char *string )
{
int i;
for (i =0; string[i] != ’\0’; i++);
return i;
}

int main( void )
{
char string[132];
int result;
int again = 1;
char answer;

printf( "This program finds the length of any word you ");
printf( "enter.\n" );
do
{
printf( "Enter the word: ");
fflush(stdin);
gets( string );
result = find_length( string );
printf( "This word contains %d characters. \n", result);
printf("Again? ");
scanf("%c", &answer);
} while (answer == ’Y’ || answer == ’y’);
}

If you execute this program, you get the following output:

This program finds the length of any string you enter.

Enter the string: Copenhagen
The string is 10 characters.
Again? y

Enter the string: galaxy
The string is 6 characters.
Again? n

© Hewlett-Packard Development Company, L.P.