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.

Newcomers AppGameKit Corner / How do I return values with functions?

Years of Service
User Offline
Joined: 14th Apr 2016
Posted: 21st Apr 2016 04:02
I can't seem to return values with functions in AGK. I'm not sure what the limitations of functions are in AGK2, so if there is a guide to how to implement them properly exists, I could really use it.
AGK Developer
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 21st Apr 2016 05:38
Van B
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 21st Apr 2016 13:10 Edited at: 21st Apr 2016 13:12
Yeah, AppGameKit is actually quite straightforward with function returns, doesn't enforce rules that other languages might.

For example, if you wanted to calculate the distance between 2 sprites....(untested)

Just tag on the variable that you're returning to the Endfunction line.
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 21st Apr 2016 17:34 Edited at: 30th Apr 2016 16:04
(slightly modified from another post of mine)

Returning values from a function can be done two ways. Either through returning a single entity, or by referencing one or more entities. These entities can be a variable, array or datatype.

for instance you can pass a variable or datatype to a function so that it can do stuff with it and return a value.


This will print 7

With datatypes you can bunch many related variables together and pass through in the same manner:

This will print 7 and 12

The other method is to reference the original datatype when passing it into the function.

Like this:

Again this will print 7 and 12. Remove the reference however, and nothing is done to the datatype outside of the function, so it will return 0 and 0. The advantage of this method is that it'll keep the changes you make even if you pass into it several datatypes, unlike the first method where you can only return one datatype.

like so:

This prints 7, 12, 10 and 5. Having made changes to both the variables and results datatypes inside the function.

Arrays work in the same way, you just need to attach an open and close square bracket when passing in and out of a function, like so: function doStuff(array ref as myArray[]).

For multi dimensional arrays, each set of brackets represent a dimension, like so: function(3dArray ref as my3DArray[][][])

Login to post a reply

Server time is: 2020-02-26 02:12:24
Your offset time is: 2020-02-26 02:12:24