Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Passing Vars to Functions as reference

Author
Message
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 24th Apr 2018 11:53 Edited at: 24th Apr 2018 12:37
This works:
MyFunction( TheVar REF AS INTEGER[] )

but, this does not:
MyFunction( TheVar REF AS INTEGER )

"error: Cannot pass basic data types by reference, only arrays and types support the ref keyword"

Is it possible to pass Integers & Floats to functions as a reference?
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 24th Apr 2018 12:30
Quote: ""error: Cannot pass basic data types by reference, only arrays and types support the ref keyword"

Is it possible to pass Integers & Floats to functions?"


Yes you can pass Integers and floats to functions... but you cant pass them by reference. (As the error states above)

MyFunction( TheVar REF AS INTEGER ) //WONT WORK
MyFunction( TheVar AS INTEGER ) //WORKS OK
MyFunction( TheVar REF AS INTEGER[] ) //WORKS OK
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 24th Apr 2018 16:29 Edited at: 24th Apr 2018 16:30
you can use a workaround

type myinteger
int as integer
endtype

local TheVar as myinteger
TheVar.int = 123

MyFunction(TheVar)

Function MyFunction( TheVar REF AS myinteger)
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : NVIDIA (390.65) GeForce GTX 1050 Ti : Mac mini OS High Sierra (10.13)
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 24th Apr 2018 19:55
Markus wrote: "
Abusing a type for the sake of referencing variables
"


Don't understand me wrong Markus, you're a very good programmer and you do amazing things on this forum.
But IMHO, this was the single worst advice you could give for this question.

The fact that variables can't be used as a reference parameter in functions is actually one of the good design decisions the AppGameKit made.
Passing variables as reference is almost always a bad idea, passing multiple is worse, encapsulating them in an 'object' (Type in AGK) is the worst. You have no idea what's happening to those variables when calling the function. This could stretch things to make one supertype with every single variable included, because it's easy to pass, right?

So, Nieb, why do you want to pass as reference? I'm sure there are better options from a design perspective.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 25th Apr 2018 12:42
Quote: "The fact that variables can't be used as a reference parameter in functions is actually one of the good design decisions the AppGameKit made."

its inconsequent because something you allow by reference and others not (bad design). by reference is optional and needed always.
i not said you useing one variable inside.
in daily business you would use much more data in a struct as function input for using inside this function.
i said its a workaround and a solution, paul will not make it consistent.
consider also that share a pointer to a struct & object is much faster instead of copying data in mass.
i have always a idea what my app doing because "ref" is used it is very clear that it affect the calling function above.

sure if nieb said what he will do we can look for a better solution ..




AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : NVIDIA (390.65) GeForce GTX 1050 Ti : Mac mini OS High Sierra (10.13)
Increase
7
Years of Service
User Offline
Joined: 21st Feb 2017
Location:
Posted: 26th Apr 2018 01:47
You can reference single variables at the end of the function.

e.g.



Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th Apr 2018 15:39
Quote: "You can reference single variables at the end of the function."

You're still passing by value there, not by reference.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Increase
7
Years of Service
User Offline
Joined: 21st Feb 2017
Location:
Posted: 26th Apr 2018 23:22 Edited at: 26th Apr 2018 23:24
I woudn't mind to see it included in the function head as reference either... - just pointing out what seems to be the easiest way till now.
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 27th Apr 2018 00:01
You could always define your variable as Global and use it directly in your function without having to include it as a parameter at all:

Global MyVar as integer

do
myfunc()
print(MyVar)
sync()
loop

function myfunc()
Inc MyVar
endfunction



*runs and hides*
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 27th Apr 2018 03:03
*Gasp* how dare you suggest globals!
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 27th Apr 2018 22:02
Quote: "You have no idea what's happening to those variables when calling the function."


You should know, if not, your a bad programmer or a good programmer that has written bad code, either way, you should have some idea of your variable states when calling a function.

*not calling you personally a bad programmer, just saying a decent dev will anticipate this in advanced.

Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 28th Apr 2018 00:32
Oh the fun of chasing a type up and down the function-call hierarchy to find the places it isn't passed by reference when you suddenly decide that deep down in one branch of that pyramid you really do need it to be mutable up and down that branch.

Anyhow, since in AppGameKit you can only return a single parameter, be it a variable, array or struct, it is often necessary to to pass as reference if you want what happens to that array or struct to be exported out of its' function scope. Besides, it is faster than playing pass the bucket making a local copy (import) and then returning that copy in what is essentially another copy operation. Passing by reference is just giving the function a memory-pointer to the array or struct instance, and allowing the function to go wild on it if so required. Saves quite the few CPU cycles on larger data-sets - and retains a modicum of security as only functions with access to the array or struct instance can modify it, unlike globals.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 28th Apr 2018 00:47
^^ pick a system and stick to it, I like to pass by ref so I am always working on the 'live' type, this helps to anticipate what the next function should do, combined with a coherent state machine and constant states, a few sub-helper functions of course just to tidy code and prevent overtype (repeated code)

the problem is not "should I pass to this function as ref" more like "should I ref this project", with benefits and disadvantages weighed up if its complex I ref the entire thing, consistency is the key.

Login to post a reply

Server time is: 2024-11-23 21:29:29
Your offset time is: 2024-11-23 21:29:29