Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Function overloading and return type

  • Difficulty Level : Basic
  • Last Updated : 03 Sep, 2018

In C++ and Java, functions can not be overloaded if they differ only in the return type.

For example, the following program C++ and Java programs fail in compilation.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

C++ Program




#include<iostream>
int foo() { 
    return 10; 
}
  
char foo() {  // compiler error; new declaration of foo()
    return 'a'
}
  
int main()
{
    char x = foo();
    getchar();
    return 0;
}

Java Program




// filename Main.java
public class Main {
    public int foo() {
        return 10;
    }
    public char foo() { // compiler error: foo() is already defined
        return 'a';
    }
    public static void main(String args[])
    
    }
}

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above




My Personal Notes arrow_drop_up
Recommended Articles
Page :