• Advertisement

Archived

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

Help With Functions

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m very new to C++, and I''m having some problems that I can''t solve by looking in my text book. I can''t figure out what''s wrong with my simple program. Here''s the source code:
  int doSomething(int num1, int num2); //function prototypes


int main(int argc, char* argv[])
{
  int num1, num2;

  cout << "Enter number 1: ";
  cin >> num1;
  cout << "Enter number 2: ";
  cin >> num2;
  cout << endl;

  int ans = doSomething(num1, num2);         //assigns what

                                             //the function returns

  cout << "The answer is: " << ans << ".";   //to ans

  getch();

  return 0;
}

int doSomething(int num1, int num2)
{
  return num1(sqrt(num2))+(num1+num2);
}  
When I try to compile this, my compiler gives me the error "[C++ Error] Learning1.cpp(34): E2314 Call of nonfunction." Any help here? I''ve never come across that error before and I don''t know how to fix it.. Thanks! -Kevinator

Share this post


Link to post
Share on other sites
Advertisement
The compiler thinks:

''return num1(sqrt(num2))+(num1+num2);''

is a function call. Remove the ''num1'' bit and it should ''do something''

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I tried it out on mine and all it says is that getch() and sqrt() are undecalred. What are you using to compile?

Share this post


Link to post
Share on other sites
Your problem is this line:


return num1(sqrt(num2))+(num1+num2);


You are trying to use num1 as if it were a function, which it clearly isn''t. You probably mean


return num1*(sqrt(num2))+(num1+num2);

Share this post


Link to post
Share on other sites
In:

return num1(sqrt(num2))+(num1+num2);

It thinks you''re calling a function called num1();... Look at how you have it set up. You need some sort of operator between num1 and the opening paren ''(''. That''ll fix it. ex:

return num1*(sqrt(num2))+(num1+num2);

and you don really need the parens and could just use:

return num1*sqrt(num2)+num1+num2;


Gamer-Insight.com

Share this post


Link to post
Share on other sites
Hmm, I added a ''*'' after the first num1 and that did the trick. BTW I''m using Borland C++Builder..

Share this post


Link to post
Share on other sites
Wow, I go to type my reply and 5 other people reply in the mean-time. Thanks for all the help.

Share this post


Link to post
Share on other sites
BTW, to the AP: include < math.h >, this is where sqrt () is defined, and include < stdio.h > for getch ().

[edited by - Melraidin on June 6, 2002 10:34:44 AM]

Share this post


Link to post
Share on other sites
No its not...

_________________________________________________________________________
Can someone be nice and help me on my way to be the next Hideo Kojima?
Thought So...

Share this post


Link to post
Share on other sites

  • Advertisement