Archived

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

page211

Functions

Recommended Posts

page211    122
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 this post


Link to post
Share on other sites
Null and Void    1088
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 this post


Link to post
Share on other sites
VolkerG    151
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest 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. 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 this post


Link to post
Share on other sites
Kylotan    9864
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 this post


Link to post
Share on other sites
conundrum07    122
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.

Share this post


Link to post
Share on other sites