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 / Tip for beginners, passing by reference

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Mar 2024 06:35
I know many folks here already understand the difference between passing by reference and passing by value. For younger coders who don't, I thought I'd give a brief explanation why it's important to know the difference and when to use it.

By default, when you call a function with parameters, the values you pass to it are copied into the function. For most of the time, this is ok. But sometimes you want the function to enact changes upon the variable that's being used a parameter. You could do some calculation in the function, return it, then assign it to your variable and overwrite its previous value. For example:



Another way is to pass your variable by reference. What this means is you're not passing the value of X into the function anymore but instead the memory address that points to the value X is assigned. That means, anything you do to X in the function happens to that variable outside of the function as well. To pass by reference you need to use the keyword "ref as" in the function header.




Now here's an important tip, passing by reference doesn't always have to be about altering variables. Remember that passing by value copies that value to a new memory address. Not a big deal for a few integers or strings, but what if you had a very large array? All those elements would be copied as well. That's more memory used and it can be costly the larger the array becomes. Or, if you pass the array by reference, the speed of the function call is no longer affected by the size of the array. Here's an example you can run to test the speed difference for yourself.

The function testA() will pass an array of 10k integers by value while testB() will pass the array by reference. It'll call the function 10k times to better show the speed difference. On my computer, testA takes about 50ms while testB only takes 1ms. As you increase the numbers you'll see a far greater speed improvement by using reference over value. Even with 20k elements called 50k times, referencing the array still only takes about 4ms while trying to pass it by value takes an astonishing 530ms! While the number of loops is rather extreme and not likely to happen in your program, its point is to demonstrate the advantage of using references. And an array of 20k elements is completely plausible, just considering storing the data for a tile map. 100x100 map isn't that large you're already at 10k elements. Make each element a UDT for holding multiple values per tile and the memory size of your array can grow very quickly. Pointing to your data will always be faster than copying it.


Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda
Pixel-Perfect Collision

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 11th Mar 2024 19:38
Excellent tute.
Only thing i noticed is you have an integer passed as ref. Only arrays and types can be passed by reference
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Mar 2024 05:11
Oops, good catch
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda
Pixel-Perfect Collision

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-11-21 09:36:26
Your offset time is: 2024-11-21 09:36:26