Raised This Month: $32 Target: $400
 8% 

Compare two variables in a return?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
deprale
Senior Member
Join Date: Oct 2018
Location: Leeds
Old 01-30-2019 , 11:45   Compare two variables in a return?
Reply With Quote #1

I've searched for quite a bit (about 1 hour), of any amxx documentations regarding "return"

I've seen some examples on the forums
PHP Code:
stock GetRandomPlayer(const predicateFuncName[]) {
    new 
player[1];
    return 
GetRandomPlayers(player1predicateFuncName) ? player[0] : 0;

How does this "return" in this case work? I don't get it. What does '?' and ':' stand for and what is the general rule of working with one?

Let's say I've got this case:
PHP Code:
new a,b;
3
4
public Whatever()
{
if(
HigherNumber()==a)
{
//blablabla
}
else
//blablablabla
}

stock HigherNumber()
{
if (
a>b) return a;
if (
b>a) return b;

Why in my example it doesn't work? It still says it has to return a value, but why does it work in the first example, and HOW?

As I said I would've researched this on my own but I couldn't find any documentation about it, writing "return" in the amxmodx api just shows what each of the amxmodx natives do, they "return" a certain value but it doesn't show or specify anywhere how to use "return' for non-natives. Maybe I just don't get how "return" works in general, I know of FMRES_SUPERCEDE, HAM_IGNORE, PLUGIN_HANDLED, PLUGIN_CONTINUE, PLUGIN_HANDLED_MAIN... but I do not understand how the "DIY" return works.

Maybe I'm just blind, link it, or explain, please!

Thanks!
__________________

Last edited by deprale; 01-30-2019 at 12:00.
deprale is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 01-30-2019 , 12:43   Re: Compare two variables in a return?
Reply With Quote #2

You need to pass arguments.

Code:
new a,b; a = 3 b = 4 new HigherNumber = max(a, b);

Quick reference.
https://wiki.alliedmods.net/Pawn_Tutorial

"?" and ":" it's basically "else" and "else if", but simplified.

Code:
a == b ? server_print("a = b") : server_print("a != b");

it's same as

Code:
if (a == b) {     server_print("a = b"); } else {     server_print("a != b"); }
__________________









Last edited by CrazY.; 01-30-2019 at 12:47.
CrazY. is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-30-2019 , 12:54   Re: Compare two variables in a return?
Reply With Quote #3

PHP Code:
stock HigherNumber()
{
if (
a>b) return a;
if (
b>a) return b;

Compiler sees that you are returning only on certain conditions and complains that in some cases your function could return nothing.
And here, that's the case. If a == b then what is your function going to return?

You could write it like this:
PHP Code:
stock HigherNumber(ab)
{
    if(
b
    {
        return 
a
    
}

    return 
b

if a > b fails then a <= b so in that case you should return b. There's no need for a second if check.

Or if you want to use the ternary operator(?):
PHP Code:
stock HigherNumber(ab)
{
    return (
b) ? b

In general it's a good idea to stay away from ?, as it is less readable and more error prone that regular if and else statements.

Note that even in this case:
PHP Code:
stock HigherNumber(bool:b)
{
    if(
== true)
    {
        return 
1
    
}
    else if(
== false)
    {
        return 
0
    
}

the compiler will still complain.
We know that a boolean is either true or false, so either b is true or b is false, the function will always return. But the compiler is dumb and doesn't know that, so it will still issue a warning.

To avoid the warning add either a dummy return at the end(which will never be reached) to make the compiler shut up:
PHP Code:
stock HigherNumber(bool:b)
{
    if(
== true)
    {
        return 
1
    
}
    else if(
== false)
    {
        return 
0
    
}
    
    return -
1

either refactor your if else if branches where possible(as I did in HigherNumber).
__________________

Last edited by HamletEagle; 01-30-2019 at 12:59.
HamletEagle is offline
deprale
Senior Member
Join Date: Oct 2018
Location: Leeds
Old 01-30-2019 , 14:55   Re: Compare two variables in a return?
Reply With Quote #4

Quote:
Originally Posted by HamletEagle View Post
PHP Code:
stock HigherNumber()
{
if (
a>b) return a;
if (
b>a) return b;

Compiler sees that you are returning only on certain conditions and complains that in some cases your function could return nothing.
And here, that's the case. If a == b then what is your function going to return?

You could write it like this:
PHP Code:
stock HigherNumber(ab)
{
    if(
b
    {
        return 
a
    
}

    return 
b

if a > b fails then a <= b so in that case you should return b. There's no need for a second if check.

Or if you want to use the ternary operator(?):
PHP Code:
stock HigherNumber(ab)
{
    return (
b) ? b

In general it's a good idea to stay away from ?, as it is less readable and more error prone that regular if and else statements.

Note that even in this case:
PHP Code:
stock HigherNumber(bool:b)
{
    if(
== true)
    {
        return 
1
    
}
    else if(
== false)
    {
        return 
0
    
}

the compiler will still complain.
We know that a boolean is either true or false, so either b is true or b is false, the function will always return. But the compiler is dumb and doesn't know that, so it will still issue a warning.

To avoid the warning add either a dummy return at the end(which will never be reached) to make the compiler shut up:
PHP Code:
stock HigherNumber(bool:b)
{
    if(
== true)
    {
        return 
1
    
}
    else if(
== false)
    {
        return 
0
    
}
    
    return -
1

either refactor your if else if branches where possible(as I did in HigherNumber).
Yeah, the code in my actual plugin is written accordingly, just like how you wrote the if branches.

I was just trying to get my point across, what I'm trying to do is just understand my own stocks better and the ":" "?" stuff, as I had to return a value, but realised quickly that it's much easier to just do it the way CrazY said - with the max(a,b) which is much easier, then I'd have to just return one value.

Thanks @HamletEagle, @CrazY, for the tips! Now I understand how it works!
__________________

Last edited by deprale; 01-30-2019 at 14:56.
deprale is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-30-2019 , 15:03   Re: Compare two variables in a return?
Reply With Quote #5

Quote:
I was just trying to get my point across, what I'm trying to do is just understand my own stocks better and the ":" "?" stuff, as I had to return a value, but realised quickly that it's much easier to just do it the way CrazY said - with the max(a,b) which is much easier, then I'd have to just return one value.
You would still return only one value even with ? :, it's just another syntax and works like an if else. Nothing special.
__________________
HamletEagle is offline
deprale
Senior Member
Join Date: Oct 2018
Location: Leeds
Old 01-30-2019 , 17:01   Re: Compare two variables in a return?
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
You would still return only one value even with ? :, it's just another syntax and works like an if else. Nothing special.
Yeah, but less writing, more compact I guess, both work just fine and I understood how the return function works in stocks.

Thanks, HamletEagle, you're always very helpful!
__________________
deprale 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 04:28.


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