Home > database >  Non void function does not return any value
Non void function does not return any value

Time:11-05

I'm trying to solve the basic twosum problem on leetcode using brute force i.e. two nested for loops. We are given an array and we have to find a single pair of two numbers whose sum is a specific given value. I've checked the code thrice. The code seems right to me. I don't know why it's not returning any value.

#include <bits/stdc  .h>
using namespace std;
class Solution 
{
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int>::iterator it, it2;
        vector<int> sol;
        
        for(it = nums.begin(); it != nums.end(); it  )
            {    
            for(it2 = it 1; it2 != nums.end(); it2  )
                {
                    if((*it)   (*it2) == target)
                    {
                        sol.push_back(*it);
                        sol.push_back(*it2);
                        return sol;
                    }    
                }
            }
    }

CodePudding user response:

This is because the if condition may not be satisfied for certain input in which case the control flow will not encounter any return statement and will have undefined behavior.

You can solve it by adding a return statement at that point of your function body where no conditions are satisfied so that even if no condition is satisfied, the control flow will pass through that return statement. For example, you can add a return as shown below:

vector<int> twoSum(vector<int>& nums, int target) 
{
    vector<int>::iterator it, it2;
    vector<int> sol;
    
    for(it = nums.begin(); it != nums.end(); it  )
    {    
        for(it2 = it 1; it2 != nums.end(); it2  )
        {
            if((*it)   (*it2) == target)
            {
                sol.push_back(*it);
                sol.push_back(*it2);
                return sol;
            }    
        }
    }
    return sol; //added this return 
}

CodePudding user response:

vector<int> twoSum(vector<int>& nums, int target) 
{
    vector<int>::iterator it, it2;
    vector<int> sol;
    
    for(it = nums.begin(); it != nums.end(); it  )
    {    
        for(it2 = it 1; it2 != nums.end(); it2  )
        {
                if((*it)   (*it2) == target)
                {
                    sol.push_back(*it);
                    sol.push_back(*it2);
                    return sol;
                }    
            }
        }
}

The problem is if if((*it) (*it2) == target) is fasle in your entire program, you function will not return anything, hence the error.

If leetcode ensure you that there will be at least one time that if((*it) (*it2) == target) is true, you can fill in with some placeholder values so that it wiil return on any path, like:

vector<int> twoSum(vector<int>& nums, int target) 
{
    vector<int>::iterator it, it2;
    vector<int> sol;
    
    for(it = nums.begin(); it != nums.end(); it  )
    {    
        for(it2 = it 1; it2 != nums.end(); it2  )
        {
                if((*it)   (*it2) == target)
                {
                    sol.push_back(*it);
                    sol.push_back(*it2);
                    return sol;
                }    
            }
        }
    return std::vector<int>{-1};
}

CodePudding user response:

if ((*it) (*it2) == target) is false you don’t return anything so you must add a return statement at the end of the function.

vector<int> twoSum(vector<int>& nums, int target) 
{
    vector<int>::iterator it, it2;
    vector<int> sol;
    
    for(it = nums.begin(); it != nums.end(); it  )
    {    
        for(it2 = it 1; it2 != nums.end(); it2  )
        {
                if((*it)   (*it2) == target)
                {
                    sol.push_back(*it);
                    sol.push_back(*it2);
                    return sol;
                }    
            }
        }
    return std::vector<int>{-1};
}

Also, I’m not sure if I am talking about the same problem but I believe you are supposed to return an array of the indices and not the actual values.

Example: [2, 7, 3] target: 9 Don’t return [2, 7] Instead [0, 1]

  •  Tags:  
  • c
  • Related