Below is my code in C++. Here in the function "temp", 1st, the value of 'ch' is declared & then printed(at the first time 'ch' is not initialized so 'ch' has Undefined value).Then "if" condition is satisfied(Inside "if" no work is done). Then function gets over & returned to the next line of the function call(increments 'i'). Then the next iteration also goes in the same way as the previous call. The same cycle goes on until a key is pressed. When a key is pressed(for e.g. 'a' is pressed), "else" gets executed & the key pressed is taken by getch(). Then function gets over & returned to next line of function call(i++), then in all the coming next iteration, when a function is called, 'ch' is assigned with the previous key pressed('a') and it prints that character('a'). It goes on printing 'a' until another key is pressed. Why & how 'ch' is assigned with the previous function call's value. Is the value of variable of previous call is stored inside the stack & assign it to the next call's variable?
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int flag = 0,i = 1;
void temp()
{
char ch;
cout<<ch;
if(!_kbhit())
{
}
else{
ch=_getch();
}
}
int main()
{
while(flag!=1)
{
temp();
i++;
}
return 0;
}