question archive Using functions and reference variables (and default variables) Make a program that sends a person's hitpoints into the function
Subject:Computer SciencePrice:2.89 Bought3
Using functions and reference variables (and default variables) Make a program that sends a person's hitpoints into the function. Normally it will subtract 1 point, but once in a great while it will subtract 5 points. (So, hitpoints is changing in the function)
Please find below code.
For the function, hitpoint is passed by reference from main function. It also have default value of great=0, which indicates that it is not a great while scenario
#include <iostream>
using namespace std;
void CalcHitpoint(int &hitpoint, int great=0)
{
hitpoint = great==1? hitpoint-5:hitpoint-1; // if great=1, then it is great while scenario, reduce hitpoint by 5
}
int main()
{
int hit=0;
for (int i=100; i>=1;i-=10)
{
int hits =i;
if (i >40 && i < 60)
{
CalcHitpoint(hits,1);
cout << "Function returned value for " << i << " hitpoints " << hits <<endl;
}
else
{
CalcHitpoint(hits);
cout << "Function returned value for " << i << " hitpoints " << hits <<endl;
}
}
return 0;
}