5

I wrote a practice program for my class, and everything in it works except for returning the value of a variable. My question is, why isn't it returning the value? Here is sample code I wrote out to avoid having to copy and paste large parts of code that aren't relevant.

#include <iostream> using std::cout; using std::cin; using std::endl; using std::fixed; #include <iomanip> using std::setw; using std::setprecision; int testing(); int main() { testing(); return 0; } int testing() { int debtArray[] = {4,5,6,7,9,}; int total = 0; for(int debt = 0; debt < 5; debt++) { total += debtArray[debt]; } return total; } 
4
  • 2
    That code simply discards the return value. Try changing testing(); to std::cout << testing(); and see if you don't get something. Commented Aug 31, 2013 at 18:41
  • 1
    The testing function does indeed return a value. But you simply discard that value in the call. What did you expect to happen? Commented Aug 31, 2013 at 18:41
  • 1
    "Here is sample code I wrote out to avoid having to copy and paste large parts of code that aren't relevant." -- And we thank you kindly for that. Commented Aug 31, 2013 at 18:43
  • Thanks for the input and giving me your time. Unfortunately it seems all my mistakes turn out to be rookie mistakes that beg the reply "you should have known better". Commented Aug 31, 2013 at 19:02

5 Answers 5

9

In fact, the function is returning a value. However, main() is choosing to ignore that return value.

Try the following in your main():

int total = testing(); std::cout << "The total is " << total << std::endl; 
Sign up to request clarification or add additional context in comments.

Comments

4

The function does return a value. You are not displaying the returned value on the screen so that is why you think it doesnt return a value

Comments

4

testing() does return a value, but the value does not get used or saved anywhere. You are using std::cout, std::cin, std::endl, etc. but you aren't using them. I'm assuming what you wanted to do was display total. A program for that would look like:

#include <iostream> using std::cout; using std::endl; int testing(); int main() { int totaldebt = testing(); cout << totaldebt << endl; return 0; } int testing() { int debtArray[] = {4,5,6,7,9}; int total = 0; for(int debt = 0; debt < 5; debt++) { total += debtArray[debt]; } return total; } 

What is happening in your code is (assuming the compiler doesn't optimize in any way) inside main(), testing() is called, goes through its instructions, and then the program moves on. The same thing happens if you call printf from <cstdlib>. printf is supposed to return the number of characters it displays, but if you don't store the result anywhere it just displays the text and the program continues.

What I have to ask is why are you using more than you actually make use of? Or is this not the complete code?

1 Comment

@ExpletiveDeleted Ah, alright, I was going to say that you were using so many things that you never actually made use of.
3

Return is not equivalent to print. If you want the value the function has returned to display to stdout, you have to have a method of doing that. This is accomplished by printing the value that was returned using std::cout and the << operator either in main or in the function itself

Comments

2

Your code is perfect but it does not takes the value which is being returned by function testing() Try this,
This will hold the data that is being returned by your testing() function

#include <iostream> using std::cout; using std::cin; using std::endl; using std::fixed; #include <iomanip> using std::setw; using std::setprecision; int testing(); int main() { int res = testing(); cout<<"calling of testing() returned : \t"<<res<<"\n"; return 0; } int testing() { int debtArray[] = {4,5,6,7,9,}; int total = 0; for(int debt = 0; debt < 5; debt++) { total += debtArray[debt]; } return total; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.