Pass by Value Program

Can anyone help me with this code?
I try to compile it but it gives the errors message:[Error] cannot convert 'double (*)(double, double)' to 'double' for argument '3' to 'double Q(double, double, double)'

#include<iostream>
#include<conio.h>
using namespace std;
double Q(double m, double c, double T);
double T(double t1, double t2);
int main()
{
double m;
cout<<"enter mass:";
cin>>m;
double c;
cout<<"enter specific heat capacity";
cin>>c;
double t1;
cout<<"enter initial temperature:";
cin>>t1;
double t2;
cout <<"enter final temperature";
cin>>t2;
cout<<"Temperature change="<<T(t1, t2)<<endl;
cout<<"Heat energy ="<<Q(m, c, T)<<endl;
}//end function main

//function definitions
double T(double t1, double t2)//function header
{
return t2-t1;
}

double Q(double m, double c, double T)//function header
{
return m*c*T;
}
Last edited on
Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

20
21
cout<<"Temperature change="<<T(t1, t2)<<endl;
cout<<"Heat energy ="<<Q(m, c, T)<<endl;
T is a function, and you're trying to pass it to another function. You want to pass the value the function returns, not the function itself.

<conioh.> is a non-standard header and is deprecated, don't use it:
https://en.wikipedia.org/wiki/Conio.h
I change the code but still can't compile it...

#include<iostream>
using namespace std;
double Q(double m, double c, double T);
double T(double t1, double t2);
int main()
{
double m;
cout<<"enter mass:"<<endl;
cin>>m;
double c;
cout<<"enter specific heat capacity"<<endl;
cin>>c;
double t1;
cout<<"enter initial temperature:"<<endl;
cin>>t1;
double t2;
cout <<"enter final temperature"<<endl;
cin>>t2;
cout<<"Temperature change="<<T(t1, t2)<<endl;
cout<<"Heat energy ="<<Q(m, c, t1, t2)<<endl;
}

double Q(double m, double c, double t1, double t2)
{
return m*c*(-t1+t2);
}
Function Q takes three parameters, each of type double.
First you passed it two doubles and the name of a function.
Next you tried passing four variables instead of three.

There are two possible solutions, either store the result of calling T() in a separate variable, and use that new variable when calling Q(). Or put the complete call of the function "T(t1, t2)" as the third parameter.
Why does it says [Error] 'T' was not declared in this scope (Line 19)



1: #include<iostream>
2: using namespace std;
3: double Q(double m, double c, double t1, double t2);
4: void fun (int T);
5: int main()
6: {
7: double m;
8: cout<<"enter mass:"<<endl;
9: cin>>m;
10: double c;
11: cout<<"enter specific heat capacity"<<endl;
12: cin>>c;
13: double t1;
14: cout<<"enter initial temperature:"<<endl;
15: cin>>t1;
16: double t2;
17: cout <<"enter final temperature"<<endl;
18: cin>>t2;
19: cout<<"Heat energy ="<<Q(m, c, T)<<endl;
20: }
21: void fun(int T)
22: {
23: double t1;
24: cout<<"enter initial temperature:"<<endl;
25: cin>>t1;
26: double t2;
27: cout <<"enter final temperature:"<<endl;
28: cin>>t2;
29:
30: T=t2-t1;
31: return;
32: }
33:
34: double Q(double m, double c, int T)
35: {
36: return m*c*T;
37:
Please put your code between [code]your code here[/code] - it makes it so much easier to read.

Why does it says [Error] 'T' was not declared in this scope (Line 19)
Look through lines 1 to 18. do you see a variable named T defined anywhere?
Thanks... I manage to compile and run it...... But is this pass by value program? How do I change it to pass by reference?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
int Q(double m, double c, double t1, double t2);
int main()
{
	double m;
	cout<<"enter mass:"<<endl;
	cin>>m;
	double c;
	cout<<"enter specific heat capacity"<<endl;
	cin>>c;
	double t1;
	cout<<"enter initial temperature:"<<endl;
	cin>>t1;
	double t2;
	cout <<"enter final temperature"<<endl;
	cin>>t2;
	cout<<"The heat energy is:"<<Q(m, c, t1, t2)<<endl;
}

int Q(double m, double c, double t1, double t2)
{
	return m*c*(t2-t1);
} 
You're passing by value here.

You could change to pass by reference, but do you need the function to be able to modify the mass, heat capacity or temperatures? Or just calculate?
I need to use the function but I don't know to write the code...
What I mean is - you're using the function to calculate the heat energy with the numbers the user entered. Given the current use of the function - you don't need to pass variables by reference and give permission to the function to modify the numbers the user entered. Is the function supposed to do more than just calculate?
It just do calculation... How can I change it to pass by reference?
See the tutorial on functions. It explains pass by value and by reference:
http://www.cplusplus.com/doc/tutorial/functions/

In your own code, there is no particular reason to pass by reference, pass by value seems most suitable.
So, is this pass by reference?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
void input(double &, double &, double &, double &);
double Q (double, double,double,double);
void display (double);

int main(int arg, char *argv[])
{
	double m, c, t1, t2, temperature, heat;
	input (m, c, t1, t2);
	heat= Q(m, c, t1, t2);
	display (heat);
	return 0;
}
void input(double &m, double &c, double &t1, double &t2)
{
    cout<<"mass: "<<endl;
    cin>>m;
    cout<<"specific heat: "<<endl;
    cin>>c;
    cout<<"initial temp.: "<<endl;
    cin>>t1;
	cout<<"final temp.: "<<endl;
    cin>>t2;
}

double Q (double m, double c, double t1, double t2)
{
	double heat;
	heat= m*c*(t2-t1);
	return heat;
}
void display (double heat)
{
	cout<<"heat energy is= "<<heat<<endl;
}
That seems reasonably ok. Pass by reference in function input() where the values are changed, and pass by value in the other functions where the variables are unchanged.

(Though as mentioned in the tutorial, sometimes pass by reference is used where you don't want to change a variable).

Other comments, on line 11, a variable temperature is defined, but is never used. In C++ the include should be <cmath> instead of <math.h>






Thanks... I will edit the code...
Topic archived. No new replies allowed.