return statement in C++ with Examples

In this tutorial, we are going to learn about return statements in C++ along with some examples. To get a clear understanding of the topic first let us understand what are return statements.

The return statement basically returns the flow of the execution of the function from where that function is called. When the return statement is executed the flow of the program breaks from that point and returns the control to the function from where it was called. It causes the function to exit and hands back the value to the caller.

In case of function with return type-

  1. Void- Return statement may or may not return a value.
  2. Non-Void – The return statement must return a value to the calling function of its defined return type.

Syntax:

return [expression];


Some ways in which we can use the return statements are-

1) Case when using those methods that do not return a value

In the C/C++ program, we cannot leave the return statement when the methods are of the return type, but we can skip the return statement if the function is of the void return type. We will have two cases in the void return type function-

Function with void return type where no return statement is used- In case of a function has a void return type it will not return any value to the calling function. So, it becomes completely unnecessary to use the return statement inside that function.

Look at the example below to understand the case of a function with a void return type when no return statement is used.

#include <iostream>
using namespace std;

// void method without return statement 
void display() { 
    cout<<("Welcome to Codespeedy, hope you are coding well!!"); 
    
} 
int main() 
{ 
    display();//calling the function 
    return 0; 
    
}

Output:

Welcome to Codespeedy, hope you are coding well!!

2) Case when using those methods which return a value

In the case of methods that define a return type, the return statement must be used and should be immediately followed by the return value of the specified return type. To have more clarity, look at the example below of the function which returns the product of two integers.

#include <iostream>
using namespace std; // function to multiply two numbers 

int multiply(int x, int y) {
    int m = x * y; 
    
    // using return statement 
    // to return a value 
    return m; 
    
}

int main()
{ 
    int a = 20; 
    int b = 10; 
    int product = multiply(a, b); //displaying the product
    cout << "Product = " << product; 
    return 0; 
}

 

Output:

Product = 200

The function (multiply) is of non-void return type that uses return statement to return the product of two numbers.

Sometimes, the function can use multiple return statements as well. Consider a function (maximum) that returns the maximum value amongst the two numbers. This type of function will use multiple return statements and return the control to the caller function. Below is the code to illustrate the same.

#include<iostream>
using namespace std;

//Multiple return statements
//used in this case
int maximum(int x, int y) {
    if (x > y) {
        return x;
    } else {
        return y;
    }
}

int main(){
    int n1=10;
    int n2=20;
    
    int max_value=maximum(n1,n2); //calling the function
    
    cout<<"Max value is "<<max_value; //displaying the maximum value
    
    return 0;
}

Output:

Max value is 20

The example given above uses multiple return statements and the (maximum) function returns the maximum value between the given two numbers.

Thus, we have learned how to use return statements in the case of functions with void and non-void return types, and also about the functions where multiple return statements are used.

Also read: return vs exit() in main() in C++ with examples

Leave a Reply

Your email address will not be published.