• C Programming Video Tutorials

Return Statement in C



The return statement terminates the execution of a function and returns control to the calling function. Every function should have a return statement as its last statement. While using the returns statement, the return type and returned value (expression) must be the same.

Syntax of return Statement

Here is the syntax of the return statement:

return value_or_expression;

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.

The void return statement

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.

Example

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

Return type mismatch in return statement

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.

Example

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 values with return statement

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 −

Example

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

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

Function returning 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() instead of return statement

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