1

I was seeing some code snippet where I saw calling of function that I could not understand.It was something like this

 void recursion(int v,int p=0)     //Definition
 {
         //whatever in the body
 }


  main()
 {
         //something ...
         recursion(0);// *_*
 }

I was taught in the school that calling and definition should have same number of arguments.But here I could not understand.It looks like numbers of arguments can be of different number.

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • I am sorry I didn't know what it is called therefore put a question.But it looks like it is already been answered.Thanks everyone and sorry for inconvinience – thoughtful me Oct 15 '15 at 09:39

2 Answers2

1

That is called a default argument value. They have to be specified after the other arguments without default argument values.

Then if you don't specify the argument in the function call the value will be used.

If you have more than one default argument, say:

void f(int first, int second = 0, char* third = "");

You have to omit the following default values if you are ommitting a preceding one:

//You can do
f(0);
f(1, 2);
//but not
f(1, "Three");
Dominique McDonnell
  • 2,510
  • 16
  • 25
1

This call is equal to:

 main()
 {
         //something ...
         recursion(0,0);// *_*
 }

because the default value for the second variable is 0.

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • 1
    Thanks mate.I hope someone will upvote your beautiful answer.I cannot upvote since I have less reputation.But thanks a lot – thoughtful me Oct 15 '15 at 09:42