-6

Although the control isn't going to the if condition in isCyclic() function but still it is returning 1. When I uncomment the return 0 statement, it is indeed returning 0.

So, does an int function return 1 by default? I don't think so. It returns garbage value. But then why it is returning 1 here!!!

#include <bits/stdc++.h>

using namespace std;

class edge{
public:
    int src,des;
};

class graph{
public:
    int v,e;
    edge *edges;
    int edges_counter;
    graph(int v, int e){
        this->v = v;
        this->e = e;
        edges = new edge[e];
        edges_counter = 0;
    }
    void addedge(int src, int des){
        edges[edges_counter].src = src;
        edges[edges_counter].des = des;
        edges_counter++;

    }

    int find(int parent[], int i){
        if(parent[i]==-1)
            return i;
        return find(parent, parent[i]);
    }

    void Union(int parent[], int x, int y){
        parent[x] = y;
    }

    int isCyclic(){
        int parent[v];
        for(int j=0; j<v ;j++) parent[j] = -1;

        for(int i=0; i<e; i++){

            int first = find(parent, edges[i].src);
            int second = find(parent, edges[i].des);

            if(first==second){
                cout <<"Hell";
                return 1;
            }

            Union(parent, first, second);

        }
        //return 0; //here
    }
};

int main(){
    graph g(3,2);
    g.addedge(0, 1);
    g.addedge(0, 2);
    cout << g.isCyclic() << endl;
    if(g.isCyclic()==1) cout << "Grpah contains cycle\n";
    else cout << "Grpah doesn't contain cycle\n";
}

2 Answers2

2

And isn't 1 a perfect "garbage value"?

The C++11 draft states in § 6.6.3/2:

Flowing off the end of a function [...] results in undefined behavior in a value-returning function.

"undefined behavior" basically means "anything can happen". If you have a function returning "int", but you don't return, it'll return whatever is currently laying around in memory. That could be 0, 1, 12345, or any other garbage value you can think of.

You should add a return statement to every possible code flow where your function could end. You can use the parameter -Wreturn-type to make the compiler warn you about these problems in the future.

Community
  • 1
  • 1
Florian Bach
  • 919
  • 7
  • 22
2

So, does an int function return 1 by default?

No. There is no default return value (except for main, which returns 0 by default.)

It returns garbage value.

Correct. More correctly: The behaviour of the program is undefined.

But then why it is returning 1 here!!!

Because the behaviour is undefined.

I think you misunderstand what "garbage" value means. Garbage can have any value. 1 is within the set "any value".

but it is returning 1 every time on every PC!!

Because the behaviour is undefined. The standard does not guarantee that the return value be other than 1 on any occasion. Considering that, why would you expect the function to not return 1? Of course, it doesn't guarantee that the return value be 1 either, so expecting that would be equally silly. Any expectation would be folly when you know that the behaviour is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326