1. ## Need simple help please!!

Hey guys!
Could you please tell me what the difference is between these two programs?

please explain in depth what the first program does step by step...

Code:
```// Listing 5.1 - demonstrates the use of function prototypes

#include <iostream.h>
int FindArea(int length, int width)

int main()
{
int lengthOfYard;
int widthOfYard;
int areaOfYard;

cout << "\nHow wide is your yard? ";
cin >> widthOfYard;
cout << "\nHow long is your yard? ";
cin >> lengthOfYard;

areaOfYard= FindArea(lengthOfYard, widthOfYard);

cout << "\nYour yard is ";
cout << areaOfYard;
cout << " square feet\n\n";
return 0;
}

int FindArea(int l, int w)
{
return l * w;
}```
I have written the next program my self, same output:

Code:
```#include <iostream.h>
#include <stdlib.h>
int main()
{
int l, w, a;
cout << "How wide is your yard?\n";
cin >> w;
cout << "How long is your yard?\n";
cin >> l;
a = (l*w);
cout << "Your yard is:" << a << "Square Feet\n";
system ("pause");
return 0;
}```
My code is A LOT shorter that the book's code, and it's even easier to understand!

I dont understand anything about how the first program works, please explain to me!

(I need to proceed to other chapters of the book)

2. My code is A LOT shorter that the book's code, and it's even easier to understand!
True.

I dont understand anything about how the first program works, please explain to me!
Which is why the first program is written the way it is. It's meant to teach you a bit about functions and good program design.

Why do you need to be explained the first program when it's practically the same as the second program? The only differences are that you calculate the area in the main function, and the program calculates it in a seperate function, FindArea(). (And you have a system("pause");, but that's not important.)

3. ## What the?

Ok, It got a little more clear in my mind after your reply...

But how the hell does the program work ?
when I enter the width and the length, how does the program deal with the data?
does it go to line 17 then 25 then 19?

I want to know how the program work !

4. Well, there are no line numbers so I can't tell you for sure, but the program reads in the users input, then calls the function FindArea(), (i.e. transfers control to the start of the FindArea() function), which calculates and returns the area, (i.e. transfers control back to the calling routine). It then write it out.

A function is a piece of code which can be called from other places in your program, often from several different places. It breaks your code up into smaller pieces.

With a 20-30 line program that is not terribly important, but believe me, when you have millions of lines, you will not want to start at the beginning each time and search for the bit you need to fix.

5. at this point:

Code:
`areaOfYard= FindArea(lengthOfYard, widthOfYard);`
the code for the FindArea function is called along with the values of lengthOfYard and widthOfYard, so you will get this:

Code:
```int FindArea(int l, int w)
{
return l * w;
}```
with l == lengthOfYard and w == widthOfYard

The int part that I've highlighted is the return type from the function. This means the function has to return to the point where it was called with an int value. FindArea returns the value of l * w, so now we get :

Code:
`areaOfYard = l*w`
I hope thats sort of clear, all you are really doing is replacing "FindArea" with the code specified in the FindArea function.

6. the first program takes advantage of functions.

treat FindArea(lengthOfYard, widthOfYard) kind of like a shortcut.
when the computer sees it, it goes looking for how to handle it.

the shortcut is defined outside of the main loop, at the bottom. how it works is this:

int FindArea(int l, int w) /*this names the function and shows what variable goes in it. the 'int' at first tells what kind of variable the function will return (in this case, when it solves for area, the answer will be an int, so it returns an int). the int l and int w in parenthesis tell what will be put into the function to be used (in this example, the function is solving for area using length and width, so you want to put the length and width from your program into the function). */

{
return l * w; /*this is where the work is done. your function multiplies l by w and sends that answer back to your main loop. Ideally, functions would hold large amounts of data that would be inconvenient to have to write over and over again in your program, so you put it all in a function and just use the function in your program rather than duplicating the code every instance its used. */
}

so in your main, you want it to multiply length times width, so you put whatever variable is holding length and width into the function, in this case lengthOfYard and widthOfYard.

7. ## WOW

Oh thank you very very much guys... all of you who replied!

you are the kind of people I can rely on when I stock somewhere!

You made it simple for me, and explained it very well, better than the crappy book...

Thanks again everyone!