Public Group

# C++ function error [Solved]

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

## 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 on other sites
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 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 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 on other sites
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 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 on other sites
Quote:
 Original post by thefollowerOh 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 on other sites
From what i have learnt so far the way to do that is to use a reference?

##### Share on other sites
I believe empirical was referring to templates, but leave those until it's time. It's not necessary to know those just yet.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×