Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly handle if a function exits without encoutering a return

Tags:

c++

I have a function that search a vector and returns the item if it is found. But I want to know that best software appraoch to handle if it is not found.

I have created a function and could return -1 or something but that wouldn't match the return type.

koalaGraph::PVertex Koala::lookUpVertexbyName(const std::string&vertexName, const std::vector<koalaGraph::PVertex>& koalaVertices) {

    for (size_t i = 0; i < koalaVertices.size(); i++) {

        if(koalaVertices[i]->info.name == vertexName) 
            return koalaVertices[i];
    }
}

If a situation is encountered where the item being searched for is not in the vector then program will exit.

like image 839
black sheep Avatar asked Apr 10 '19 10:04

black sheep


People also ask

What happens if function has no return?

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.

How do you call a function without returning value?

A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple. Calling the function is performed by using the call operator () after the name of the function.

Can you have a function without a return?

Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.

Should a function contains a return statement if it does not return a value?

A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non- void return type, the compiler issues a warning message.


2 Answers

You can use std::optional

#include <optional>
std::optional<koalaGraph::PVertex> 
Koala::lookUpVertexbyName(const std::string&vertexName, 
                          const std::vector<koalaGraph::PVertex>& koalaVertices) {

    for (size_t i = 0; i < koalaVertices.size(); i++) {

        if(koalaVertices[i]->info.name == vertexName) 
            return koalaVertices[i];
    }
    return {};
}

int main()
{
    Koala k;
    //...
    auto maybeVertex = k.lookUpVertexByName("foo",vertices);
    if(maybeVertex)
        koalaGraph::PVertex&& vertex = *maybeVertex;
    //alternatively
    if(maybeVertex.has_value())
        //found
}
like image 104
Quimby Avatar answered Oct 13 '22 10:10

Quimby


You could use a for-loop and return a iterator.

std::vector<koalaGraph::PVertex>::const_iterator
Koala::lookUpVertexbyName(
        const std::string&vertexName, 
        const std::vector<koalaGraph::PVertex>& koalaVertices) 
{
    for(auto iter = koalaVertices.begin(); iter != koalaVertices.end(); ++iter) {
        if(koalaVertices[i]->info.name == vertexName) {
            return iter;
        }
    }
    return koalaVertices.end();
}

Further you check if you got end back. end indicates if the value was found or not.

auto iter = <fucntioncall> // lookUpVertexbyName
if (iter == <vector>.end() {
    // abort or do what ever you want
}

To use the value you have to dereference the iterator. DON'T derefence the end-iterator, it will lead you to neverland -> undefined behavior.

std::string test = *iter;
like image 20
skratchi.at Avatar answered Oct 13 '22 10:10

skratchi.at