954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

can anyone assiat in getting my function to work?

Hello from what i can understand about function is that you need the prototype , then you need some kind of call function in the program and then the delcared function. Well I've done that but it doesnt want to work :(

#include #include #include

using namespace std; // for std::cin, std::cout etc. void f1(); int main() {

int cointoss, heads = 0, tails = 0; srand(time(0));

for (int counter = 0; counter <= 10; counter++) { cointoss = 1 + rand() % 2; if (cointoss == 1) { cout << "Heads" << endl; heads++; } else { cout << "Tails" << endl; tails++; } } cout << "there are a total of " << heads << " heads"; cout << " and " << tails << " tails" << endl;

f1();

cin.get(); // wait return 0; }

void f1(int i) {

return heads - tails;

}

basically the function is ment to take the total of Heads and takewaya the total tails, stored at int tails and int heads but it doesnt want to excuted!

Would be reatful for assistance.

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

You declared the function f1 as

void f1 ();

but the definition specify that the function receive one parameter:

void f1 (int i)
{
...
}

And, also, the return value is ofvoid type.

As you say, the function has to return the difference between 2 variables, so it should have 2 parameters, and return an int value:

int f1 (int a, int b);
frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

thanks for the reply so if your trying to make a protoype that takes more than 1 argument you must tell it? And if your returning something to the main() you should also have "int" . I guess I'm learning :D

However in my prototype i have heads - tails, which should get the values from the head - tail in the int main() but instead i get a compile error saying head tail not declared!

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

You declared the head and tail variables inside of main function; they are not available for the f1 function.
When you declare a function, it should work like this:

int f1 (int a, int b)
{
int res;
res = a - b;
return res;
}

In main, it should be:

int main ()
{
int head, tail;
.....
printf ("%d", f1 (head, tail));
}

Clear enough?

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

hello, ok could i make a prototype that took the values stored at int heads and tails, if i made them both global not local? that way could i just put the little calculation in the prototype?

like

int heads;
int tails;

int main()
int toss;

....make a statment that randomizes input to toss and stored the results at toss, and incrmements heads or tails depending on the value (1 or 2). is this possible since its outside the local varibles....

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

sorry about the double post but it seems we cant edit them!! Well i got it working by using void!

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You