function works fine without return value?

I forgot to write down
 
return ps;


in the end of the function below, but it works fine in my program, what is the reason for this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
PStation Station::make_station(int id, int c, int n){
    PStation ps=NULL;
    
    switch(s_type){
        case 1:
            ps=new ZCStation(id,c);           
            break;
        case 2:
            ps=new O_ZCStation(id,c);
            break;
        case 3:
            ps=new L_ZCStation(id,c,n);
            break;
        case 4:
            ps=new S1A_ZCStation(id,c);
            break;
        case 5:
            ps=new S1B_ZCStation(id,c);
            break;
        case 6:
            ps=new S2_ZCStation(id,c);
            break;
        default:
              std::cout<<"station type wrong!"<<std::endl;
        }  
      
    }
Pure luck.
I'm just wordering
in my program
1
2
  for(i=0;i<N;i++)
    p_sta[i]=Station::make_station(i, C, N);


how can p_sta[i] be assigned the correct pointer?

I tested the output result(some statistic data), and there is nothing wrong
Like I said, pure luck. The return method used by the compiler happened to find the pointer at the position it looked at. The smallest change to the function could completely break this.
To fully understand why this is happening, you'd have to look at the generated Assembly.
Hi,

It is not ok if you do you write a return for a function which is of return type! The compiler should return you an error regarding your function saying that nothing is returned! Afterall, you are writing this function for a purpose of expecting something to be computed/returned and pass it to some parameters in your main function.

If you do not intend to return any value. Just use void instead of PStation in the declaration!

Hope this helps.

LW
@lwtan90 (2):
Compiler should not return a error! It must return a warning, like that:

control reaches end of non-void function


@nanger (63):

PStation ps=NULL;
-is a first element in your function, and it can be, that address of function may coincide with address of variable
PStation ps=NULL,

when you try to call and execute function Station::make_station
Topic archived. No new replies allowed.