can someone explain why output is -2 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can someone explain why output is -2 ?

#include <iostream> #include <string> #include <math.h> using namespace std; int func(int a=2,int b =5){ return b-a; } int main(){ int k=7; cout<<func(k); }

3rd Apr 2019, 12:38 PM
San Anemos
San Anemos - avatar
1 Answer
+ 7
int func(int a=2,int b =5) In this line, we have just given the default values to both the variables, if while calling the function, any value is not given, only then these values will be considered. cout << func(k); Here, we have only passed 7, which will be considered as a's value. As we haven't passed the value of b, default value shall be considered. So here, a=7 and b=5 b-a = 5 - 7 = -2
3rd Apr 2019, 1:10 PM
Letsintegreat
Letsintegreat - avatar