0

I'm trying to create a function that will take the input from the user and create an inverted triangle. However, I'm getting an error at the end of my function(line 29) that states "control reaches end of non-void function."

I think it has something to do with a return value but I'm not sure. Also, when I change int number inside my function to int num I get an error saying,"Redefinition of 'num'."

Please help. Explain what the error is occurring and how I can fix it.

Thank you.

#include <iostream>

using namespace std;

int row(int num)
{
    int number;
    int decreasedNumber;
    
    for(int i = number; i >= 0; i -= 2)
    {
        decreasedNumber = i;
        
        //decreased number from the outer loop will decrease once again
        for(int j = decreasedNumber; decreasedNumber >= 0; decreasedNumber -=2)
        {
            cout << decreasedNumber << " ";
        }
        cout << endl;
    }
}

int main()
{
    
    //Prompting the user to enter a number and collect that input
    cout << "Enter a number: " << endl;
    cin >> number;
    
    cout << row(number);
   
    return 0;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    where are the `return`in the function *row* whose signature indicates it returns an `int` expected in `cout << row(number);`? – bruno Aug 21 '20 at 12:53
  • `number` in `row()` is used without initialization, and `num` has never used. – John Park Aug 21 '20 at 12:55
  • in `for(int j = decreasedNumber; decreasedNumber >= 0; decreasedNumber -=2)` the variable *j* is not used, are you sure this is what you want ? – bruno Aug 21 '20 at 12:57
  • Although this is a very basic issue which reveals a lack of basic knowledge of the language which you should definitely solve by reading a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) I must commend for the good overall construction of your answer including the propper code indentation which is something I see less often than I'd like. – anastaciu Aug 21 '20 at 13:01
  • 2
    I must say, however, that it would be best if you explained what you are trying to achieve with this program, this would probably provide you better answers. – anastaciu Aug 21 '20 at 13:08

1 Answers1

5

Any function that returns a type other than void must return a value from the function on all control flow paths. Otherwise, the program invokes undefined behavior.

Since your function has an int return type, you must return a value convertible to an int from the function:

int row(int num) {
  // ...
  return 42;  // for example
}

Also, you are not using the argument num inside the function, but are using number which is uninitialized. You probably meant to use num instead of number.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • I added return 0 after the for loops but know my loops don't work. I know the loop worked because it was successful in a previous code. – Tiana Chargin Aug 21 '20 at 13:02
  • 1
    Your previous code had UB, so if it worked, that was an accident. In what way does the code not work now? Please update your question with clear details. – cigien Aug 21 '20 at 13:04