C Programming Tutorial
Computer Tutorial

Returning Values



Returning Values

All functions, except those of type void, return a value. This value is specified by the return statement. If a non-void function executes a return statement that does not include a value, then a garbage value is returned.

A Program to demonstrate returning values in function

\* C Program to demonstrate returning values in function *\
# include < stdio.h >
int  mul (  int a, int b) ;
int   main( )
{

int   x, y, z  ;
x = 10 ;
y = 20 ;
z = mul ( x , y ) ;
printf(" %d ",  mul ( x, y )) ;
mul ( x , y ) ;
return ( 0 ) ;

}
int  mul (  int a, int b
{

return ( a * b ) ;

}

The return statement serves two purposes :

1. On executing the return statement it immediately transfers the control back to the calling program.
2. It returns the value present in the parentheses after return, to the calling program. The value of function 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.
»   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.
»   If we want that a called function should not return any value, in that case, we must mention so by using the keyword void.
»   A function can return only one value at a time.
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.