Write a function called sum, that calculates the total value of a and b, where a and b are integers, a= 100 and b = 200 in the main function. Your main function will declare your variables, call the function to add values and then call the function again. When you compile your program it has to give you total result of sum (a, b) from the main function, and the result for sum (a,b) from the function sum. There is a default value of b which is part of the function list, with the value 20. Your function sum, will calculate the result of a and b, and then return the result.
#include <iostream>
int sum(int a, int b, bool x);
int main()
{
int a = 100;
int b = 200;
std::cout << sum(a, b, true) << "\n";
std::cout << sum(a, b, false) << std::endl;
}
int sum(int a, int b, bool x) {
if (!x) {
b = 20;
}
return a + b;
}
Comments
Leave a comment