Fix a function so that it really changes the caller's variable.
What to build
A function that changes a variable through a reference.
Requirements
- Change
add_bonusso thatmarksinmainis really changed. - Do not change
main.
Input
One line with the marks and the bonus.
Output
One number.
Constraints
Each number is from 0 to 1000.
Example
Input:
70 5
Output:
75
Why it works
Without the &, the function changes its own copy, and that copy is discarded when it returns.
Examples
Example 1 · Input
70 5
Expected output
75
Example 2 · Input
0 0
Expected output
0
Hints
Hint 1
Add & after the type of the parameter.
Hint 2
The rest of the function stays the same.
Solution
Show one correct solution (try it yourself first)
#include <iostream>
using namespace std;
void add_bonus(int& marks, int bonus) {
marks += bonus;
}
int main() {
int marks, bonus;
cin >> marks >> bonus;
add_bonus(marks, bonus);
cout << marks << endl;
return 0;
}