3rd Aug 2017, 3:03 PM
wachirut chaiwong
wachirut chaiwong - avatar
7 ответов
+ 9
static variables cannot be re-initialized. For example, 1st function call: i = 5, returns 6 2nd function call: i was supposed to be 5 again if it'd be a non-static variable. But since it's static, it won't lose its previous value. So it'll return ++i = 6+1 = 7 3rd function call: same explanation as 2nd function call. It'll return 8 Change your return type from void to int, the output will be: 6 7 8
3rd Aug 2017, 3:38 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 7
But I'm getting number of errors (it's not supposed to compile) both in Code Playground and CodeBlocks. Which tool are you using to compile? The return type should be int instead of void.
3rd Aug 2017, 3:30 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
Thank you verymuch that awesome answer. Suraj and Yasmin i'm get it now.
3rd Aug 2017, 3:45 PM
wachirut chaiwong
wachirut chaiwong - avatar
+ 3
static is not constant
3rd Aug 2017, 3:49 PM
wachirut chaiwong
wachirut chaiwong - avatar
+ 2
i use play ground. i so confuse we can't change the static value is true?
3rd Aug 2017, 3:29 PM
wachirut chaiwong
wachirut chaiwong - avatar
+ 2
Static variables are those whose value keep updating at every function call. These can be used when a function needs to change the value of variable .
3rd Aug 2017, 3:35 PM
G.S.N.V. Suraj
G.S.N.V. Suraj - avatar
+ 1
return can not be used to print, the value returned is printed by cout.
3rd Aug 2017, 3:25 PM
G.S.N.V. Suraj
G.S.N.V. Suraj - avatar