default value

according to me sum should be 40 as i set b as 10 as default! where is problem?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;

    
int main()

{
  
    int q;
      int add(int a,int b=10);
    q=add(30,20);
    cout<< q <<endl;
    return 0;
    
    
    }
       int add(int a,int b){
       
        int x=a;
        int y=b;
        int sum;
        sum=x+y;
        return(sum);
    }
void PrintValues(int nValue1, int nValue2=10)
{
using namespace std;
cout << "1st value: " << nValue1 << endl;
cout << "2nd value: " << nValue2 << endl;
}

int main()
{
PrintValues(1); // nValue2 will use default parameter of 10
PrintValues(3, 4); // override default value for nValue2
}
http://www.learncpp.com/cpp-tutorial/77-default-parameters/
Last edited on
closed account (28poGNh0)
It should be 40 when you dont set the second argument or you set it as 10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# include <iostream>
using namespace std;

int main()
{
    int q;
    int add(int a,int b=10);

    q = add(30,20);//result 50
    // q = add(30); result 40
    // q = add(30,10); result 40
    
    cout << q << endl;

    return 0;
}

int add(int a,int b)
{
    int x=a;
    int y=b;
    int sum;

    sum = x+y;

    return(sum);
}

okay got it thanks again!
Topic archived. No new replies allowed.