Return statements in C

Return statements in C

  • the goto statement is used in two ways. The first way is to terminate a function and come back to the calling function.
Syntax: return;
  • The second way of using return is to return a value to the calling function.
Syntax:  return value;
Example:  return x;    /* x value is returned to calling function */
    return 100; /* 100 is returned.*/
  • Any function contains a group of statements. The general format of a function is shown here:
returntype myfunction()
{
     statements;
}
  • in the above example, If a function returns an int value, then the returntype will be ‘int’. If a function returns a float type value, then the returntype will be ‘float’. If a function does not return any value, then we should write ‘void’ before the function name. ‘void’ means ‘no value’ will be returned by the function.
  • It is possible to call one function from another function.
void m1()   /* void=no value is returned by this function */
{
   printf("\n Inside m1() function");
   return; /* terminate this function */
}
void main()
{
   printf("\n Inside main() function ");
   m1();
   printf("\n Returned from m1() function ");
}

The output will be:

Inside main() function.
Inside m1() function.
Returned from m1() function.
  • In above example, we can call m1() function from main() function. When the return is used in m1(), it will terminate that function and the flow of execution returns to the main() function.
  • We can also use a return statement to return some value to the calling function, i.e to main() function.
int m1()   /* this function returns value */
{
   printf("\n Inside m1() function");
   return 100; /* terminate this function and return 100 */
}
void main()
{
   int x;
   printf("\n Inside main() function ");
   x=m1();
   printf("\n Returned from m1() function ");
   printf("\n Returned value from m1() is %d",x);
}

The output will be:

Inside main() function.
Inside m1() function.
Returned from m1() function.
Returned value from m1() is 100
naveed08st
 

Click Here to Leave a Comment Below 0 comments

Leave a Reply: