AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Help] Return function (https://forums.alliedmods.net/showthread.php?t=172511)

jppmarujo 11-21-2011 14:23

[Help] Return function
 
Hey guys,

So, i'm trying to lear pawn scripting and i'm doing it via the documentation stated in this forum, with the help of the Allied Modders Wiki.

I'm dividing this into sectors to better understand the parts that compose the whole.

I'm stuck in here, as i don't fully understand what's the return function job.

Code:

//This is a function that takes no parameters and returns 1. //When activated, it uses the (non-existant) print function. show() {    print("Hello!")      return 1  //End, return 1 }


If it didn't have the return function, wouldn't it be the same result? Being, printing the word "Hello"!

Sorry for my nooby question, but i like to make sure i fully understand each part to not make basic errors when i really start programming.



Thanks for your help in advance.

Xellath 11-21-2011 14:40

Re: [Help] Return function
 
It doesn't do much in this case; other than returning 1 if above code is executed correctly.

Since it returns 1 if the code is executed correctly, the function would allow you to check whether it succeeded or not:
Code:
if( show( ) ) {     Print( "Hello world was printed successfully!" ); }

TheArmagedon 11-21-2011 14:44

Re: [Help] Return function
 
for example,
if you dont write " return 'anything' "
the function will continue, function will 'never' stop and this will generate errors.
And to prevent these mistakes you use the return to "stop"/"end" the function.

Xellath 11-21-2011 15:05

Re: [Help] Return function
 
As said above, you can return to block the code from continuing;

Code:
show( true ); show( false ); show( bool:print ) {     if( print )     {         Print( "Bool print is true and the function is returned" );                 return;     }         // code is never executed if print is true     Print( "Bool print is false" ); }

jppmarujo 11-21-2011 15:23

Re: [Help] Return function
 
Thank you, now i fully understand the return function. To resume, it is to halt the the function to that section of code.

And what about the number? Like in the case i posted, it ends with return '1'.

What's the function of the number '1'? Thank you for your clarifying help.

Xellath 11-21-2011 15:41

Re: [Help] Return function
 
It's simply a number. It can be any number. It can allow you to check whether the function was successfully executed or not for example, or perhaps check if it's the 'type' you wanted.

Code:
enum _:ReturnType {     return_type_1,     return_type_2,     return_type_3 }; ReturnType:function( type ) {     return type; } new ReturnType:_return = function( return_type_1 ); if( _return != return_type_1 ) {     // code is never executed since we entered return_type_1 } // a boolean value (they're actually just 1 and 0, but tagged as bools); // true = 1 // false = 0 // a function returning true or false depending on if client is connected bool:function( id ) {     if( !is_user_connected( id ) )     {         return false;     }         return true;         // could also be done like this:     return bool:is_user_connected( id ); // is_user_connected actually returns 1 if client is connected } if( function( ) == true ) // is the same as if( function( ) ) // if( true ) // could be detagged if( _:function( ) ) // if( 1 )

You could check what I did in an include file I made for a guy here on the forums. It returns certain values in case the weapon is invalid, or if client is invalid.. etc. Here's the link: http://forums.alliedmods.net/showpos...6&postcount=12


All times are GMT -4. The time now is 08:25.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.