Professional Documents
Culture Documents
Return Statement in C
Return Statement in C
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.
return value_or_expression;
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.
Example
#include <stdio.h>
/* function declaration */
void test(){
return;
}
int main() {
test();
printf("end");
https://www.tutorialspoint.com/cprogramming/c_return_statement.htm 1/5
6/16/24, 12:33 PM Return Statement in C
return 0;
}
Example
int main(){
test(5);
printf("end");
return 0;
}
float test(int a) {
return 1.1 ;
}
Output
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 ;
}
https://www.tutorialspoint.com/cprogramming/c_return_statement.htm 2/5
6/16/24, 12:33 PM Return Statement in C
Example
int test(int);
int main() {
test(5);
printf("end");
return 0;
}
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;
https://www.tutorialspoint.com/cprogramming/c_return_statement.htm 3/5
6/16/24, 12:33 PM Return Statement in C
}
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
Syntax
exit() is typically called from the main() function or any other function to terminate
the entire program.
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.
https://www.tutorialspoint.com/cprogramming/c_return_statement.htm 4/5
6/16/24, 12:33 PM Return Statement in C
It is an integer that represents the exit status of the program, indicating success or
failure.
https://www.tutorialspoint.com/cprogramming/c_return_statement.htm 5/5