Solving C++ Function Calling Error in Code

  • Context: C/C++ 
  • Thread starter Thread starter keith03
  • Start date Start date
  • Tags Tags
    C++ Function
Click For Summary

Discussion Overview

The discussion revolves around a C++ function calling error encountered by a participant while trying to implement a program that adds two numbers and computes the square root of the result. The conversation includes code snippets, error messages, and suggestions for correcting the code, focusing on function prototypes, variable assignments, and naming conventions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports a compilation error stating "term does not evaluate to a function taking 1 (or two) arguments" when calling the function Add_num.
  • Another participant suggests that the error is due to the missing function prototype for Add_num.
  • Some participants point out that the assignments N1=N1_n and N2=N2_n are incorrect and should be reversed to N1_n=N1 and N2_n=N2.
  • A later reply identifies that Add_num is declared as a variable instead of a function, which contributes to the error.
  • Concerns are raised about using the same name for a function and a variable, which can lead to confusion.
  • One participant shares a corrected version of the code that includes proper function declarations and variable assignments.
  • Another participant expresses a desire to understand how to properly implement functions and asks for clarification on making a function instead of a variable.
  • A participant indicates they have resolved the issue independently but seeks confirmation that their solution is correct and discusses the idea of creating a stub program for testing.

Areas of Agreement / Disagreement

Participants generally agree on the issues present in the original code, particularly regarding function declarations and variable assignments. However, there is no consensus on the best approach to implement the desired functionality, as multiple suggestions and corrections are offered.

Contextual Notes

Limitations include the potential for misunderstanding regarding function prototypes and variable scope, as well as the implications of naming conventions in C++. Some participants express uncertainty about the correct implementation of functions and the overall structure of the program.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in function implementation, debugging, and code structure. This discussion may also benefit those encountering similar compilation errors in their code.

keith03
Messages
31
Reaction score
0
Hello,

I have been pretty good with this stuff...but this is the umteenth time I have run into this error. I have gone around it by just using the direct function in the code, but I really really want to master this function calling. My compilation error is always term does not evaluate to a function taking 1 (or two) arguments. Where am I going wrong? I did the current function that comes up with the error by the book. literally. Here is the code. The Error comes up at the Add_num (N1_n,N2_n).


#include<iostream>
#include<cmath>
#include<math.h>
#include"Formulas.h"
double SQRT_NORM (double a);

using namespace std;

int main ()

{
bool again = false;
do
{
double N1, N2, N1_n, N2_n, Newz, Add_num, USR_SQRT;
char choice;


count<<"Enter a first of two positive numbers:"<<endl;
cin>>N1;
N1=N1_n;
count<<"Enter the second number of two positive numbers:"<<endl;
cin>>N2;
N2=N2_n;


Add_num (N1_n,N2_n); HERE IS WHERE THE ERROR IS REPORTED

SQRT_NORM (Newz);

count<<"The Magnitude of the complex number is:"<<endl;


count<< "\n Would you like to run the program again?";
cin>> choice;


if (choice == 'y')
{
again = true;
system ("cls");
}
else
again = false;
} while (again);
return 0;

}

//Below are the formulas used in this lab

double Add_num (double a, double b)
{
double Add_num;
Add_num = a+b;
return Add_num;
}

double SQRT_NORM (double a)

{
double SQRT_NORM;
SQRT_NORM = sqrt (a);
return SQRT_NORM;
}
 
Technology news on Phys.org
It's missing the function prototype

double Add_num (double a, double b);
 
It's been a while since I've done C++ but, these look backwards to me:

N1=N1_n;
N2=N2_n;
 
keith03 said:
Hello,

I have been pretty good with this stuff...but this is the umteenth time I have run into this error. I have gone around it by just using the direct function in the code, but I really really want to master this function calling. My compilation error is always term does not evaluate to a function taking 1 (or two) arguments. Where am I going wrong? I did the current function that comes up with the error by the book. literally. Here is the code. The Error comes up at the Add_num (N1_n,N2_n).


Code:
#include<iostream>
#include<cmath>
#include<math.h>
#include"Formulas.h"
double SQRT_NORM (double a);

using namespace std;

int main ()

{
   bool again = false;
   do
   {
   double N1, N2, N1_n, N2_n, Newz, Add_num, USR_SQRT;
   char choice;


   cout<<"Enter a first of two positive numbers:"<<endl;
   cin>>N1;
   N1=N1_n;
   cout<<"Enter the second number of two positive numbers:"<<endl;
   cin>>N2;
   N2=N2_n;
   

 [B]  Add_num (N1_n,N2_n);[/B] HERE IS WHERE THE ERROR IS REPORTED
  
   SQRT_NORM (Newz);

   cout<<"The Magnitude of the complex number is:"<<endl;
   

   cout<< "\n Would you like to run the program again?";
   cin>> choice;


   if (choice == 'y')
	{
		again = true;
		system ("cls");
	}
	else
		again = false;
	} while (again);
	return 0;

}

//Below are the formulas used in this lab

double Add_num (double a, double b)
{
	double Add_num;
	Add_num = a+b;
	return Add_num;
}

double SQRT_NORM (double a)

{
	double SQRT_NORM;
	SQRT_NORM = sqrt (a);
	return SQRT_NORM;
}
Please note how I embedded in code blocks. You can do this manually or use the handy code block icon that is just above the window in which you type a post.

Several problems here:
(1) You declared Add_num as a variable, not a function. This is the cause of the immediate problem.

(2) Even if you had declared Add_num correctly, your use of the function would accomplish *nothing*. The results are not being assigned to anything.

(3) The same goes for the use of SQRT_NORM.

(4) Nothing of interest is output.

(5) Using the same name of a function and the name of a variable inside the function is a very bad idea.

(6) Using ALL_CAPS for a function name is, in most places, strongly discouraged or even forbidden.
 
Borg is right. You are attempting to assign an left-hand-value to an right-hand-value.

The assignment goes the other way.

Use N1_n = N1;
and N2_n = N2;
 
I could really use some help by seeing this done properly. I will then be able to match some verbage up too. I found the lack of prototype as soon as I posted. How do I make this a function, and not a variable? Please bear with the lack of understanding.
 
This should work, more or less

Code:
#include<iostream>
#include<cmath>

using namespace std;

double SQRT_NORM (double a);
double Add_num (double a, double b);

int main ()
{
   bool again = false;
   do
   {
   double N1, N2;
   char choice;   cout<<"Enter a first of two positive numbers:"<<endl;
   cin>>N1;
   cout<<"Enter the second number of two positive numbers:"<<endl;
   cin>>N2;
   

   double sum = Add_num (N1,N2);
  
   double root = SQRT_NORM (sum);

   cout<<"The sum is:"<< sum << ", its square root is: " << root << endl;
   
   cout<< "Would you like to run the program again?";
   cin>> choice;   if (choice == 'y')
	{
		again = true;
		system ("cls");
	}
	else
		again = false;
	} while (again);
	return 0;

}

//Below are the formulas used in this lab

double Add_num (double a, double b)
{
	double var;
	var = a+b;
	return var;
}

double SQRT_NORM (double a)
{
	double var;
        var= sqrt (a);
	return var;
}
 
That is it! I figured it out on my own, but it is good to see that I didnt just tmake it work by some improper loophole. I have the whole thing finished now. The last part is to write a stub program. I have never done that before. I am assuming that I could give the user an option for a "self test"? I am assuming that I would take a value, run it through the program, reverse it, and hope that the origonal value is returned?
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
6
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K