Frage zu func()

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Frage zu func()

Re: Frage zu func()

Post by just me » 28 Nov 2023, 04:25

Moin @effel,

in Deinem Beispiel brauchst Du global test nicht nur, um die Variable test auch außerhalb der Funktion ändern zu können. Ohne global würde die Funktion immer nur 1 liefern, weil test am Funktionsende als lokale Variable immer gelöscht werden würde.

Wenn Du eine nicht-globale Variable nutzen willst, würde ich folgendes Konstrukt vorschlagen;

Code: Select all

msgbox % test() ; 1
msgbox % test() ; 2
msgbox % test() ; 3
msgbox % test() ; 4
msgbox % test(True) ; 1

Test(Init := False) {
	Static Test:= 0
	If Init
		Test := 0
	Return ++Test
	; alternativ (Ternary):
	; Return (Init ? Test := 1 : ++Test)
}

Frage zu func()

Post by effel » 27 Nov 2023, 15:20

Guten Abend,
ist global die einzige Möglichkeit wenn ich die Variable test leeren möchte?

Code: Select all

msgbox % test() ; 1
msgbox % test() ; 2
msgbox % test() ; 3
msgbox % test() ; 4
test := 0
msgbox % test() ; 1
test() {
global  test
test++
return test
}

Top