944,010 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2220
  • C RSS
Dec 21st, 2004
0

can anyone assiat in getting my function to work?

Expand Post »
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


Quote ...
#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;

}

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.










Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 21st, 2004
0

Re: can anyone assiat in getting my function to work?

You declared the function f1 as
  1. void f1 ();
but the definition specify that the function receive one parameter:
  1. void f1 (int i)
  2. {
  3. ...
  4. }
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:
  1. int f1 (int a, int b);
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Dec 21st, 2004
0

Re: can anyone assiat in getting my function to work?

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

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!
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 21st, 2004
0

Re: can anyone assiat in getting my function to work?

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:
  1. int f1 (int a, int b)
  2. {
  3. int res;
  4. res = a - b;
  5. return res;
  6. }
In main, it should be:
  1. int main ()
  2. {
  3. int head, tail;
  4. .....
  5. printf ("%d", f1 (head, tail));
  6. }
Clear enough?
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Dec 21st, 2004
0

Re: can anyone assiat in getting my function to work?

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....
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 21st, 2004
0

Re: can anyone assiat in getting my function to work?

sorry about the double post but it seems we cant edit them!! Well i got it working by using void!
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Template class
Next Thread in C Forum Timeline: Windows Main and Command Line Parameters





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC