Skip to main content
Engineering LibreTexts

10.5: Function Return Types

  • Page ID
    29093
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Return Statement in C++

    The return statement returns the flow of the execution to the location from where it is called. As soon as the return statement is executed, the flow of the program stops immediately and return 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 type of function, a return value must be returned.

    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).
      • Example:
    // C++ code to show not using return 
    // statement in void return type function 
    #include <iostream> 
    using namespace std; 
    
    // void method 
    void Print() 
    { 
        cout <<"Welcome to CSP 31A") << endl;
    } 
    
    int main() 
    { 
        // Calling print 
        Print(); 
    
        return 0; 
    } 
    

    The function is called, and processes, when done the control of the program returns to where the function was called. The output of the above code example is as follows:

    Welcome to CSP 31A
    

     Void function with a return statement 

    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 to signify that the code is indeed to return to the calling location, and this is what the programmer intended.

    // C++ code to show using return 
    // statement in void return type function 
    #include <iostream> 
    using namespace std; 
    
    // void method 
    void Print() 
    { 
        printf("Welcome to CSP 31A"); 
    
        // void method using the return statement 
        return; 
    } 
    
    // Driver method 
    int main() 
    { 
        // Calling print 
        Print(); 
        return 0; 
    } 
    

    There is no difference here, the return statement adds nothing to this code. Many organizations require a return statement, just for clarification that it is indeed the desire of the programmer to return at this point.

    Output is the same.

    Welcome to CSP 31A
    

    Invalid return from a void function

    But if the return statement tries to return a value in a void return type function, that will lead to errors. So if we have a void function, and attempt to return a value like the example below

    void func()
    {
        return value;
    }
    

    When you attempt to compile this code you would receive a message stating that you can not do that. Some compilers give you a warning, others give you an error, which is correct, in that this is against the rules of C++.

    warning: 'return' with a value, in function returning void
    OR
    error: return-statement with a value, in function returning 'void'
    • 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;
      }
      

      When a function has a return type, the value being returned should be that type.  Any value that is of a different type will be forced into that type before it is returned. The easiest thing is to make sure you are returning the proper type of value.

      // C++ code to illustrate Methods returning 
      // a value using return statement 
      
      #include <iostream> 
      using namespace std; 
      
      // integer return type - the return value MUST be an int
      // function to calculate sum 
      int SUM(int inputV1, int inputV2) 
      { 
          int s1 = inputV1 + inputV2; 
      
          // method using the return statement to return a value 
          // It will force any value to an integer before it is returned
          return s1; 
      } 
      
      // Driver method 
      int main() 
      { 
          int num1 = 10; 
          int num2 = 10; 
          int sum_of;
          
          // The SUM() return value is being assigned to an int
          sum_of = SUM(num1, num2); 
          cout << "The sum is " << sum_of; 
          return 0; 
      } 
      

      Output:

      The sum is 20

    Adapted from:
    "return statement in C/C++ with Examples" by Chinmoy LenkaGeeks for Geeks is licensed under CC BY-SA 4.0 


    This page titled 10.5: Function Return Types is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.