Devolver un valor en Pascal

For a function to return a value in Pascal the assignment FunctionName := SomeVal; is used. I assume it doesn't stop the function execution in that exact place as return in C does. Is there something similar to C return in Pascal? (I'm using FreePascal compiler)

preguntado el 09 de marzo de 12 a las 22:03

Just FYI: The FunctionName := SomeVal; syntax is antiquated; the current syntax is Result := SomeVal;, Donde Result is an automatically available variable of the proper data type to match the function declaration. For instance: function Test: Integer; begin Result := 10; end;. -

Ken White: In Free Pascal, result is afaik only supported in Delphi and delphi-like dialects. (-S2/-Sd). Other pascal's haven't adopted the Delphi centric result either, or only for compatibility modes. The last Pascal standard still documents this -

4 Respuestas

You can use the Exit procedure.

function Foo (Value : integer) : Integer;
begin      
  Exit(Value*2);
  DoSomethingElse();   // This will never execute
end; 

respondido 09 mar '12, 22:03

+1, never heard about this syntax! However might be good to put a line after the Exit with some comment that the line after Exit won't be performed. - TLama

+1 this is a nice syntax, helps to avoid the dreaded Result + Exit combo. Basically the equivalent of "return". - Thomas

In canonical pascal (without keyword Exit) you can emulate return via goto:

   function Foo (Value : integer) : boolean;
   label return;
   begin      
      if Value < 0 then
      begin
          Foo := false;
          goto return;
      end;
      Calc();
      Foo := true;
   return:
   end; 

respondido 11 mar '21, 07:03

I think you can use either the function name itself, "result" o el Exit(), but I've only used the result identifier, so don't know if the others will work for you:

function Foo(a, b: Integer): Integer;
begin
    result := a + b;
end;

Espero que ayude ^^

contestado el 02 de mayo de 18 a las 08:05

The return code of the end of every program is stored in the EAX register. So you can use Assembly inline on Pascal to return wherever you want to end the program running using!

 asm
 mov eax,%ERROLEVEL%
 end; 

Respondido el 24 de diciembre de 18 a las 02:12

That's what you compiler will do after the compilation! Followed by a RET - Victor Melo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.