Function overloading and return type
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.
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; } |
chevron_right
filter_none
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[]) { } } |
chevron_right
filter_none
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Function Overloading in C++
- Function Overloading and float in C++
- Function overloading and const keyword
- How to return a local array from a C/C++ function?
- C function argument and return values
- How to return multiple values from a function in C or C++?
- Java Function/Constructor Overloading Puzzle
- Function Overloading vs Function Overriding in C++
- Constructor Overloading in C++
- Overloading in Java
- Operator Overloading in C++
- What does main() return in C and C++?
- Constructor Overloading in Java
- Types of Operator Overloading in C++
- C++ | Operator Overloading | Question 10

