[Solved] Is a function terminates as soon as it returns a value, ignoring its next lines? [closed]


If a function returns, it terminates there, following lines of code in that function will not be executed.

#include <iostream>
#include <string>

std::string foo(int x)
{
    if(x < 0) return "If x < 0, this will be returned";
    if(x == 0) return "If x == 0, this will be returned";
    
    return "If x > 0, this will be returned";
}

int main() {
    std::cout << foo(-1) << std::endl;
    std::cout << foo(0) << std::endl;
    std::cout << foo(1) << std::endl;
}

Output:

If x < 0, this will be returned
If x == 0, this will be returned
If x > 0, this will be returned

0

solved Is a function terminates as soon as it returns a value, ignoring its next lines? [closed]