Area Cube

I cannot understand this coding. Can Someone please explain to me this program.

#include <iostream>
using namespace std;

int findarea(int length, int width = 20, int height = 12);

int main()
{
int length = 100;
int width = 50;
int height = 12;
int area;

area = findarea(length, width, height);
cout << "1st Area : " << area << "\n";

area = findarea(length, width);
cout << "2nd Area : " << area << "\n";

area = findarea(length);
cout << "3rd Area : " << area << "\n";

system ("pause");
return 0;
}


int findarea(int length, int width, int height)
{
return (length * width * height);
}
find area:

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:

findarea(100,50,12(default value));

understand?
This part is not understand

findarea(100,50,12(default value));

what can we use for default value? if we manually calculated what will be answers for 1st 2nd and 3rd? is it 60000,60000,24000?
I have developped a different program to find Area. What is the different between them and what is the best?

#include <iostream>
using namespace std;

int main()
{
double height;
double width;
double length;
double area;

cout << "Enter Height : ";
cin >> height;

cout << "Enter Length : ";
cin >> length;

cout << "Enter Width : ";
cin >> width;

area = height * length * width;

cout << "Area is : " << area << endl;

system ("PAUSE");
return 0;
}
The first one uses a "function" to calculate area, the second one does not.
#include <iostream>
using namespace std;

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);
}
Thank you
Topic archived. No new replies allowed.