Thread: How to get rid of an unwanted function return value?

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    How to get rid of an unwanted function return value?

    Hello. The function square() in my program is returning a "0"
    after each time it is called. I understand why it is doing it,
    but I don't know how to tell it that I don't want it to do it.
    For example this is what the output looks like:
    x = 1, a = 3
    Y is equal to 1.75
    Y is equal to 1.73214
    Y is equal to 1.73205
    Y is equal to 1.73205
    0
    .... on and on, for each value of x and a that I have assigned.

    What I don't want is that 0 at the bottom.

    What the program does is it uses a divide and average
    algorithm to find the square of a number.
    I like what I have so far
    but I am wondering if I used the wrong kind of loop for the problem at hand.
    The reason I chose to do it this way is because
    using the cout statements like I did is the way I
    am most familiar with to get values to the program
    without having to manually enter them.
    And the only way I know how to do it that way is by using a function to assign the values to.
    Essentially I have two questions:
    1. Can that 0 be gotten rid of, or atleast put some
    meaningful data in its place....
    or 2. Am I just plain going about this the wrong way?

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double x;  // initial approximation
    double a;  // number that I am looking for the square root of.
    double y;  // answer
    double g;  // x - a/x
    
    const double errorAllow = 1.0e-5;  // this is the control number to stop the while loop.
    double square(double x, double a); // function to do the actual number munching.
    
    int main()
    {
    
        cout << square(1.0, 3.0) << "\n";   // give the program some numbers to play with
        cout << square(10.0, 3.0) << "\n";  // However, this style has an unwanted behavior
        cout << square(0.01, 3.0) << "\n";  // of displaying a zero after each loop is 
        cout << square(100.0, 3.0) << "\n"; // completed. 
        cout << square(1.0, 4.0) << "\n";
        cout << square(2.0, 4.0) << "\n";
    }
    
     double square(double x, double a) 
     {	
        cout << "x = " << x << ", a = " << a << "\n"; // lets the user know what numbers are being used.
    	y = (x + a/x) / 2;  // init a value for x,y, and g for the while loop to work with.                          
              x = y;
        g = x - a/x;
        if (g == 0)         
    	cout << "x is a perfect square of a!\n";
    	
    	while (g >= errorAllow)
    	{
    	x = y;
    	y = (x + a/x) / 2;
            g = x - a/x;
    	cout << "Y is equal to " << y << "\n";
    	//return x,y;   // NOT BEING VERY USEFUL!		
    	}	
    	 return 0;
     }
    
     /* this code was to manually enter in values for x and a 
     ** for debugging, I moved it here to get it out of my way */
    
    	/*	cout << "Enter a value for x: " << x <<"\n";
    	cin >> x;
    	cout << "\nEnter a value for a: " << a <<;
    	cin >> a; */
    Semper Fi!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Store the return value of the function in a variable. If the value is 0 don't output, otherwise output the variable.

    Code:
    double result;
    ...
    if( (result = square(1.0,3.0)) != 0.0 ) cout << result << '\n';
    if( (result = square(10.0,3.0)) != 0.0 ) cout << result << '\n';
    ...
    ...
    etc. etc.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    The value '0' is printed out by main(), upon return from square().

    square() should return y, not 0!

    No need to store it in a variable; your original works (after you fix the return statement in square()).

    Dave

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Your square() function always returns zero!

    This...
    Code:
    cout << square(1.0, 3.0) << "\n";
    ... Will print whatever square() returns. (zero)

    I think you want to return y.

    A function can only return ONE value. This wont work...
    Code:
    return x,y;
    ... You can use pointers or references (which you may not have learned yet) if you need to "affect" more than one variable.

    And REALLY, you are computing a SQUARE ROOT, NOT A SQUARE!!!! (You can abbreviate it as "root", but not as square.)
    The square of 2 is 4, the square root of 4 is 2... But I think you knew that.

    [EDIT] -
    ... And I'm assuming that you want to "roll your own" algorithm. There is a sqrt() as well as pow() function in the <math> header file.

    Code:
    #include <math>
    
    double sqrt( double num ); // returns square root.
    double pow( double base, double exp );  // returns base to the exp power
    You can also get a square root by raising a number to the one-half power:

    x = pow(y, 0.5); // x = the square root of y
    x = pow(y, 0.333); // x = (approximate) cube-root of y
    x = pow(y, 0.25); // x = 4th root of y
    Last edited by DougDbug; 03-22-2004 at 03:32 PM.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Thanks for the input. I fixed it up, using abit from each of you. I moved the "return y" statement to where the "return 0" statement was, and then added text to the cout's so that when the program executes it displays:
    x = 1, a = 3
    Y is equal to 1.75
    Y is equal to 1.73214
    Y is equal to 1.73205
    Y is equal to 1.73205
    The square root of 3 is: 1.73205


    No more 0. Cool. Thanks.
    Semper Fi!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM