Help with Return operator..
#1

Hi guys. I need your help. I can't understand operator "RETURN", i gave on public if(IsPlayerNPC(i)) return 1; compiled and got warning 209: function "SetPlayerUnjail" should return a value, well i read wiki but i still don't understand what is value i need make for my return...
1 or 0? Or something else? Help me please.

Reply
#2

return 1;

Reply
#3

well.. i understood it like this:

return 1 means the script will go on.
return 0 means the script checks the callback again.

You can return variables, too, to make their change visible at the global variables.

Callbacks should return a value.

I guess you just have another kind of problem, looks like SetPlayerUnjail gives a value, but it cannot be taken.

It's just a suggestion, so pls correct me if i'm wrong.

Cheers.
Reply
#4

the function is probally something like this:
pawn Код:
stock myFunction(stuff)
{
  if(something == something)
    return 1;
}
So you can make it return false (return 0 if you want it to "cancel" the function if the statement isn't true:


pawn Код:
stock myFunction(stuff)
{
  if(something == something)
    return 1;
  return 0;
}
Reply
#5

Pretty good already lrZ^ aka LarzI but not detailed enough:

The return operator, if you can even call it operator, is as the english word says, returning a variable. We all know that there are different variables, such as:

(I am using pawn ones)
pawn Код:
The string: "Hello, my name is Extremo"
pawn Код:
The Integer: 1337
pawn Код:
The float: 1337.000000
That are the most known datatypes we use. So, to understand how return works, you first have to understand what a Procedure and a Subroutine is.

There are two different functions so people call it on this forum, which indicate something. A procedure for example does something, without leaving anything to the program, nor the programmers hand. So for example:

pawn Код:
Function()
{
  print("Hello");
}
No matter what happens, Function will print "Hello", even if the printing fails. This is a procedure.

A subroutine is anything which returns a variable. So, as already mentioned above, variables are strings, integers, floats, doubles and there are many many more.
To go into detail, this would be a subroutine:

pawn Код:
Function()
{
  if(print("Hello"))
  {
    return true;
  }
  return false;
}
Alright. Now, lets have a closer look at it. I am expecting that print will return true(as it seems to be a boolean) if the message could be printed onto the console and false if not. So, true and false are equal to 1 and 0, so many say here on the forums. I dare to disagree with all of them. The compiler is intelligent enough to take most of the thinking away from you and just converts 1 and 0 to true and false, if that are the only variables the function returns. While compiling, your compiler checks your functions and gives them types, even though pawn has always been called a typeless language, which it is, internly it still remains a language with types. Let me give you a example:

pawn Код:
Function()
{
  if(//something)
  {
    return 1;
  }
  return 0;
}
As only 0 and 1 are used, this will compile as a boolean function. returning true and false.

pawn Код:
Function()
{
  // do something
}
As this does not return anything, it will compile as a procedure, returning nothing.

pawn Код:
Function()
{
  if(//something)
  {
    return 0;
  }
  else if(//something)
  {
    return 1;
  }
  else
  {
    return 2;
  }
}
This will compile as a integer function. It returns a integer.

So, return always SENDS a message back to the caller, depending on what you return.
So, lets say if you call a function with "if", it will send the result to if and if will evaluate your result into true or false.
e.g: if(IsPlayerConnected(playerid)) - if the player is connected, IsPlayerConnected will send back the value 1(I think it is 1) if not, it will send the value 0. So now the IF just basically checks if the value the IsPlayerConnected subroutine sends back is a value above 0 or not. If not, it will not do the grouped bit of code inside the if, if it is above 0, then it will do the grouped bit of code inside the if. Thats the basic explanation of return.

EDIT: Your error just occured, because with the return 1, you turned your function into a subroutine and a subroutine HAS to return a value at all times, no matter under what condition.
Reply
#6

but watch out @ onplayertext returns works different, return 1; = send text, return 0; = dont send text.
Reply
#7

Thanks to ALL! Big Thanks you Extremo now i understand!
Reply
#8

Quote:
Originally Posted by Toney
Thanks to ALL! Big Thanks you Extremo now i understand!
Lol, so everytime a { is opened, it must be closed (}). For example if you got a total of 40 {, you should have 40 } too. Otherwise, you will get 26 errors (Because 26 is the max of errors that PAWNO can return);
Reply
#9

Even if he might know that already, thanks for sharing it, but it's kinda off topic, since it has nothing to do with return values..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)