The return statement


return will terminate a function and send a value to its caller. The value returned is the result of an expression.


As an Example this will print 7

        /* Declare Func */

        int Func(void);

	main()
 	{
 	   /* Call Func and print its return value. */

	   printf("%d \n", Func());
        }

        /* Define Func. */

        int Func(void)
        {
           return 7;
	}

What ever follows the return statement will be evaluated as an expression. So, to be consistant you could place brackets around the return value.


        return(7);

Or you could evaluate a formula.:


	return (Count-1);

If a function returns void the return statement is not required at the end of the function block.

If you need to leave a function before the end of the function block you can provide the return statement without an expression.


	void CheckDate(int)

	main()
	{
  	    CheckDate(40)
        }

	void CheckDate(int Month)
	{
	    if (Month > 31)
	    {
	        return;
	    }

	    puts("Month is valid");
        }


A function is not limited to returning an int, any valid datatype can be returned. More information needed. Maybe a char * example

There is no agreed convention on the data returned either. Depending on the author, one function can return a positive value to indicate an error and another function can return a zero to indicate error. The only way to be sure of what will be returned is to read the documentation associated with the function.


See also:

The exit function.


Top Master Index Keywords Functions


Martin Leslie 03-June-1999