return statement in C/C++ with Examples - GeeksforGeeks

23 Feb, 2022
Pre-requisite: Functions in C/C++
The return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called. The return statement may or may not return anything for a void function, but for a non-void function, a return value must be returned.
Syntax:
return[expression];
There are various ways to use return statements. Few are mentioned below:
Methods not returning a value: In C/C++ one cannot skip the return statement, when the methods are of return type. The return statement can be skipped only for void types.Not using return statement in void return type function: When a function does not return anything, the void return type is used. So if there is a void return type in the function definition, then there will be no return statement inside that function (generally).Syntax:void func()
{
    .
    .
    .
}
Example:CC++C// C code to show not using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}C++// C++ code to show not using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}Output:Welcome to GeeksforGeeks
Using return statement in void return type function: Now the question arises, what if there is a return statement inside a void return type function? Since we know that, if there is a void return type in the function definition, then there will be no return statement inside that function. But if there is a return statement inside it, then also there will be no problem if the syntax of it will be:Correct Syntax:void func()
{
    return;
}
This syntax is used in function just as a jump statement in order to break the flow of the function and jump out of it. One can think of it as an alternative to “break statement” to use in functions.Example:CC++C// C code to show using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}C++// C++ code to show using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}Output:Welcome to GeeksforGeeks
But if the return statement tries to return a value in a void return type function, that will lead to errors.Incorrect Syntax:void func()
{
    return value;
}
Warnings:warning: 'return' with a value, in function returning void
Example:CC++C// C code to show using return statement// with a value in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();    return 0;}C++// C++ code to show using return statement// with a value in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();      return 0;}Warnings:prog.c: In function 'Print':
prog.c:12:9: warning: 'return' with a value, in function returning void
  return 10;
         ^
Methods returning a value: For methods that define a return type, the return statement must be immediately followed by the return value of that specified return type.Syntax:return-type func()
{
    return value;
}
Example:CC++C// C code to illustrate Methods returning// a value using return statement  #include <stdio.h>  // non-void return type// function to calculate sumint SUM(int a, int b){    int s1 = a + b;      // method using the return    // statement to return a value    return s1;}  // Driver methodint main(){    int num1 = 10;    int num2 = 10;    int sum_of = SUM(num1, num2);    printf("The sum is %d", sum_of);    return 0;}C++// C++ code to illustrate Methods returning// a value using return statement  #include <iostream>using namespace std;  // non-void return type// function to calculate sumint SUM(int a, int b){    int s1 = a + b;      // method using the return    // statement to return a value    return s1;}  // Driver methodint main(){    int num1 = 10;    int num2 = 10;    int sum_of = SUM(num1, num2);    cout << "The sum is " << sum_of;    return 0;}Output:The sum is 20

Methods not returning a value: In C/C++ one cannot skip the return statement, when the methods are of return type. The return statement can be skipped only for void types.Not using return statement in void return type function: When a function does not return anything, the void return type is used. So if there is a void return type in the function definition, then there will be no return statement inside that function (generally).Syntax:void func()
{
    .
    .
    .
}
Example:CC++C// C code to show not using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}C++// C++ code to show not using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}Output:Welcome to GeeksforGeeks
Using return statement in void return type function: Now the question arises, what if there is a return statement inside a void return type function? Since we know that, if there is a void return type in the function definition, then there will be no return statement inside that function. But if there is a return statement inside it, then also there will be no problem if the syntax of it will be:Correct Syntax:void func()
{
    return;
}
This syntax is used in function just as a jump statement in order to break the flow of the function and jump out of it. One can think of it as an alternative to “break statement” to use in functions.Example:CC++C// C code to show using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}C++// C++ code to show using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}Output:Welcome to GeeksforGeeks
But if the return statement tries to return a value in a void return type function, that will lead to errors.Incorrect Syntax:void func()
{
    return value;
}
Warnings:warning: 'return' with a value, in function returning void
Example:CC++C// C code to show using return statement// with a value in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();    return 0;}C++// C++ code to show using return statement// with a value in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();      return 0;}Warnings:prog.c: In function 'Print':
prog.c:12:9: warning: 'return' with a value, in function returning void
  return 10;
         ^

Not using return statement in void return type function: When a function does not return anything, the void return type is used. So if there is a void return type in the function definition, then there will be no return statement inside that function (generally).Syntax:void func()
{
    .
    .
    .
}
Example:CC++C// C code to show not using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}C++// C++ code to show not using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}Output:Welcome to GeeksforGeeks

