Ad

Sunday, June 17, 2007

Introduction to Function Overloading in C++

Let us start this with a question!

All of you know that we cannot have two variables of the same name, but can we have two Functions having the same name.

The answer is YES, we can have two functions of the same name by a method known as function overloading and the functions having the same name are known as overloaded functions.

So, what’s the use of Function Overloading

Function overloading is one of the most powerful features of C++ programming language. It forms the basis of polymorphism (compile-time polymorphism).

Most of the time you’ll be overloading the constructor function of a class.

How function overloading is achieved

One thing that might be coming to your mind is, how will the compiler know when to call which function, if there are more than one function of the same name.

The answer is, you have to declare functions in such a way that they differ either in terms of the number of parameters or in terms of the type of parameters they take.

What that means is, nothing special needs to be done, you just need to declare two or more functions having the same name but either having different number of parameters or having parameters of different types.

Example 1: Overloading Functions that differ in terms of NUMBER OF PARAMETERS

  //Example Program in C++
  #include<iostream.h>

  //FUNTION PROTOTYPES
  int func(int i);
  int func(int i, int j);

  void main(void)
  {
   cout<<func(10);//func(int i)is called

   cout<<func(10,10);//func(int i, int j) is called
  }

  int func(int i)
  {
   return i;
  }

  int func(int i, int j)
  {
   return i+j;
  }

Example 2: Overloading Functions that differ in terms of TYPE OF PARAMETERS

  //Example Program in C++
  #include<iostream.h>

  //FUNTION PROTOTYPES
  int func(int i);
  double func(double i);

  void main(void)
  {
   cout<<func(10);//func(int i)is called

   cout<<func(10.201);//func(double i) is called
  }

  int func(int i)
  {
   return i;
  }

  double func(double i)
  {
   return i;
  }

One more Question, is the program below, valid?

  //Example Program in C++
  #include<iostream.h>

  //FUNTION PROTOTYPES
  int func(int i);
  double func(int i);

  void main(void)
  {
   cout<<func(10);

   cout<<func(10.201);
  }

  int func(int i)
  {
   return i;
  }

  double func(int i)
  {
   return i;
  }

No, because you can’t overload functions if they differ only in terms of the data type they return.

I Hope this article throws some light on function overloading!

Good-Bye for now

Thanks for reading…

Do check back for updates!

Related Articles:

42 comments:

  1. Anonymous8:33 PM

    kewl.... nice explanation.........

    ReplyDelete
  2. EinsTeiN6:02 PM

    really thanks for the explanation, it's very usefull

    ReplyDelete
  3. Anonymous1:06 PM

    Very GOOD.....and very understandable code to learn Function OverLoading easily....nice wrok,,,

    ReplyDelete
  4. @Anonymous

    Thanks!
    Enjoy!

    ReplyDelete
  5. @ Anonymous,

    Thanks!

    Enjoy!

    ReplyDelete
  6. Anonymous9:28 PM

    gr8888888 explanation.............

    ReplyDelete
  7. @ Anonymous,

    Thank you.

    ReplyDelete
  8. thanks for explanation this much clear.

    ReplyDelete
  9. Really Nice work.... Very easy and understable language explanation with very nice examples.... Thanx!!!

    ReplyDelete
  10. @ Amitesh,

    It's my pleasure.
    Enjoy.

    ReplyDelete
  11. Hi Eman,

    Thanks.

    Enjoy!

    ReplyDelete
  12. Anonymous10:19 AM

    really useful..........

    ReplyDelete
  13. Thank you for clean explanation with simple examples

    ReplyDelete
  14. Hi Thiagarajan,

    It's my pleasure!

    ReplyDelete
  15. Anonymous8:25 PM

    nice explanation.
    thanx.

    ReplyDelete
  16. Hi Anonymous,

    It's my pleasure!
    Enjoy

    ReplyDelete
  17. Nice explanation.Quite understandable!!!
    Thanx

    ReplyDelete
  18. Hi Maria,

    My pleasure.

    ReplyDelete
  19. Anonymous1:56 PM

    Thank you Arvind, I searched through 7 other explainations before I arrived at this one and it sums up overloading functions wonderfully. ;)
    Chris

    ReplyDelete
  20. Anonymous4:43 PM

    its nice..:)
    but what happens when function overloading comes with default parameters..
    like two functions
    int f(int a);
    int f(int a,int b=0);
    and i call from main like f(5)...cant find the answer in book or over the net
    sourav

    ReplyDelete
  21. Hi Sourav,

    I don't think that such a code will compile (as function call is ambiguous), or for that matter, you even need to use such concept.

    Please refer to the following nice article for more info on this:

    http://smart2help.com/e-books/ticpp-2nd-ed-vol-one/Chapter07.html

    ReplyDelete
  22. Anonymous4:42 PM

    thanks a lot...nice explanation!!

    ReplyDelete
  23. Nice work !
    http://javaenthusiastic.blogspot.com

    ReplyDelete
  24. I have one question regarding this, mite be silly but still want to give abit of light on it.
    Why we have to declare the FUNCTION PROTOTYPES before the main, is it compulsion? as I tried with giving the FUNCTION PROTOTYPES inside the main() function as we do for any other function..
    why the difference ??

    ReplyDelete
  25. your explaination is quiet simple and its ver nice

    ReplyDelete
  26. hey.. nice explanation..
    i want to ask a question..
    is there any way that we take a value from user.. n based on the type of value he entered(float, long int, double etc.. ) we execute functions..
    thankss .. :D

    ReplyDelete
  27. Anonymous4:17 PM

    hiii
    thanks dear!!!!
    it helps alot..........

    ReplyDelete
  28. thank u man.....keep up the good work!

    ReplyDelete
  29. awesome explanation... thanks... itz really awesome :)

    ReplyDelete
  30. Anonymous11:55 PM

    Quickly Understandable

    ReplyDelete
  31. shekar1:48 PM

    thank you and iam expecting few more examples

    ReplyDelete
  32. Anonymous4:17 PM

    :-D Awesome!
    Short and sweet, thanks!

    ReplyDelete
  33. Anonymous7:15 PM

    excellent n easy xplanatn..
    thankz..:)

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.