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 <iostream>
#include <ctime>
#include <cstdlib>
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;
}

Edited 3 Years Ago by happygeek: fixed formatting

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 of void 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);

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!

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?

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....

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

This article has been dead for over six months. Start a new discussion instead.