Help with Functions

So I am in a intro class for computer programming. I am not very good at working with multiple functions in a script. I was given this task this week relating to the code below.

"Rewrite the function trackVar so that rather than printing the value of z it sends its values back to the calling environment (main() function) and the calling environment prints the values of z.
Please write comments throughout the program to illustrate your understanding."

Below is the code in question. I have tried several attempts to do what I think is being asked of me to no avail. Including changing the function so it returns an output. Any input is greatly appreciated!

-Justin

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
#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std; 
 
void trackVar(double& x, double y); 
 
int main() 
{ 
    double one, two; 
 
    cout << fixed << showpoint << setprecision(2); 
    cout << "Enter two numbers: "; 
    cin >> one >> two; 
    cout << endl; 
 
    trackVar(one, two); 
    cout << "one = " << one << ", two = " << two << endl; 
 
    trackVar(two, one); 
    cout << "one = " << one << ", two = " << two << endl; 
 
 
    return 0; 
} 
 
void trackVar(double& x, double y) 
{ 
    double z; 
    z = floor(x) + ceil(y); 
    x = x + z; 
    y = y - z; 
 
    cout << "z = " << z << ", "; 
}
Last edited on
functions can return a type -- void means 'nothing returned'.

so
int foo()
{
return 11;
}

returns the value 11.
you can use that in a cout statement:
cout << foo() << endl;

everything you need is above. return a double from trackvar ... see if you can make it work.
Last edited on
Hello WakelessFoil,

Starting with:

"Rewrite the function trackVar so that rather than printing the value of z it sends1 its values back to the calling environment (main() function) and the calling environment prints the values of z.


1. Meaning returns the value.

From jonnin's example this would be: double trackVar(double& x, double y) for the function definition and the prototype would also have to change.

You may find this helpful https://www.learncpp.com/cpp-tutorial/introduction-to-functions/

Or the home page https://www.learncpp.com

That is something to start with. When you have made your changes post your code for discussion.

Andy
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
39
40
41
#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std;

double trackVar(double& x, double y); // < -- handy andy points out now a double return type. 

int main()
{
    double one, two;

    cout << fixed << showpoint << setprecision(2);
    cout << "Enter two numbers: ";
    cin >> one >> two;
    cout << endl;

    trackVar(one, two);
    cout << "one = " << one << ", two = " << two << endl;

    trackVar(two, one);
    cout << "one = " << one << ", two = " << two << endl;

    // the function trackVar now will return a double so you can use it as a variable like below

    cout << "trackVar = " << trackVar(one, two) << endl;

    return 0;
}

double trackVar(double& x, double y) // like others have said the return value is now  a "double" type instead of a "void"
{
    double z;
    z = floor(x) + ceil(y);
    x = x + z;
    y = y - z;

    // cout << "z = " << z << ", "; <-- Remove this as they want to you print from the main function
    // replace this removed code with the following

    return z; // This will send the value of z out of the function and back to the place where you made the function call.
}
Last edited on
Thanks to all that helped. I need to study user defined functions more!
Do you understand functions now, or are you still unsure?
Topic archived. No new replies allowed.