The Complete Guide to Using Return Statements in C Programming

Return Statements in C

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 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’ denotes that the function will return ‘no value.

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 the above example, we can call m1() function from main() function. When return is used in m1(), it will terminate that function, and the flow of execution returns to main() function.

We can also use the return statement to return some value to the calling function, i.e to the 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
Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.