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)
Asked
Active
Viewed 2.7k times
15
-
1Just 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
-
4Ken 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 Answers
15
You can use the Exit procedure.
function Foo (Value : integer) : Integer;
begin
Exit(Value*2);
DoSomethingElse(); // This will never execute
end;
-
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
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
-
That's what you compiler will do after the compilation! Followed by a RET – Victor Melo Dec 24 '18 at 02:55
-
Using inline-assembly is not cross-platform compatible and using X86 assembly won't help you if you target the jvm or ARM devices. – M.Stramm Mar 13 '23 at 08:22