return statement in C

The return statement serves two purposes:

  • On executing the return statement it immediately transfers the control back to the calling program
  • It returns the value present in the parentheses after return, to th3e calling program. In the above program the value of sum of three numbers is being returned.
  • There is no restriction on the number of return statements that may be present in a function. Also, the return statement need not always be present at the end of the called function. The following program illustrates these facts.
fun( ) 
 char ch ; 
 printf ( "\nEnter any alphabet " ) ; 
 scanf ( "%c", &ch ) ; 
 if ( ch >= 65 && ch <= 90 ) 
 return ( ch ) ; 
 return ( ch + 32 ) ; 

In this function different return statements will be executed depending on whether ch is capital or not

  • Whenever the control returns from a function some value is definitely returned. If a meaningful value is returned then it should be accepted in the calling program by equating the called function to some variable. For example

sum = calsum ( a, b, c ) ;

  • All the following are valid return statements

return ( a ) ;
return ( 23 ) ;
return ( 12.34 ) ;
return ;

In the last statement a garbage value is returned to the calling function since we are not returning any specific value. Note that in this case the parentheses after return are dropped.

  • If we want that a called function should not return any value, in that case, we must mention so by using the keyword void as shown below.

void display( )
printf ( “\nHeads I win…” ) ;
printf ( “\nTails you lose” ) ;

  • A function can return only one value at a time. Thus, the following statements are invalid.

return ( a, b ) ;
return ( x, 12 ) ;

Leave a Comment