We were told that there is a major error with this code but it still manages to run. I know it has something to do with passing by reference and the pointers but it seems very messy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
usingnamespace std ;
float* calculatePercentage ( float mark , float total )
{
float percentage = mark / total *100;
return &percentage ;
}
int main ()
{
float* percent = 0;
percent = calculatePercentage (1 ,10);
cout << " The percentage is " << *percent << " % " << endl ;
return 0;
}
As MiiNiPaa has said, the function returns a pointer to a variable which ceases to exist once the function terminates. Either pass by reference or return by value (or reference).