Variablen Deklaration / Schlüsselwörter

Für allgemeine Fragen zur Programmierung mit PureBasic.
Benutzeravatar
#NULL
Beiträge: 2235
Registriert: 20.04.2006 09:50

Variablen Deklaration / Schlüsselwörter

Beitrag von #NULL »

[Variable Declaration Keywords]
Weil das Thema gerade wieder aufkam (http://www.purebasic.fr/german/viewtopi ... 15#p344515) und ich vor ein paar Wochen mal manches davon zusammengetragen hatte weil ich es mir nicht immer merken konnte:

Code: Alles auswählen

EnableExplicit

Debug "----------------------------- 1:"

; protected can shadow global

Global d1

Procedure p1()
  Protected d1
  d1 + 1
  Debug d1
EndProcedure

p1()            ; 1
P1()            ; 1
Debug d1        ; 0

Debug "----------------------------- 2:"

; static can shadow global

Global d2

Procedure p2()
  Static d2
  d2 + 1
  Debug d2
EndProcedure

p2()            ; 1
P2()            ; 2
Debug d1        ; 0

Debug "----------------------------- 3:"

; define cannot shadow global

; Global d3
; 
; Procedure p3()
;   Define d3   ; error: Variable already declared with a different scope
;   d3 + 1
;   Debug d3
; EndProcedure
; 
; Define d3     ; error: Variable already declared with a different scope

Debug "----------------------------- 4:"

; procedure-local define does not conflict with main-local define and behaves like protected

Define d4

Procedure p4()
  Define d4
  d4 + 1
  Debug d4
EndProcedure

p4()            ; 1
P4()            ; 1
Debug d4        ; 0

Debug "----------------------------- 5:"

; access to globals is implizit in procedures, even with EnableExplicit

Global d5

Procedure p5()
  d5 + 1
  Debug d5
EndProcedure

p5()            ; 1
P5()            ; 2
Debug d5        ; 2

Debug "----------------------------- 6:"

; access to main-locals in procedures needs explicit Shared

Define d6

Procedure p6()
  Shared d6
  d6 + 1
  Debug d6
EndProcedure

p6()            ; 1
P6()            ; 2
Debug d6        ; 2

Debug "----------------------------- 7:"

; main-locals need to be declared before procedure to be accessible

; Procedure p7()
;   Shared d7     ; error: With 'EnableExplicit', variables have to be declared
;   d7 + 1
;   Debug d7
; EndProcedure
; 
; Define d7
; 
; p7()
; P7()
; Debug d7

Debug "----------------------------- 8:"

; main-globals need to be declared before procedure to be accessible

; Procedure p8()
;   d8 + 1        ; error: With 'EnableExplicit', variables have to be declared
;   Debug d8
; EndProcedure
; 
; Global d8
; 
; p8()
; P8()
; Debug d8

Debug "----------------------------- 9:"

; Global in procedure creates main-global

Procedure p9()
  Global d9
  d9 + 1
  Debug d9
EndProcedure

Procedure p99()
  Debug d9
EndProcedure

p9()            ; 1
P9()            ; 2
Debug d9        ; 2
p99()           ; 2

Debug "----------------------------- 10:"

; Shared does not care if already global

Global d10

Procedure p10()
  Shared d10    ; ok
  d10 + 1
  Debug d10
EndProcedure

p10()           ; 1
P10()           ; 2
Debug d10       ; 2

Debug "----------------------------- 11:"

; you cannot define main-globals and main-locals together inside a procedure

Global d11_global_a           ; main-global
Define d11_local_a            ; main-local

Procedure declareVariables()
  Global d11_global_b         ; main-global
  Define d11_local_b          ; procedure-local
  ;Shared d11_local_c         ; error: With 'EnableExplicit', variables have to be declared
EndProcedure

Debug "----------------------------- 12:"

; Define.<type> without variable to change the default type has been removed as of pb 5.60, 
; the documentation is out-of-date (pb 5.61) but it's mentioned in the history/changelog

;Define.w                    ; Syntax error
;Define d12

Debug "----------------------------- 13:"

; Global does not complain about redeclaration as long as the type does not change. That includes
; the case where the second declaration does not use any type and intends to use the default type

Global g13.a = 255           ; somewhere
; ...
Global g13                   ; assume integer and zero-initialization?
g13 + 1
Debug g13                    ; 0

Procedure p13()
  Global g13
  g13 + 1
  Debug g13                  ; 1
EndProcedure
p13()

Debug "----------------------------- 14:"

; Define does not complain about redeclaration but gives error for different implicit type

Define d14 = 100             ; somewhere
; ...
Define d14                   ; assume zero-initialization?
d14 + 1
Debug d14                    ; 101

Define d14b.a
;Define d14b                 ; error: Variable already declared with another type

Procedure p14()
  Define d14 = 200
  ; ...
  Define d14                 ; assume zero-initialization?
  d14 + 1
  Debug d14                  ; 201
  
  Define d14_.a = 200
  ;Define d14_               ; error: Variable already declared with another type
EndProcedure
p14()

Debug "----------------------------- 15:"

; Protected does not allow redeclaration at all

Procedure p15()
  Protected d15
  ;Protected d15             ; error: Local variable already declared
EndProcedure

Debug "----------------------------- 16:"

; Threaded

; threads share variables by default (one variable instance for all threads)

Global d16_global = 100       ; main-global
Define d16_local  = 200       ; main-local

d16_global + 1
d16_local + 1

Debug d16_global                      ; 101
Debug d16_local                       ; 201

Procedure p16(param)
  Shared d16_local
  d16_global + 1
  d16_local + 1
  Debug "thread: " + d16_global       ; 102
  Debug "thread: " + d16_local        ; 202
EndProcedure

Define thread = CreateThread(@p16(), 0)
WaitThread(thread)

Debug "----------------------------- 17:"

; Threaded ..is like Global, but with separate variable instances per thread
; use Threaded instead of Global for that purpose, don't try to combine Threaded/Define/Shared etc.. in  a declaration

Threaded d17_threaded_a = 100       ; initialized for each thread instance
Threaded d17_threaded_b

d17_threaded_a + 3                  ; affects only main thread instance
d17_threaded_b = 500                ; affects only main thread instance
d17_threaded_b + 3                  ; affects only main thread instance

Debug d17_threaded_a                ; 103
Debug d17_threaded_b                ; 503

Procedure p17(param)
  d17_threaded_a + 1
  d17_threaded_b + 1
  Debug "thread: " + d17_threaded_a ; 101
  Debug "thread: " + d17_threaded_b ; 1
EndProcedure

Define thread = CreateThread(@p17(), 0)
WaitThread(thread)

my pb stuff..
Bild..jedenfalls war das mal so.
Benutzeravatar
NicTheQuick
Ein Admin
Beiträge: 8677
Registriert: 29.08.2004 20:20
Computerausstattung: Ryzen 7 5800X, 32 GB DDR4-3200
Ubuntu 22.04.3 LTS
GeForce RTX 3080 Ti
Wohnort: Saarbrücken
Kontaktdaten:

Re: Variablen Deklaration / Schlüsselwörter

Beitrag von NicTheQuick »

Da würde ich gerne auch noch mein Erklärungsvideo dazu posten: [PB] Grundlegende Schlüsselwörter
Bild
Benutzeravatar
diceman
Beiträge: 347
Registriert: 06.07.2017 12:24
Kontaktdaten:

Re: Variablen Deklaration / Schlüsselwörter

Beitrag von diceman »

Ein wirklich tolles Tutorial-Video, ihr dürft mich jetzt auch zum Team "Protected" zählen. :mrgreen:
Sowas wie Static habe ich mir in Blitzbasic manchmal gewünscht (zum Beispiel wenn ich zählen wollte/mußte, wie oft eine Prozedur aufgerufen wurde, bzw. in der Prozedur selbst abgefragt habe, ob sie das erste Mal aufgerufen wurde, oder ob es sich um einen wiederholten Aufruf handelte); das mußte ich dann immer etwas ungeschickt über globale Deklarationen lösen. Es funktionierte, aber wirklich glücklich war ich damit nicht.
Threaded brauche ich erstmal nicht, denke ich. Trotzdem interessant zu wissen, was alles so geht.
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
Benutzeravatar
RSBasic
Admin
Beiträge: 8022
Registriert: 05.10.2006 18:55
Wohnort: Gernsbach
Kontaktdaten:

Re: Variablen Deklaration / Schlüsselwörter

Beitrag von RSBasic »

@NicTheQuick
Machst du irgendwann noch weitere PB-Videos?
Aus privaten Gründen habe ich leider nicht mehr so viel Zeit wie früher. Bitte habt Verständnis dafür.
Bild
Bild
Antworten