Jump to content
  • Advertisement
Sign in to follow this  
CelticSir

C++ function error [Solved]

This topic is 3035 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

Hey

I been using functions in my learnings so the error is some what confusing me.

I get:
error C2660: 'test' : function does not take 0 arguments
error C2562: 'test' : 'void' function returning a value

I got my script from copying a tutorial and they used the same structure except i went a bit further and added two values together - assign that a variable then to try to return the value of that variable. But i've gone wrong some where. And no doubt its probably some thing obvious but i cannot see it.


#include <iostream>

void test(int, int);

int main(){
test(5,8);
std::cout << "The answer is: " << test() << std::endl;
}

void test(int iValue1,int iValue2){
int iSum;
iSum = iValue1 + iValue2;
return iSum;
}





[Edited by - thefollower on August 24, 2010 6:50:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
In order for a function to return a value it must be of a type, for instance


int value(const int& x, const int& y)
{
return x*y;
}



notice that void isn't present for the function type.

EDIT:

also you wouldn't call value(5, 8); than later for output you would do this,

cout << value(5, 8) << endl;

Share this post


Link to post
Share on other sites
I replaced it to this


#include <iostream>

void test(int, int);

int main(){
std::cout << test(5, 8) << std::endl;
}

void test(int iValue1,int iValue2){
return iValue1 + iValue2;
}




EDIT: do the function values it recieve have to be set as a constant?

But its not happy with the << before the function call.

Share this post


Link to post
Share on other sites
void functions do not return anything, there is your problem as I stated above. Since you are using the type int your function should of of type int. Read my above post and it will solve your problems.

If you wanted to use void you could use it like this,


void value(const int& x, const int& y)
{
int value = x + y;
std::cout << value << std::endl;
}

Share this post


Link to post
Share on other sites
By declaring your function as

void test(int, int)

you are telling the compiler that your function does not have a return value. You need to change the void to an int.

Share this post


Link to post
Share on other sites
Oh i see its working now - man that was much more complex than i was expect.

Is declaring function as INT or void a neceessity or can it dynamically just follow based on the code (i came from learn PHP where by you didn't need to set the function as a type you see so just wondered)

Thanks for the help!

Share this post


Link to post
Share on other sites
Quote:
Original post by thefollower
Oh i see its working now - man that was much more complex than i was expect.

Is declaring function as INT or void a neceessity or can it dynamically just follow based on the code (i came from learn PHP where by you didn't need to set the function as a type you see so just wondered)

Thanks for the help!


No c/c++ is explicit.

There are some tricks you can do later on with more advanced C++ but for right now just define things explicitly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!