Syntax:
void func()
{
    .
    .
    .
}

Example:
C
C++
// C code to show not using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}
// C++ code to show not using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");}  // Driver methodint main(){      // Calling print    Print();      return 0;}
Welcome to GeeksforGeeks

Using return statement in void return type function: Now the question arises, what if there is a return statement inside a void return type function? Since we know that, if there is a void return type in the function definition, then there will be no return statement inside that function. But if there is a return statement inside it, then also there will be no problem if the syntax of it will be:Correct Syntax:void func()
{
    return;
}
This syntax is used in function just as a jump statement in order to break the flow of the function and jump out of it. One can think of it as an alternative to “break statement” to use in functions.Example:CC++C// C code to show using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}C++// C++ code to show using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}Output:Welcome to GeeksforGeeks
But if the return statement tries to return a value in a void return type function, that will lead to errors.Incorrect Syntax:void func()
{
    return value;
}
Warnings:warning: 'return' with a value, in function returning void
Example:CC++C// C code to show using return statement// with a value in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();    return 0;}C++// C++ code to show using return statement// with a value in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();      return 0;}Warnings:prog.c: In function 'Print':
prog.c:12:9: warning: 'return' with a value, in function returning void
  return 10;
         ^

Correct Syntax:
void func()
{
    return;
}

This syntax is used in function just as a jump statement in order to break the flow of the function and jump out of it. One can think of it as an alternative to “break statement” to use in functions.
Example:
C
C++
// C code to show using return// statement in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}
// C++ code to show using return// statement in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return statement    return;}  // Driver methodint main(){      // Calling print    Print();    return 0;}
Welcome to GeeksforGeeks

But if the return statement tries to return a value in a void return type function, that will lead to errors.
Incorrect Syntax:
void func()
{
    return value;
}

Warnings:
warning: 'return' with a value, in function returning void

Example:
C
C++
// C code to show using return statement// with a value in void return type function  #include <stdio.h>  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();    return 0;}
// C++ code to show using return statement// with a value in void return type function  #include <iostream>using namespace std;  // void methodvoid Print(){    printf("Welcome to GeeksforGeeks");      // void method using the return    // statement to return a value    return 10;}  // Driver methodint main(){      // Calling print    Print();      return 0;}
prog.c: In function 'Print':
prog.c:12:9: warning: 'return' with a value, in function returning void
  return 10;
         ^

Methods returning a value: For methods that define a return type, the return statement must be immediately followed by the return value of that specified return type.Syntax:return-type func()
{
    return value;
}
Example:CC++C// C code to illustrate Methods returning// a value using return statement  #include <stdio.h>  // non-void return type// function to calculate sumint SUM(int a, int b){    int s1 = a + b;      // method using the return    // statement to return a value    return s1;}  // Driver methodint main(){    int num1 = 10;    int num2 = 10;    int sum_of = SUM(num1, num2);    printf("The sum is %d", sum_of);    return 0;}C++// C++ code to illustrate Methods returning// a value using return statement  #include <iostream>using namespace std;  // non-void return type// function to calculate sumint SUM(int a, int b){    int s1 = a + b;      // method using the return    // statement to return a value    return s1;}  // Driver methodint main(){    int num1 = 10;    int num2 = 10;    int sum_of = SUM(num1, num2);    cout << "The sum is " << sum_of;    return 0;}Output:The sum is 20

Syntax:
return-type func()
{
    return value;
}

Example:
C
C++
// C code to illustrate Methods returning// a value using return statement  #include <stdio.h>  // non-void return type// function to calculate sumint SUM(int a, int b){    int s1 = a + b;      // method using the return    // statement to return a value    return s1;}  // Driver methodint main(){    int num1 = 10;    int num2 = 10;    int sum_of = SUM(num1, num2);    printf("The sum is %d", sum_of);    return 0;}
// C++ code to illustrate Methods returning// a value using return statement  #include <iostream>using namespace std;  // non-void return type// function to calculate sumint SUM(int a, int b){    int s1 = a + b;      // method using the return    // statement to return a value    return s1;}  // Driver methodint main(){    int num1 = 10;    int num2 = 10;    int sum_of = SUM(num1, num2);    cout << "The sum is " << sum_of;    return 0;}
The sum is 20

ramreisokashung
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Converting Strings to Numbers in C/C++
Left Shift and Right Shift Operators in C/C++
Function Pointer in C
Core Dump (Segmentation fault) in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects