Avoid declaring Global Variables Inside functions by declaring Static Variables instead

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Avoid declaring Global Variables Inside functions by declaring Static Variables instead

23 Oct 2016, 16:52

This is an example showing how to make the "Main Script" to get "Static Variables" values from functions, avoiding so declaring "Global" variables inside functions!

Test Function Example:

Code: Select all

loop, 10
Test( )

msgbox, % Test("Addition") "`n`n" Test("Test") "`n`n" Test("Auto") "`n`n" Test("Code") "`n`n" Test("Lol")

msgbox, % Addition "`n`n" Test "`n`n" Auto "`n`n" Code "`n`n" Lol	;these variables from the main script are all blank


Test("", "Avoid using Global Variables Inside functions.txt")	;read ".ini" files directly from the function
					;"Error" output if no file found


msgbox, % Test("Addition") "`n`n" Test("Test") "`n`n" Test("Auto") "`n`n" Test("Code") "`n`n" Test("Lol")

msgbox, % Addition "`n`n" Test "`n`n" Auto "`n`n" Code "`n`n" Lol	;these variables from the main script are all blank



Test(GetStaticVarValue := "", Load := "")	;___________ Test (Function) ____________
{
static Addition, Test, Auto, Code, Lol

	if Load !=		;if "Load" is not blank
	{
	Iniread, Addition, %Load%, section, Addition
	Iniread, Test, %Load%, section, Test
	Iniread, Auto, %Load%, section, Auto
	Iniread, Code, %Load%, section, Code
	Iniread, Lol, %Load%, section, Lol
	return
	}

	if GetStaticVarValue !=		;if "GetStaticVarValue" is not blank
	{
	GetStaticVarValue := %GetStaticVarValue%
	return, GetStaticVarValue
	}

Addition++
Test = Alot of test
Auto = no manual
Code = Script
Lol = not funny
}
Test "Load" File:

Code: Select all

[section]
Addition = 200
Test = 201
Auto = 202
Code = 203
Lol = 204
Last edited by V for Vendetta on 29 Nov 2016, 12:04, edited 3 times in total.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Avoid using Global Variables Inside functions

23 Oct 2016, 19:00

To use global variable inside a function, you have to declare it as global in the function (or declare it as a super-global outside of it). Otherwise you are just using local variables that happen to have the same names as your global variables. See the documentation on global variables inside functions.

Btw, this thread should be in the Ask For Help forum, not in Scripts and Functions.
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid using Global Variables Inside functions

29 Nov 2016, 11:59

HotKeyIt wrote:Moved to Ask For Help
This isn't a "Question"!

This is an example showing how to make the "Main Script" to get "Static Variables" values from functions, avoiding so declaring "Global" variables inside functions!
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

29 Nov 2016, 13:23

Before you edited it, it was not a "how to" example. You had a script in which you incorrectly expected the variables to act as global simply because they had the same name both inside and outside the function, and you didn't understand why.
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Avoid using Global Variables Inside functions

29 Nov 2016, 13:26

V for Vendetta wrote:
HotKeyIt wrote:Moved to Ask For Help
This isn't a "Question"!

This is an example showing how to make the "Main Script" to get "Static Variables" values from functions, avoiding so declaring "Global" variables inside functions!
Your function basically boils down to this:

Code: Select all

Test(GetStaticVarValue := "")
{
	static Addition, Test, Auto, Code, Lol
	Addition++
	Test = Alot of test
	Auto = no manual
	Code = Script
	Lol = not funny
	return, %GetStaticVarValue%
}
Which is a trivial example demonstrating what "static" does. It is not a function that is likely to be used by others which is generally what is posted in Scripts and Functions.

It could possibly go in Tutorials but its rudimentary nature gives the impression that you need help understanding how super-global, global, local, and static variables work.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

29 Nov 2016, 13:29

boiler wrote:Before you edited it, it was not a "how to" example. You had a script in which you incorrectly expected the variables to act as global simply because they had the same name both inside and outside the function, and you didn't understand why.
yes, it is and always was a "how to" example!

The Script is the same, I didn't modify anything, so, you are the one who didn't understand it at all!
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid using Global Variables Inside functions

29 Nov 2016, 13:35

FanaticGuru wrote: It could possibly go in Tutorials but its rudimentary nature gives the impression that you need help understanding how super-global, global, local, and static variables work.
FG
I have the impression that you are the one here who needs help understanding how super-global, global, local, and static variables work.
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid using Global Variables Inside functions

29 Nov 2016, 13:45

FanaticGuru wrote: Your function basically boils down to this:

Code: Select all

