Raised This Month: $ Target: $400
 0% 

[Help] Return function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jppmarujo
Junior Member
Join Date: Nov 2011
Location: Portugal
Old 11-21-2011 , 14:23   [Help] Return function
Reply With Quote #1

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.
jppmarujo is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 11-21-2011 , 14:40   Re: [Help] Return function
Reply With Quote #2

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!" ); }
__________________
Achievements API - a simple way for you to create your OWN custom achievements!
Xellath is offline
TheArmagedon
Senior Member
Join Date: Sep 2010
Location: Unknown Source
Old 11-21-2011 , 14:44   Re: [Help] Return function
Reply With Quote #3

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.
TheArmagedon is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 11-21-2011 , 15:05   Re: [Help] Return function
Reply With Quote #4

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" ); }
__________________
Achievements API - a simple way for you to create your OWN custom achievements!
Xellath is offline
jppmarujo
Junior Member
Join Date: Nov 2011
Location: Portugal
Old 11-21-2011 , 15:23   Re: [Help] Return function
Reply With Quote #5

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.
jppmarujo is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 11-21-2011 , 15:41   Re: [Help] Return function
Reply With Quote #6

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
__________________
Achievements API - a simple way for you to create your OWN custom achievements!
Xellath is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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