How to update a variable in a procedure

Everything else that doesn't fall into one of the other PB categories.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

How to update a variable in a procedure

Post by coco2 »

I'm still a bit of a beginner, so sorry if this has been asked before.

We can't pass a pointer to a primitive (integer etc) in a procedure so how do we change a variable? Make it global?
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to update a variable in a procedure

Post by TI-994A »

coco2 wrote:...can't pass a pointer to a primitive (integer etc) in a procedure so how do we change a variable?

Code: Select all

Procedure passByRef(*myInt, *myString)
  PokeI(*myInt, 456)
  PokeS(*myString, "World")
EndProcedure

myInt = 123
myString.s = "Hello"

Debug myInt
Debug myString

passByRef(@myInt, @myString)

Debug myInt
Debug myString
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: How to update a variable in a procedure

Post by coco2 »

Thanks I guess I'll have to do it that way then. Any idea why PB doesn't have pointers to primitive types?
User avatar
Demivec
Addict
Addict
Posts: 4090
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to update a variable in a procedure

Post by Demivec »

coco2 wrote:Thanks I guess I'll have to do it that way then. Any idea why PB doesn't have pointers to primitive types?
Here is a modified version of TI-994A's sample. His example covers one way of doing things, here's another. I think this was more along the lines of what you were asking.

Code: Select all

;Structures for primitive PureBasic types (and each of their solitary sub-elements):
;BYTE\b.b, ASCII\a.a, UNICODE\u.u, LONG\l.l, INTEGER\i.i, QUAD\q.q, STRING\s.s
;
;To pass a reference to a string it is often easier to assign it to the same
;structure that PureBasic uses.  If this is not done more steps are involved.
;

Procedure passByRef(*myInt.Integer, *myString.string)
  *myInt\i = 456
  *myString\s = "World"
EndProcedure

myInt = 123
myString.string\s = "Hello"


Debug myInt
Debug myString\s

passByRef(@myInt, @myString)

Debug myInt
Debug myString\s
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: How to update a variable in a procedure

Post by coco2 »

I see so PureBasic does pass pointers to primitive types, you just have to do it a special way
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to update a variable in a procedure

Post by TI-994A »

coco2 wrote:...so PureBasic does pass pointers to primitive types...
Not really. Those are convenience structures; not native types.

Here's another approach:

Code: Select all

Procedure passByRef(*myInt, *myString)  
  *intStructure.INTEGER = *myInt
  *stringStructure.STRING = @*myString
  *intStructure\i = 456
  *stringStructure\s = "World!"  
EndProcedure

myInt = 123
myString.s = "Hello"

Debug myInt
Debug myString

passByRef(@myInt, @myString)

Debug myInt
Debug myString
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply