Archived

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

Kevinator

Help With Functions

Recommended Posts

Kevinator    229
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
Carrot    136
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   
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
Sandman    2210
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
powdahound    158
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
Melraidin    128
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