15

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)

Ken White
  • 123,280
  • 14
  • 225
  • 444
Clueless
  • 605
  • 3
  • 10
  • 19
  • 1
    Just FYI: The `FunctionName := SomeVal;` syntax is antiquated; the current syntax is `Result := SomeVal;`, where `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 Mar 09 '12 at 22:49
  • 4
    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 – Marco van de Voort Mar 11 '12 at 19:48

4 Answers4

15

You can use the Exit procedure.

function Foo (Value : integer) : Integer;
begin      
  Exit(Value*2);
  DoSomethingElse();   // This will never execute
end; 
Ken White
  • 123,280
  • 14
  • 225
  • 444
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 1
    +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 Mar 09 '12 at 22:15
  • 1
    +1 this is a nice syntax, helps to avoid the dreaded Result + Exit combo. Basically the equivalent of "return". – Thomas Mar 13 '12 at 11:03
1

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; 
saybooboo
  • 19
  • 1
  • Structured programming. Several returns can be acceptable sometimes, even throwing exceptions, but a goto should only be used when any alternative is not acceptable, as it makes the code very difficult to read and maintain. In this case, adding an `else` statement solve the case. – Adrian Maire Feb 02 '23 at 16:07
  • @AdrianMaire Using an else statement increases the cyclomatic complexicty of the function. Arguably, in the absence of `Exit`, a return label incurs less cognitive load on the reader than nesting, especially if multiple places in the function need an early exit. – M.Stramm Mar 13 '23 at 08:24
  • We write code for humans, not for computers: with the exception of efficiency-critical code, code should be implemented for readability and maintainability. The fact that most developers banned the goto (and for good reasons), makes this type of code unusual, and consequently error-prone. It's IMHO not true that the cyclomatic complexity is increased in this case: you have 2 jumps + the return in both cases. Additionally, I am not sure that cyclomatic simplicity is a goal, it's neither an indicative of CPU usage, neither a indicative of code readability. – Adrian Maire Mar 13 '23 at 08:55
0

I think you can use either the function name itself, "result", or 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;

Hope that helps ^^

coder
  • 8,346
  • 16
  • 39
  • 53
Khang
  • 141
  • 1
  • 11
0

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; 
Victor Melo
  • 188
  • 1
  • 13