Test(GetStaticVarValue := "")
{
	static Addition, Test, Auto, Code, Lol
	Addition++
	Test = Alot of test
	Auto = no manual
	Code = Script
	Lol = not funny
	return, %GetStaticVarValue%
}
What is this? what is this non-sense? It is not even possible to get functions "Static Variables" values from "Main Script" as you are demonstrating in that function of yours!
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

29 Nov 2016, 13:48

V for Vendetta wrote:
boiler wrote:Before you edited it, it was not a "how to" example. You had a script in which you incorrectly expected the variables to act as global simply because they had the same name both inside and outside the function, and you didn't understand why.
yes, it is and always was a "how to" example!

The Script is the same, I didn't modify anything, so, you are the one who didn't understand it at all!
As of today, you edited your original post three times, the latest being earlier today (a month after you originally posted it). It was originally showing that the value of a variable wasn't what you said it should have been, and the reason was that there was nothing tying the values of variables inside a function to those outside of it, other than the names were the same (which doesn't do that, and it seems you may have learned that since and are revising history). A moderator should have access to your original unedited post, although I'm not interested in pursuing it any further. It is pretty clear you have become (or have always been) a forum troll, and I will be ignoring your threads and posts from now on.
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

29 Nov 2016, 13:53

boiler wrote: I am a troll ...!
off course you are a troll, and a liar too!

The "original unedited post" will confirm that you are a little liar!

I just edited the title and add this This is an example showing how to make the "Main Script" to get "Static Variables" values from functions, avoiding so declaring "Global" variables inside functions! to the main thread! Nothing more!
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

29 Nov 2016, 16:28

As you've been told before, your originally post did not contain something which could be considered as an 'useful script'. Just LOL and join a LOL-Community. You'll be the "Pro" you want to be, there.

Also:
V for Vendetta wrote:off course you are a troll, and a liar too!
You just got your first warning.
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

29 Nov 2016, 16:31

just me wrote:As you've been told before, your originally post did not contain something which could be considered as an 'useful script'. Just LOL and join a LOL-Community. You'll be the "Pro" you want to be, there.

Also:
V for Vendetta wrote:off course you are a troll, and a liar too!
You just got your first warning.
My first warning of what?

he said I edited the script, but I didn't, so it's a lie, he's lying, so he's a liar!
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

29 Nov 2016, 16:42

just me wrote: As you've been told before, your originally post did not contain something which could be considered as an 'useful script'. Just LOL and join a LOL-Community. You'll be the "Pro" you want to be, there.
It's obvious an "Useful" Script!

You are the one who should join a "LOL-Community"! You are a "Pro" making people Laughing Out Loud!

LolLolLolLolLolLolLolLolLolLolLolLolLolLolLolLolLolLol...!
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

30 Nov 2016, 06:04

If you would be so kind as to add more text then everyone would be happy.
As it is now nobody understands that much anyways.
Recommends AHK Studio
User avatar
tank
Posts: 3130
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

30 Nov 2016, 09:02

V for Vendetta wrote:he said I edited the script, but I didn't, so it's a lie, he's lying, so he's a liar!
V for Vendetta wrote:Last edited by V for Vendetta on Tue Nov 29, 2016 12:04 pm, edited 3 times in total.
Please review the terms of this board
AutoHotkey Community - Registration wrote:You agree that “AutoHotkey Community” have the right to remove, edit, move or close any topic at any time should we see fit.
the folks you are calling liars at least a few are moderators of this board nearly its entire lifetime. I have found them rarely on the wrong side of a point tho it does happen. had it not been for the log of your edits i might have been inclined to go with you here. but then you called them liars and forget that all activity on this board is in fact logged.

I am happy to back someone if they are right against other moderators, but your clearly just trying to :trollface:
You had your laugh now be done with it.
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Avoid declaring Global Variables Inside functions by declaring Static Variables instead

30 Nov 2016, 11:03

tank wrote: the folks you are calling liars ...
the folks? please ... the one person, to be objective ...!
tank wrote: and forget that all activity on this board is in fact logged.
Please don't make me Laugh!

Yesterday I was searching for different ways to make the "Main Script" get "Static Variables" values from functions, and I just happen to find this "Thread" of mine in the "Help" section! The 3 thread (not script) editions was made yesterday!

I edited the "thread" not the "Script"! The "original unedited post" can prove this! But anyway, if you believe me or not, it's not my problem, it's your problem!
tank wrote: but your clearly just trying to :trollface:
Haha, you really make me :trollface:, my friend! LolLolLolLolLolLolLolLolLol...!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Marium0505, mcl and 345 guests