| | |
can anyone assiat in getting my function to work?
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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;
}
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.
You declared the function f1 as
but the definition specify that the function receive one parameter:
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:
C Syntax (Toggle Plain Text)
void f1 ();
C Syntax (Toggle Plain Text)
void f1 (int i) { ... }
As you say, the function has to return the difference between 2 variables, so it should have 2 parameters, and return an int value:
C Syntax (Toggle Plain Text)
int f1 (int a, int b);
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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!

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:
In main, it should be:
Clear enough?
When you declare a function, it should work like this:
C Syntax (Toggle Plain Text)
int f1 (int a, int b) { int res; res = a - b; return res; }
C Syntax (Toggle Plain Text)
int main () { int head, tail; ..... printf ("%d", f1 (head, tail)); }
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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....
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....
![]() |
Similar Threads
- tolower function does not work on certain letters (C++)
- fread() doesn't work when 're-used' in a for( ; ;) function (C)
- HideCaret() Windows API function doesn't work properly (C#)
- i can't seem to make my search function work.... help!... (C)
Other Threads in the C Forum
- Previous Thread: Template class
- Next Thread: Windows Main and Command Line Parameters
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi





