Archived

This topic is now archived and is closed to further replies.

functions...

This topic is 5127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the difference between a function prototype and the actual function definition? Why are both needed because it seems in the below code that the mult function is defined below #include because my compiler doesnt get an error of undefined. The tutorial really doesnt explain the difference. It just states that both are needed. #include <iostream> int mult(int x, int y); int main() { int x, y; std::cout<<"Please input two numbers to be multiplied: "; std::cin>>x>>y; std::cout<<"The product of your two numbers is "<

Share this post


Link to post
Share on other sites
The declaration and definition are indeed both needed. The declaration tells the compiler what the function is called and what parameters will be passed, and that the function exists at all. The defintion tells the compiler how the function will be used, and indeed, what the function does.

Share this post


Link to post
Share on other sites
In C++ you can just put the whole function BEFORE main. If you want to put it after, you need to declare it before main, in specific, before you call it. You can also #include it RIGHT before you call it in main.


--
You're Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

[edited by - Pipo DeClown on November 29, 2003 12:28:50 PM]

Share this post


Link to post
Share on other sites
If you plan on having your function used in many source files... it is also wise, not to put it in a header file, as each file its used in, will end up with its own copy of the function. Instead, placing a declaration in that header, and the definition in a corresponding source file will result in a single version of the function to be created, and shared by all files including the header. The method of placing functions in order (as the declaration is implicitly in the definition) works to an extent, but if for example two functions have a cyclic dependancy, this is not possible... in such a case, a declaration must also be used.

Share this post


Link to post
Share on other sites
Thanx for your responses, but i have another question.

#include <iostream.h>
#include <conio.h>
int mult(int x, int y)
{
std::cout<<"Please enter two numbers: "< std::cin>>x>>y;
std::cout<<"Your total is: "< system ("PAUSE");

}

int add(int x, int y)
{
std::cout<<"Please enter two numbers: "< std::cin>>x>>y;
std::cout<<"Your total is: "< system ("PAUSE");
}

float divide(int x, int y)
{
std::cout<<"Please enter two numbers: "< std::cin>>x>>y;
std::cout<<"Your total is: "<<(x/y)< system ("PAUSE");
}

int sub(int x, int y)
{
std::cout<<"Please enter two numbers: "< std::cin>>x>>y;
std::cout<<"Your total is: "< system ("PAUSE");
}

int main()
{


int input,x,y;
float a,b;

std::cout<<"1. Multiply two numbers."< std::cout<<"2. Add two numbers."< std::cout<<"3. Divide two numbers."< std::cout<<"4. Subtract two numbers."< std::cin>>input;


switch (input)
{
case 1: mult(x, y);
break;
case 2: add(x,y);
break;
case 3: divide(x,y);
break;
case 4: sub(x,y);
break;
default: std::cout<<"Error. Invalid choice. Terminating..."< system("PAUSE");
return 0;

}

return 0;
}

On the float divide function, it wont return with a float for its value. i can make it return a float by changing the variables declared in the function to float a, float b, but i dont really want to do that if its possible. Thanx. dmounty: wouldn''t it just be easier to place the declaration in the code if ur going to be putting the definition there anyways. I dont see how it would make sense to make a seperated header file just for one line of code. (NOTE: I do not know what i am talking about, just guessing...)

Share this post


Link to post
Share on other sites
just have your div function

return static_cast <float>(a) / b;


And change your iostream.h to just #include <iostream>





--{You fight like a dairy farmer!}

[edited by - Greatwolf on November 30, 2003 5:28:24 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Greatwolf
just have your div function return static_cast(a) / b;

And change your iostream.h to just #include <iostream>





--{You fight like a dairy farmer!}


This is wrong because the function will cause INTEGER division to occur. Not what is wanted. The way to do it is thus:


float divide(int x, int y)
{
return x/(float)y;
}


I think you have become a little confused with the structure of your code since you are duplicating all the input retrieval in the functions and not using the parameters i.e. you could remove the parameters altogether the way you are currently doing it.

A way to do it neatly would be to use a function pointer for the arithmetic operation set in the switch function. And then just called as an input to the output stream as in the earlier version with just multiply.

James

Share this post


Link to post
Share on other sites
quote:
Original post by jamessharpe

This is wrong because the function will cause INTEGER division to occur. Not what is wanted. The way to do it is thus:


float divide(int x, int y)
{
return x/(float)y;
}




Actually what I presented is the same as yours. The stupid html tags ruined my code and I forgot to double check it. my bad

Anyway, it''s fixed now





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need a function prototype for the same reason you add header files to your project, the compiler needs to know that a function exists. I think C adds the prototypes at runtime and in C++ you need to add them for stronger type checking.

I prefer to use function prototypes mainly because I can use a function before defining it.

If you provide a definition before main() you don''t need to provide a prototype.

When you include a file such as <iostream>, basically you are including all the prototypes for the Input/Output stream of the standard library.

Share this post


Link to post
Share on other sites