Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return nothing from an integer function in C++?

Consider the following code:

#include <iostream>
int test(int a){
    if (a > 10){
        return a;
    }
    else{
        std::cout << "Error!";
        return nothing;
    }
}
int main(){
    std::cout << test(9);
    return 0;
}

What I want is that The integer function test(int a), return a if a > 10, otherwise return Error!. but since this is an integer function, it must return an integer value, but I want that it print Error and return nothing. Is there a way for do this? (Also note that I don’t want to use a void function)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

#include <stdexcept>

int test(int a){
    if (a > 10){
        return a;
    }
        else{
            throw std::invalid_argument( "a is smaller or eq than 10" );
    
        }

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading