Frictional Games Forum (read-only)
returning values (return command) - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: returning values (return command) (/thread-24281.html)



returning values (return command) - RaideX - 01-02-2014

hello again,

could someone maybe explain and declare me what the return function in some instances do excectly?

All i know is that return "returns" a value and ends a function. But in an amnesia example there are only void datatypes for functions (atleast most of the time). So what excectly would be the difference between the following two if statements and how/when would they be used?

PHP Code:
void function (string &in asParentstring &in asChildint alState) {
if (
alState == 1) {
PlaySoundAtEntity(""sound.snt"Player"0false);
return; }

if (
alState == -1) {
PlaySoundAtEntity(""sound2.snt"Player"0false); 
return; }


PHP Code:
void function (string &in asParentstring &in asChildint alState) {
if (
alState == 1) {
PlaySoundAtEntity(""sound.snt"Player"0false);
}

if (
alState == -1) {
PlaySoundAtEntity(""sound2.snt"Player"0false); 
}




RE: returning values (return command) - Daemian - 01-02-2014

In that case, return is used to prevent the code from reading the rest of the code. It stops and continues at the parent function that called it.

If you had your function declared as string or whatever instead of void, you could return data of that type by using return value


PHP Code:
string Message () 
{    
     return 
"hello";




At least that's how I use it and works as expected.
No idea if here has another use other than that.


RE: returning values (return command) - RaideX - 01-02-2014

(01-02-2014, 09:36 PM)Amn Wrote: In that case, return is used to prevent the code from reading the rest of the code. It stops and continues at the parent function that called it.

If you had your function declared as string or whatever instead of void, you could return data of that type by using return value


PHP Code:
string Message () 
{    
     return 
"hello";




At least that's how I use it and works as expected.
No idea if here has another use other than that.

i'm not quite getting the part with the "stops reading the code". What exactly would be the difference here? I mean, the whole code in the brackets is going to be executed no matter if it returns it or not.

I think i'll need some more examples on the first bit. The second one is quite self explanatory.


RE: returning values (return command) - Daemian - 01-03-2014

In that function, return is not doing much.
But look at this one below,

PHP Code:
void function (string &in asParentstring &in asChildint alState) {
if (
alState != 1) { return; }
     
GiveMoney();


It's gonna run GiveMoney function only when alState is 1.
When alState is not 1, return is read and it says "I'm out of here", and skips the rest of the code.

You should try it.


RE: returning values (return command) - Javist - 01-03-2014

In fact, the functions that you're listed above are identical (in action).
Regardless of return type - current function will be immediately terminated when return operator is executed, without executing the remaining statements.

http://ideone.com/vymeMf