Actually, scratch that, I was so focused on the return value from the score function that I didn't see that you actually print the value of total
The reason is the order of evaluation of parameters within an expression. The language does not specify the order in which sub-expressions within an expression are evaluated. In this case, you have two sub-expressions that causes the problem:
- The value of the function call to score.
- The value of total.
The language does not specify the order in which these two sub-expressions are evaluated. If the value of total is evaluated before the function call, then the value of total results in zero, and then the function is called, setting it to its new value. In essence, you have two expressions where one (the value of total
) depends on the other (the function call), and where the one expression has a side effect on the other expression. Your program is not well formed.
You need to call the function and grab its return value before printing it to ensure that the function is actually called before printing the value of total
int total = 0;
int myscore = score(sol, total);
cout << myscore << "/" << total;