Returning a value from function : help!

This is a discussion on Returning a value from function : help! within the C++ Programming forums, part of the General Programming Boards category; this is driving me nuts.....the object is to generate the sum of numbers 1-10. that number, which is 55, is ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Returning a value from function : help!

    this is driving me nuts.....the object is to generate the sum of numbers 1-10. that number, which is 55, is generated with the function COMPUTE_SUM.

    The data does not display correctly as SUM keeps coming back with 0.

    The catch is....the SUM has to be displayed within MAIN and I cannot use global variables.

    I had placed the cout statement outside the loop and it will generate the proper sum but I'm guessing that is not within MAIN.

    Here's the given code. All help is appreciated.


    Code:
    #include<iostream>
    using namespace std;
    
    void compute_sum(int);
    
    
    void main()
    {
    	int sum = 0;
    	int n = 10;
    	compute_sum(n);
    	cout << "The sum is " << sum << endl;
    }
    
    void compute_sum(int x)
    {
    	int sum = 0;
    	int num = 1;
    	while (num <= x)
    	{
    		sum += num;
    		num++;
    	}
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    Look like you probably need to have your function return a value instead of being a void function.

    Code:
    int compute_sum(int x);
    
    ...
    
    int compute_sum(int x)
    {
        int sum = 0;
        int num = 1;
        while (num <= x)
        {
            sum += num;
            num++;
        }
        return sum;
    }
    Your main function needs to then capture this returned value. I'll leave that to you... it's real easy. BTW, main should return an int, not void. You should also realize that the sum variable declared in main is not the same sum variable as is declared in compute_sum. Though they share the same name, they are distinct variables each only visible in their respective functions. I think this is why you say "sum keeps comming back 0". You need to assign the return value from the compute_sum function to the sum variable in main.
    Last edited by hk_mp5kpdw; 10-16-2006 at 08:53 PM.
    "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
    Oct 2006
    Posts
    4
    I have changed the void to int including MAIN. I used the RETURN after the while loop has concluded through a quick COUT, the value of SUM is 55. Why MAIN is not picking it up is the next big mystery.


    Code:
    #include<iostream>
    using namespace std;
    
    int compute_sum(int);
    
    
    int main()
    {
    	int sum = 0;
    	int n = 10;
    	compute_sum(n);
    	cout << "The sum is " << sum << endl;
    	return 0;
    }
    
    int compute_sum(int x)
    {
    	int sum = 0;
    	int num = 1;
    	while (num <= x)
    	{
    		sum += num;
    		num++;
    	}
    	return sum;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You should have

    Code:
      cout << "The sum is " << compute_sum(n) << endl;

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Code:
    cout << "The sum is " << compute_sum(n) << endl;
    It works!

    Still confused but logically the value of compute_sum after completion is 55 so therefore the reference in the cout statement pulls from compute_sum. So I guess my question is....did the return sum; statement return the value of sum back to main or to compute_sum & compute_sum carried it back to main?

    WGK

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    When you call compute_sum() (or any function which returns a value), the value gets returned to the point where you called it. So when you had the line

    Code:
      compute_sum(n);
    you were basically throwing away the value it returned, since you didn't assign it to anything. You can also get rid of the sum variable in main(), since you're not using that either.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Got a better picture of it now. Thanks for the help.

    WGK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 03:30 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 10:37 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21