Converting a value-returning function to a void function

How would I convert this statement into a void function? I cannot use a cout statement in my void function.

double Test (double x, double y, double z)
{
if ( x > y || y > z )
return 0.5;
else
return -0.5;
}

Once again, thanks in advance.
Last edited on
If you code in c++ , you can use pointers and references.
If you code in C , you can use pointers only.
Last edited on
Dufresne is right. Pass 4 parameters to function "test", one of them is a pointer. consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream.h>
#include<conio.h>

void test(double,double,double,double*);
main()
{
      double x=.9;                      //First variable
      double y=.95;                     //2nd variable
      double z=1.0;                     //3rd variable
      double ans;                       //Variable indicating x,y,z status
                                        //Modified by "test" using pointer
      test(x,y,z,&ans);                 //Call to function
      
      cout<<ans;
      getch();
}
void test(double x,double y,double z,double *ansptr)
{
    if ( x > y || y > z )
    *ansptr=.5;                         //Actually modifying "ans" in main()
    else
    *ansptr=-.5;                        //Actually modifying "ans" in main()
}
That example is full of bad C++ ...
Here, fixed:

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
#include <iostream>                     // <iostream>, not <iostream.h>
                                        // conio.h removed (deprecated, nonstandard)
using namespace std;                    // if using namespace std, then say so

void test(double,double,double,double*);

int main()                              // main must return an int
{
      double x=.9;
      double y=.95;
      double z=1.0;
      double ans;

      test(x,y,z,&ans);
      
      cout<<ans;

      cout << "\nPress Enter to continue\n";  // replaced conio's _getch() with
      cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );  // std solution

      return 0;                               // return an int
}

void test(double x,double y,double z,double *ansptr)
{
    if ( x > y || y > z )
    *ansptr=.5;
    else
    *ansptr=-.5;
}
Last edited on
Excellent. Thanks for the help. I am taking an online C++ class and it sucks because the teacher isn't around to answer questions so I am left with bugging you guys. Thanks again for the help and I am sure I will be posing more stuff.

Also, how can I get better search results when I search the forums. I feel like I get ALOT of junk that is of no use. Thanks.
Thanks bazzy and Disch.
BTW the true C++ way is by reference ( as Dufresne said )
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
#include <iostream>
using namespace std;

void test(double,double,double,double&);

int main()
{
      double x=.9;
      double y=.95;
      double z=1.0;
      double ans;

      test(x,y,z,ans);
      
      cout<<ans;

      cout << "\nPress Enter to continue\n";
      cin.ignore( numeric_limits<streamsize>::max(), '\n' );

      return 0;
}

void test(double x,double y,double z,double &ansref)
{
    if ( x > y || y > z )
        ansref=.5;
    else
        ansref=-.5;
}
Last edited on
Topic archived. No new replies allowed.