Skip to main content
Engineering LibreTexts

10.7: Function Overloading

  • Page ID
    29096
  • \( \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}}\)

    Function Overloading in C++

    Function overloading is a feature in C++ where two or more functions can have the same name but different parameters and behave differently based on the types of arguments passed from the calling function.

    Function overloading can be considered as an example of polymorphism feature in C++.

    Following is a simple C++ example to demonstrate function overloading.

    #include <iostream> 
    using namespace std; 
    
    void print(int i) 
    { 
       cout << " Here is int " << i << endl; 
    } 
    
    void print(double f) 
    { 
       cout << " Here is float " << f << endl; 
    } 
    
    void print(char const *c) 
    { 
       cout << " Here is char* " << c << endl; 
    } 
    
    int main() 
    { 
       print(10); 
       print(10.10); 
       print("ten"); 
       
       return 0; 
    }
    

    In the above code, the first call to the print() function passes an integer argument. The concept of overloading then ensures that the correct function is called with the appropriate parameter specified. So, that is how C++ knows exactly which function to call - by the argument list provided by the function call. The second call to print() has a float/double argument type - so the appropriate function gets called. The same is true with the thrid print() function call - a character string is passed in. The output of the above looks like the following then. 

    Here is int 10
    Here is float 10.1
    Here is char* ten 

    If we go back to our discussion of default parameters from the last lesson, when function overloading done along with default values we need to make sure it will not be ambiguous.

    The compiler will throw error if ambiguous. Take a look at the following code, this will cause an error when compiled

    #include<iostream> 
    using namespace std; 
    
    // A function with default arguments, it can be called with 
    // 2 arguments or 3 arguments or 4 arguments. 
    int sum(int x, int y, int z=0, int w=0) 
    { 
        return (x + y + z + w); 
    } 
    int sum(int x, int y, float z=0, float w=0) 
    { 
        return (x + y + z + w); 
    } 
    /* Driver program to test above function*/
    int main() 
    { 
        cout << sum(10, 15) << endl; 
        cout << sum(10, 15, 25) << endl; 
        cout << sum(10, 15, 25, 30) << endl; 
        return 0; 
    }
    

    In this code we now have an overloaded function, with the parameters slightly changed between the two functions. The first call to the sum() function is ambiguous because C++ can not determine which of the function is being called. Both functions can handle the function call with 2 absolute parameters, but since the signature of both functions have 4 arguments it can not determine which one to call. So, in this instance we need to provide at least a thrid argument to the function call which will be either an integer or a float value, THEN we know which function is being called.

    prog.cpp: In function 'int main()':
    prog.cpp:17:20: error: call of overloaded 
    'sum(int, int)' is ambiguous
      cout << sum(10, 15) << endl; 
                        ^
    prog.cpp:6:5: note: candidate: 
    int sum(int, int, int, int)
     int sum(int x, int y, int z=0, int w=0) 
         ^
    prog.cpp:10:5: note: candidate: 
    int sum(int, int, float, float)
     int sum(int x, int y, float z=0, float w=0) 
         ^

    Key Points:

    • Default arguments are different from constant arguments as constant arguments can't be changed whereas default arguments can be overwritten if required.
    • Default arguments are overwritten when calling function provides values for them. For example, calling of function sum(10, 15, 25, 30) overwrites the value of z and w to 25 and 30 respectively.
    • During calling of function, arguments from calling function to called function are copied from left to right. Therefore, sum(10, 15, 25) will assign 10, 15 and 25 to x, y, and z. Therefore, the default value is used for w only.
    • Once default value is used for an argument in function definition, all subsequent arguments to it must have default value. It can also be stated as default arguments are assigned from right to left. For example, the following function definition is invalid as subsequent argument of default variable z is not default.

    Also - the following gives us a error. the variable w NEEDS a default value according to the rules above.(highlighted statement)

    // Invalid because z has default value, but w after it 
    // doesn't have default value 
    int sum(int x, int y, int z=0, int w) 

     Adapted from:
    "Function Overloading in C++" by raHUL GUPTA 23Geeks for Geeks is licensed under CC BY-SA 4.0
    "Default Arguments in C++" by Multiple contributors, Geeks for Geeks is licensed under CC BY-SA 4.0


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