• C Programming Video Tutorials

Return Statement in C



The return statement appears as the last statement in a C function. It returns the program flow control back to the calling function. The return statement in C terminates the execution of a function and returns control to the calling function. Every function in C should have return as its last statement. The return type of a function and the type of expression returned by it must match.

The return statement can also return a value to the calling function. Execution resumes in the calling function at the point immediately following the call. The calling function receives the value returned by the called function in a variable whose type should match with the function’ return type.

The following main() function shows return as its last statement −

int main(){
   // function body;
   return 0;
}

The main() function returning 0 indicates the successful completion of the function. To indicate failure of the function, a non−zero expression is returned.

Void return type

A function's return type can be void. In such a case, return statement is optional. It may be omitted, or return without any expression is used.

#include <stdio.h>
/* function declaration */
void test(){
   return;
}
int main() {
   test();
   printf("end");
   return 0;
}

Each function in the program must have a forward declaration of its prototype. By default, each function returns an integer. However, function of other return types without prototype is not accepted.

Return type mismatch

int  main(){
   test(5);
   printf("end");
   return 0;
}
float test(int a) {
   return 1.1 ;
}

Output

Error: C:\Users\mlath\test.c|12|error: conflicting types for 'test'

This is because, function without prototype is assumed as of int type, which conflicts with the definition.

The same error occurs if the return type of a function in the prototype doesn't match with the type of return expression, an error is reported as below −

float test(int);
int  main(){
   test(5);
   printf("end");
   return 0;
}
float test(float a){
   return 1.1 ;
}

Multiple return

A function can be defined with more than one arguments, but can return only one value. You can however use multiple conditional return statements as shown below −

int test(int);
int  main() {
   test(5);
   printf("end");
   return 0;
}

int test(int a){
   if (a<3)
      return 1;
   else
      return 0;
}

Function returns an array

It is not possible to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Example

The following program shows how to pass an array to a function that returns an array after performing a certain process.

#include <stdio.h>
int* test(int *);
int  main(){
   int a[] = {1,2,3,4};
   int i;
   int *b = test(a);
   for (i=0; i<4; i++){
      printf("%d\n", b[i]);
   }
   return 0;
}
int * test(int*a){
   int i;
   for (i=0; i<4; i++){
      a[i] = 2*a[i];
   }
   return a;
}

Output

2
4
6
8

function can only return a single value using return statement. To return multiple values, we use pointers or structures

exit()

Unlike the return statement, the exit() function is also used to terminate the execution of the program without transferring the control back to the calling function. It is used inside a function when the program has finished its execution or when an unrecoverable error occurs. It is a standard way of handling exception errors in C. When exit() is called, the program control does not return to the point where exit() was invoked. Instead, the control is handed back to the operating system.

The exit() function is library function defined in stdlib.h header file.

Syntax

void exit(int status);

exit() is typically called from the main() function or any other function to terminate the entire program.

It is included in the <stdlib.h> header file.

Since it results in termination of the program, exit() does not return a value directly to the caller function. Instead, it terminates the program and returns a status code. It is an integer that represents the exit status of the program, indicating success or failure.

Advertisements