Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

quick basic function question

Status
Not open for further replies.

gjpollitt

Programmer
Nov 8, 2005
4
GB
OK, can anyone please explain to me why the 'VolumeCube' function has to be declared before the 'int(main)' and also why 'int width=25' etc. as when the function is called the 'int width' variable is being passed 50 and not 25?

thanks
Graham

#include <iostream.h>

int AreaCube(int length, int width = 25, int height = 1);

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

area = AreaCube(length, width, height);
cout << "First area equals: " << area << "\n";

area = AreaCube(length, width);
cout << "Second time area equals: " << area << "\n";

area = AreaCube(length);
cout << "Third time area equals: " << area << "\n";
return 0;
}

AreaCube(int length, int width, int height)
{

return (length * width * height);
}
 
You need a prototype for all the functions in your program except main().

The width = 25 that you have in your prototype is a default value. In main() you change that to 50, so the function will use what is passed. If you did not pass width and height it would use the default values of width = 25 and height = 1.

As your program is written, the AreaCube() should use length = 100, width = 50 and height = 2.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
thanks for the quick reply
so would you still include your function prototypes before 'int main' even if there were 100 of them? Is there not a module similar to VB that you can add to your workspace/project whereby you can store your functions and/or prototypes?
Im sure I'll get the grasp of this soon, my mindset is VB so trying to move away from that.

thanks
 
Yes, there is a file. It is the header file (.h).

If for example your main file is MyProgram.cpp then you would put your 100 function prototypes and member variables in the MyProgram.h file.

I know what you mean about VB. I'm a long time VB programmer and have recently started to do C++ programming.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
I do not program in C because i hate it. Although i could mention something and hope i'm right.

- Missing here??
int AreaCube(int length, int width, int height)
{
return (length * width * height);
}


- As Hyper said, you need this line before the main. It is the prototype and it is required for the compiler.


- int AreaCube(int length, int width = 25, int height = 1);
This is the prototype. I don't think you can assign values. The right one is:
int AreaCube(int length, int width, int height);
or just
int AreaCube(int, int, int);


And one last: you write int main() {... return 0;}.
Do you use somewhere the '0' by the return? I would write:
void main() { ... }
 
You can actually get away without writing a prototype if each function is listed from top to bottom in the order that they are referenced.

In your example, simply put the main() function at the bottom, then you don't need the prototype.

But it's a good idea to get into the habit of putting your function (or class) declarations into .h files and their definitions (body) into .cpp files. Then include their .h files in the files where you need to use those functions/classes.
 
TipGiver,

The prototype for AreaCube is correct. It just means that the values given are default values. If nothing is passed into the function the function will use the default values.

cpjust is right about putting the functions before main AND before they are referenced to avoid using prototypes. But it may not always be possible. If function1() references function2() and function3(), but function3() references function4() which in turn references function2(), then you have a problem. In this case, you need to do prototypes.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
TipGiver said:
And one last: you write int main() {... return 0;}.
Do you use somewhere the '0' by the return? I would write:
void main() { ... }
void main() is not legal C++ (or C). It might work on your compiler, but using int main() is correct and preferred.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top