-1

As you see in my code, I have to return an array through return function. What necessary thing do I have to change in my code to disable the warning of return type? When I run this code, it gives me a warning. I have to remove this warning:

warning: no return statement in function returning non-void [-Wreturn-type]

#include <bits/stdc++.h>
using namespace std;

int sort(int a[], int n)
{
    int zeros = 0;

    for (int i = 0; i < n; i++)
    {
        if (a[i] == 0)
        {
            zeros++;
        }
    }
    int k = 0;
    while (zeros--)
    {
        a[k++] = 0;
    }
    while (k < n)
    {
        a[k++] = 1;
    }
}

int main()
{
    int n;
    cout << "Enter the n value : ";
    cin >> n;
    int a[n];

    cout << "Enter the array elements : ";
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    sort(a, n);

    for (int i = 0; i < n; i++)
    {
        cout << a[i];
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Atharva
  • 11
  • 1
  • 1
    Take a moment to unpack that error message. It says your function is non-void (because it returns int) and it's missing a return statement (because you don't have one). If you don't need that function to return a value, change its return type to void. – paddy Nov 24 '21 at 08:34
  • `sort` is declared to return an `int` but it never returns one. Either declare its return type to be `void` or return something – 463035818_is_not_an_ai Nov 24 '21 at 08:34
  • `int a[n];` is not standard C++. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use `std::vector` for dynamically sized arrays – 463035818_is_not_an_ai Nov 24 '21 at 08:35
  • There is also a wrinkle that your function is named `sort()` but what is does is not sorting. That's a mistake waiting to happen when some future programmer - including possibly yourself - tries to optimise the code, and rewrites it to actually perform a sort rather than what you intend. – Peter Nov 24 '21 at 09:10

1 Answers1

2
int sort(int a[], int n)

Your sort function is declared to return an int, but you didn't return anything. If you don't want to return anything, declare it using void.

Also, VLA is not valid C++. You should use std::vector instead.

So, your code should look like this.

#include <iostream>
#include <vector>
void sort(std::vector<int> &a) // no need for n
{
    int zeros = 0;

    for (int i = 0; i < a.size(); i++) // use member function size() instead for n
    {
        if (a[i] == 0)
        {
            zeros++;
        }
    }
    int k = 0;
    while (zeros--)
    {
        a[k++] = 0;
    }
    while (k < n)
    {
        a[k++] = 1;
    }
}

int main()
{
    int n;
    std::cout << "Enter the n value : ";
    std::cin >> n;
    std::vector<int> a(n);

    std::cout << "Enter the array elements : ";
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    sort(a);

    for (cons auto &i : a) // range-base for loop, recommend
    {
        std::cout << a[i];
    }

    return 0;
}

Note: using namespace std; and #include <bits/stdc++.h> are both bad practice, so avoid using it.

  • thank you but insted i have to use a return type so can you explain how to define function i don't know how to print array in return type. so there is anything except void i don't want to use void – Atharva Nov 24 '21 at 18:03