5

I am trying to implement the following subroutine.

string& sortAndMerge(string &u, int start, int end){
    if(end>=0){

        int mid=(start+end)/2;

        if(start<end){
            sortAndMerge(u,start,mid);
            sortAndMerge(u,mid+1,end);
            return merge(u,start,mid,end);
        }
    }
    return;
}

The function is called as sortAndMerge(s,0,s.length()-1), here s is a string. As the 3rd parameter can end up being negative, I need the subroutine to ignore those cases.

I am receiving the following error:-

return-statement with no value, in function returning ‘std::string& {aka std::basic_string<char>&}’ [-fpermissive].

I have two questions.
1) Is there a way I can return nothing from a function having a return type other than void.
2) When can I use the return keyword with nothing or NULL.

11
  • 4
    Not sure why this is necessary. The string is passed by ref, so why are you returning anything? As long as your merge function takes the string by ref as well void will do
    – smac89
    Oct 27, 2015 at 6:26
  • 1
    If end being negative is an error condition you should throw an exception. Or refactor your function to return a bool that indicates success or failure. Oct 27, 2015 at 6:27
  • 3
    No, references is an all-or-nothing thing, either you have a valid reference to an existing object, or you can't use references. And that's one path could take here, simply return by value. Oct 27, 2015 at 6:28
  • 1
    You must return a reference, and there is no kind of null reference. When the return value is a kind of a pointer, then by convention you can return 0 (or NULL), which means the address of nothing. In your case, consider using exceptions. Oct 27, 2015 at 6:29
  • 1
    You don't seem to use the return value anyway so consider changing the function to have void return type
    – M.M
    Oct 27, 2015 at 6:35

4 Answers 4

9

1) Is there a way I can return nothing from a function having a return type other than void.

No. Not if you want your program to work.

2) When can I use the return keyword with nothing or NULL.

You can use the return keyword with nothing only in functions that have a return type of void. You can use the return keyword with NULL in any function that has a pointer (not reference) type as its return type.

Depending on how NULL is defined I suppose you could also return NULL from functions with int as their return type, but it would make more sense to just return 0.

0
5

You can't do that, because declaring your function as
string& sortAndMerge(string &u, int start, int end)

You promise function callers to return a string reference. So all the callers expect and waiting to get a string reference. So compiler not allow the break such promises (rules).

Either you have to return what you promise or redefine function to return some other data type. You can consider Enum, Union or some other thing if you need to return different things. Other than this you can trow an exception to exit from the function without any return. but it does not consider as proper function execute.

1

You could always use bool as return type

bool do(int& tryInt, int s, int e)
{
  if(s>5 || e<0)
    return false;
  tryInt= s + e;
    return true;
}

int main()
{
  int tryInt = 0;
  int myInt = do(tryInt,6,4)? tryInt : 0;
}

edit : sorry, I didn't see when it was posted :/

0

The is the C++17 structure, std::optional, that may hold a value, but also may not. There are cases when it just there is no value, including the first loop iteration in many algorithms. You can check is as boolean to verify if there is the value or not.

However std::optional does not work with references. If you really need to return your string by reference (say it is very long), use std::stringview instead:

std::optional<std::stringview> sortAndMerge(std::stringview u, int start, int end)
 

std::stringview does not hold the string internally, only reference to it, so it is cheap to copy and you no longer need to work with references by hand. It has many methods similar to that std::string provides, so you can do similar things with it.