Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
returning values (return command)
RaideX Offline
Member

Posts: 212
Threads: 33
Joined: May 2013
Reputation: 7
#1
returning values (return command)

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: (Select All)
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: (Select All)
void function (string &in asParentstring &in asChildint alState) {
if (
alState == 1) {
PlaySoundAtEntity(""sound.snt"Player"0false);
}

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


If you don't draw first, you don't get to draw at all... -The False Shepherd
01-02-2014, 07:35 PM
Find
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#2
RE: returning values (return command)

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: (Select All)
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.

01-02-2014, 09:36 PM
Find
RaideX Offline
Member

Posts: 212
Threads: 33
Joined: May 2013
Reputation: 7
#3
RE: returning values (return command)

(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: (Select All)
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.

If you don't draw first, you don't get to draw at all... -The False Shepherd
01-02-2014, 09:56 PM
Find
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#4
RE: returning values (return command)

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

PHP Code: (Select All)
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.

01-03-2014, 03:52 AM
Find
Javist Offline
Junior Member

Posts: 46
Threads: 3
Joined: Sep 2010
Reputation: 0
#5
RE: returning values (return command)

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

unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep.
(This post was last modified: 01-03-2014, 09:47 AM by Javist.)
01-03-2014, 09:34 AM
Find




Users browsing this thread: 1 Guest(s)