tracing functions inside of functions

Oct 19, 2016 at 3:16am
Hi, I need help tracing how to get the value for y in the main function below. I understand how to get x and z. the output is

x = 12
y = 108
z = 8

I assumed to get the individual values that where inside the function, then add them, and then send that value to the function Times3(double y). However, that's wrong, so here is how I traced it;

y = Times3(Times2(4.0) + Times2(6.0)) = Times3(8 + 12) = Times3(20) = 60

I was wondering what order or way do I go about tracing this?

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
#include <iostream>

using namespace std;

double Times2(double);
double Times3(double);

int main()
{
	double x = 4.0, y = 6.0, z;
	
	z = Times2(x);
	x = Times3(x);
	y = Times3(Times2(x) + Times2(y));
	
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "z = " << z << endl;
	
	return 0;
}

double Times2(double x)
{
	double y = 2*x;
	return y;
}
double Times3(double y)
{
	double x = 3*y;
	return x;
}
Oct 19, 2016 at 3:56am
Never mind I just looked at it again and realized x's value changed. so my approach was right just used wrong x value.

anyone who read this and didn't know either; x's value changes to 12 so,

y = Times3(Times2(12 NOT 4) + Times2(6)) = Times3(24 + 12) = Times3(36) = 108
Last edited on Oct 19, 2016 at 3:56am
Topic archived. No new replies allowed.