-3

What does " control reaches end of non-void function" means??

How to remove the warnings from out code ?

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

int LinearSearch()
{
    int ans=-1;
    
    cout << "Enter the Size of the array:  \n";
    int n;
    cin >> n;
    cout << "Enter the array elements: \n";
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int key;
    cout << "Enter the key: \n";
    cin >> key;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == key)
        {
            cout << "the " << key << " is found at index " << i << endl;
        }
        return ans;
    }
}

    int main()
{
    while (1)
    {
        cout << "\t Main Menu\n";
        cout << "1. For Linear Search\n";
        cout << "2. For Binary Search\n";
        cout << "3. For First and last Occurence\n";

        int ch;
        cout << "Enter the choice: \n";
        cin >> ch;
        switch (ch)
        {
        case 1:
           
          cout<<  LinearSearch();
            break;
        case 2:

            break;
        case 3:

            break;

        default:
            cout << "Invalid Choice OOps!! ";
            break;
        }
    }

    return 0;
}

enter image description here

AS I am trying to run it it is giving me Warning Why?? Error is: warning: control reaches end of non-void function [-Wreturn-type] How to resolve it?

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • 2
    Tip: You should probably read in the array outside of `LinearSearch`, and pass it as an argument (along with its size). – ikegami Sep 06 '22 at 03:30
  • Simple approach is to add a `return -1;` to the end of `LinearSearch()` (or return some other value which indicates the function had not previously returned). Incidentally, `int arr[n];` where `n` is a variable is NOT VALID in standard C++ (although some compilers support it as a *non-standard extension*). – Peter Sep 06 '22 at 03:33
  • "Control reaches end of non-void function" means that you've defined a (non-void) return type for the function but haven't actually returned anything. Or more correctly, it is possible that you don't return anything. – Layne Bernardo Sep 06 '22 at 03:34
  • 1
    On a side note use `#include ` instead of `#include ` (which is not standard C++) and use `std::vector` instead of `int a[n]` it is a language extension. Default C++ requires `n` to be a constant. And do not use `using namespace std;` And if you use `std::vector` have a look at [range based for loops](https://en.cppreference.com/w/cpp/language/range-for) – Pepijn Kramer Sep 06 '22 at 03:39
  • Looks like you're trying to learn C++ from competition sites. Save yourself a lot of time and get [a good book or two](https://stackoverflow.com/q/388242/4581301) to get yourself started. If you want to compete for fun or practice after learning the fundamentals, go nuts, but competition sites are not intended to teach and as a result do a terrible job. – user4581301 Sep 06 '22 at 04:16
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jesper Juhl Sep 06 '22 at 06:22
  • At the end of your function that is supposed to return something, but doesn't return something, and you don't expect the program should be able to get there, just put a `throw "never happen";` at the end of the code block. – Eljay Sep 06 '22 at 11:29

3 Answers3

0

When n is zero, you never reach a return. You need a return after the loop. Returning -1 might be the sensible choice in that situation. (Also, the return in the loop is misplaced...)

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

It means that int LinearSearch() is expected to return an int but there are code paths where that does not happen. For instance, if n == 0. You fix this by adding a return statement with an appropriate value on that code path. It's probably an error that you have the return within the for() loop as this means you get at most one iteration. Maybe this is what you want?

int LinearSearch()
{
    int ans=-1;

    cout << "Enter the Size of the array:  \n";
    int n;
    cin >> n;
    cout << "Enter the array elements: \n";
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int key;
    cout << "Enter the key: \n";
    cin >> key;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == key)
        {
            cout << "the " << key << " is found at index " << i << endl;
            ans = i;
            break;
        }
    }
    return ans;
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0

Like others have mentioned, the warning indicates one of your code path has no return value. In LinearSearch after the for loop a return is missing. You can use -1 to return when a key match is not found or better still is if your C++ compiler supports C++17 or higher standard then I would suggest using std::optional and to return a "no value" to use std::nullopt and return the "ans" when you actually find the key. Please look at the code below for a sample implementation.

#include <optional>

using namespace std;

std::optional<int> LinearSearch()
{
    int ans;
    
    cout << "Enter the Size of the array:  \n";
    int n;
    cin >> n;
    cout << "Enter the array elements: \n";
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int key;
    cout << "Enter the key: \n";
    cin >> key;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == key)
        {
            cout << "the " << key << " is found at index " << i << endl;
            return ans;
        }
    }
    return std::nullopt;
}

    int main()
{
    while (1)
    {
        cout << "\t Main Menu\n";
        cout << "1. For Linear Search\n";
        cout << "2. For Binary Search\n";
        cout << "3. For First and last Occurence\n";

        int ch;
        cout << "Enter the choice: \n";
        cin >> ch;
        switch (ch)
        {
        case 1:{
          auto res = LinearSearch();
          if (res) cout<<  *res;
          else cout << "key not found";
        }
            break;
        case 2:

            break;
        case 3:

            break;

        default:
            cout << "Invalid Choice OOps!! ";
            break;
        }
    }

    return 0;
}
Shaks
  • 1
  • 2