What is the return value if we dont return anything from a non void return typed function in c++?[Experimental] -
this question has answer here:
i have observed if don’t return value empty function int
return type 1
. in below case showing 4 3 2
o/p(is value of static variable si
getting printed here? if print si
o/p 2 3 4, in reverse of now. there function's stack push , pop here in case?). observed if use float return type prints nan nan nan
o/p. behavior compiler dependant (i have tried both gcc , devcpp, observed same)? going on here? please share thoughts on this.
#include<iostream> using namespace std; int f(int i){ static int si = i; si = si + i; ///return si; } int main(){ cout<<f(1)<<" "<<f(1)<<" "<<f(1); //cout<<" "<<f(1); //if uncomment line o/p is: 4 3 2 5, looks it's printing value of si. }
it looks behavior of cout
causing reverse printing of static variable si
's value?
it undefined behaviour. have return non-void function*
from § 6.6.3 return statement [stmt.return]
flowing off end of function equivalent return no value; results in undefined behavior in value-returning function.
* main()
function has implicit return 0
not necessary explicitly return. special case.
Comments
Post a Comment