Raised This Month: $ Target: $400
 0% 

static question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
n0br41ner
Senior Member
Join Date: May 2012
Location: Planet Earth
Old 03-13-2013 , 20:37   static question
Reply With Quote #1

Hello,

I have a small and possibly dumb question. Before i came hear i read the part where it explains about static variables in the pawn language guide, but still i couldn't find an answer to my question.

Let's say i have this line:
PHP Code:
MyFunction( ) {
    static 
iVariable 5;

    
// stuff here

In C++, AFAIK, the first time that function is called, iVariable is set to 5, but when any other time that function is called, it is not re-initialized to 5, it's current value stay the same. Let me make things clear with an example.

Let's say inside that function i changed the value of iVariable like the following:
PHP Code:
MyFunction( ) {
    static 
iVariable 5;

    
// stuff here

    
iVariable += 1;

    
// stuff here

The first time the function is called, iVariable is created and set to 5. Then iVariable becomes 6. But then at the second call of the function, iVariable is not reset to 5, but it's value remains 6, therefore after the function ended, it's value becomes 7. That is in C++.

My question is, does that scenario applies to pawn as well?

If i wasn't completely clear, please tell me and i will explain in another way if possible.

EDIT: nvm tested it and the scenario still applies. I have no idea why i didn't test it before.

Regards,
n0br41ner
__________________

Last edited by n0br41ner; 03-13-2013 at 20:53.
n0br41ner is offline
Ef_Deas
Member
Join Date: Aug 2012
Location: Rio de Janeiro
Old 03-14-2013 , 08:23   Re: static question
Reply With Quote #2

Yes.
PHP Code:
MyFunction() {
    static 
iVariable 5;
    
iVariable += 1;
    
console_print(0"Variable: %d\n"iVariable )

PHP Code:
while(true)
MyFunction(); 
Should display in console
Quote:
"Variable: 6
"Variable: 7
"Variable: 8
"Variable: 9
"Variable: 10
...
"Variable: 65535
...

Last edited by Ef_Deas; 03-14-2013 at 08:30.
Ef_Deas is offline
Send a message via Skype™ to Ef_Deas
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:36.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode