C++ function error [Solved]

Started by
7 comments, last by boogyman19946 13 years, 8 months ago
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]
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;
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.
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;}
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.
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!
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.

From what i have learnt so far the way to do that is to use a reference?
I believe empirical was referring to templates, but leave those until it's time. It's not necessary to know those just yet.

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement