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 UDT to function not updating outside of function

Author
Message
Harlequin
14
Years of Service
User Offline
Joined: 22nd Mar 2010
Location: Melbourne (AU)
Posted: 13th Oct 2014 15:32 Edited at: 13th Oct 2014 15:48
Hi all,

AGKv2 Alpha 7.1 (I think)

probably doing something wrong here, but the UDT does not appear to be updating from within a function

Basically if I print the OrbitName.Name within the "Orbiting" function I get the "New Sun", if I print Sun1.Name outside the function it remains as "Sun", but if I call OribitName.Name within "Orbiting" function when I have NOT assigned a new name I get "Sun" (so it is passing the UDT to the function)

The calls I am making are below... Also these are all in separate source files all joined in main with #include



It's late, I'm tired, out of coffee and now I'm wet.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 13th Oct 2014 17:28
This is about scope. AppGameKit Basic now allows call by reference as well as call by value.

Call by value is how you declared Orbiting(). When something is passed by value a copy of the thing you have passed is created inside the function, and any changes you make are lost when the function exits, and the original is left unchanged. Call by value is the default in most computer languages.

Call by reference means that you are actually working on the original, not a copy, and anything you change is changed in the original.

function Orbiting(Orbitname ref as typeOrbit)

This is a powerful feature which brings AppGameKit Pascal into line with more advanced languages. But use it with caution - if you are careless you can screw up vital data.

It should be impossible to pass a constant as a ref parameter, but I haven't tried it!

When to use call by value:

Most of the time. You want to do some operation on the data passed and do something else with it without changing the original. (The AppGameKit Basic help file examples are wrong about function return values in some cases, actually).

When to use call by reference:

When you really do want to change the original data. You will do this infrequently, and often when you want to change several fields in one go, since functions can only return one result.

-- Jim - When is there going to be a release?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 13th Oct 2014 17:40
this "pass by reference" is in offline help here
file:///D:/AGKV2A7/IDE/Help/guides/12_array_changes.htm

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini
Harlequin
14
Years of Service
User Offline
Joined: 22nd Mar 2010
Location: Melbourne (AU)
Posted: 13th Oct 2014 23:57
thanks guys,

3 little letters had me totally, In this case I do need to change all the elements in the UDT so by ref worked perfectly

Also I was not seeing UDT's as a type/alternate array so I also learnt another thing.

2 for one...!

It's late, I'm tired, out of coffee and now I'm wet.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 14th Oct 2014 00:45
Glad to help.

Under many circumstances pass by reference may be faster, even if you don't want to change the data, because only a pointer is passed, not all the structure. AppGameKit Basic has its own internal rules, but generally call by value will push an entire "snapshot" of the parameters passed onto the stack.

For example, if you pass a string to a function, the string will normally be copied to the stack. That copy will be used inside the function and when the function exits the top of stack pointer will be moved down.

For that reason, languages like modern Pascal allow you to have the best of both worlds for your desired use, with the "const" keyword.That means "Don't put it all on the stack, give me a read-only pointer." Smart compilers will do this anyway, but they need to analyse what you are doing inside the function scope. But, if you are producing things for third-party use, "locking" the parameters may avoid mistaken changes.

-- Jim - When is there going to be a release?

Login to post a reply

Server time is: 2024-11-25 09:33:36
Your offset time is: 2024-11-25 09:33:36