0

I have two small functions called from main. Below is the program

int fun1(int x)
{
   int s;
   s=8;
}

int fun2(int x)
{
   int s;
   s=x*4;
}
int main(int argc, char *argv[])
{
   int n=2;
   cout << fun1(n)<<endl;
   cout<<fun2(n)<<endl;
   return 0;
}

1.fun1() function called with arguement x but no operation done using x variable. So in the output "cout<< fun1()-- I am getting output 2 "present value of n".

2.fun2() function called with arguement x and used it in operation "s=x*4" and in the output "cout<< fun2()"-- I am getting output 8--"2*4".

I am not passing any reference or pointer value to the function still getting two different outputs. Would like to know how the stack unwinding is different in the two different function cases.

Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51

1 Answers1

1

Your functions do not return anything , despite having a return type. This is undefined behaviour. This question/answer provides more details Function with missing return value, behavior at runtime .

As you are invoking undefined behaviour your display output is essentially meaningless.

Community
  • 1
  • 1
mathematician1975
  • 21,161
  • 6
  • 59
  • 101