int Length holds the length, this variable is mandatory because it is given no initial value. The variables width and height are optional, if no values or given to the function they will pass 20 - 12 by default. The function returns the value of L * W * H, or the volume of a square(not area).
int main:
4 variables are declared, Length Width and Height are pretty self explanatory. Area will hold the value the function findarea will return.
area = findarea(length, width, height);
area is set equal to the value computed with (100, 50, 12)
area = findarea(length, width);
since height is optional, a value of 12 is set by defualt. this is the same as:
int findarea(int length, int width = 20, int height = 12);/*the 2nd and 3rd values have default parameters set so if the are not passed, it assumes they are passed with these values.*/
int main()
{
int length = 100;
int width = 50;
int height = 12;
int area;
area = findarea(length, width, height);//Passes all parameters so the default ones are not used
cout << "1st Area : " << area << "\n";
area = findarea(length, width);//Passes two parameters so the function uses the 3rd one as the default set since it is not passed
cout << "2nd Area : " << area << "\n";
area = findarea(length);//Passes one parameter so the other two uses the default
cout << "3rd Area : " << area << "\n";
system ("pause");
return 0;
}
int findarea(int length, int width, int height)
{
return (length * width * height);
}