Archived

This topic is now archived and is closed to further replies.

Functions

This topic is 5971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey Can someone help me with the following code . it generates no errors or warnings but displays the wrong results. thanks ///////////////code #include #include int getal1; int getal2; int uitkomst; int resultaat; int optellen() { return uitkomst = getal1 + getal2; } int main() { cout<< "geef getal 1 in.\n"; cin>> getal1; cout<< "geef getal 2 in.\n"; cin>> getal2; int optellen(); resultaat = uitkomst*2; cout<< resultaat << "\n"; return 0; }

Share on other sites
I don''t know exactly what you''re looking for, but here''s an educated guess (correction):
  int getal1;int getal2;int uitkomst;int resultaat;void optellen(){ uitkomst = getal1 + getal2; }int main(){ cout << "geef getal 1 in.\n"; cin >> getal1; cout << "geef getal 2 in.\n"; cin >> getal2; optellen(); resultaat = uitkomst*2; cout << resultaat << "\n"; return 0;}

Remember, just because it compiles and links, doesn''t mean it works the way you want it to .

[Resist Windows XP''s Invasive Production Activation Technology!]

Share on other sites
Hi!

There seems to be a missundarstanding concerning functions in C. Here code that should work (not tested, not my style, but your code with minimal changes:
int getal1;int getal2;int uitkomst;int resultaat;int optellen(){  return getal1 + getal2; }int main(){  cout<< "geef getal 1 in.\n";  cin>> getal1;  cout<< "geef getal 2 in.\n";  cin>> getal2;  uitkomst = optellen();  resultaat = uitkomst*2;  cout<< resultaat << "\n";  return 0;}

However, I would pass getal1 and getal2 as argument to the function.
Hope that makes functions little bit clearer.

Share on other sites
The specific problem is the line: "int optellen();" in main. You might think that you are calling the optellen function, but by placing int in front, you are actually declaring a new variable called optellen that is an int. Basically, as the others have suggested, you remove the int and it''ll call your function.

You''ve ran in to two major causes of confusion in C++, (1) more than one variable or function can have the same name as long as they are at different scopes (in this case, the global function optellen, and the local variable optellen), and (2) variable declarations and function declarations can look exactly the same due to how constructors are called and the designers'' strange decision to extend this syntax to all variable types.

Share on other sites
quote:
Original post by Anonymous Poster
The specific problem is the line: "int optellen();" in main. You might think that you are calling the optellen function, but by placing int in front, you are actually declaring a new variable called optellen that is an int.

Actually, in this case, int optellen(); is a forward declaration that a function called optellen exists and takes no parameters and returns an int.

Share on other sites
If you want to use global variables, you need to change two lines of code.

First:
return uitkomst = getal1 + getal2;
needs to be
uitkomst = getal1 + getal2;
The return is not needed because you are using global identifiers.
Second:
int optellen();
should be changed to
optellen();
this way it is actually called as a function.

Hope that helped explain what happpened.

• Forum Statistics

• Total Topics
628642
• Total Posts
2983998

• 9
• 9
• 10
• 21
• 20