Answer to Question #350688 in C++ for lebo

Question #350688

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.

1
Expert's answer
2022-06-15T07:07:17-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS