2

For example in c you could say have a condition and if that condition is not met you can return(0); and this would terminate the program.

Is there a way I could do this in Ada?

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
  • 1
    In C, `return 0;` (the parentheses aren't necessary) terminates the program only if it's in the `main` function. Otherwise it merely returns from the current function to its caller -- just like the `return` statement in Ada. C's `exit()` function terminates the program (with some caveats if you've registered something via `atexit()`). You're asking whether Ada has something equivalent to C's `exit()` function. – Keith Thompson Feb 20 '15 at 21:48
  • 3
    possible duplicate of [Ada 83(Only) How to end an entire program which has multiple procedures](http://stackoverflow.com/questions/23176305/ada-83only-how-to-end-an-entire-program-which-has-multiple-procedures) – Simon Wright Feb 20 '15 at 21:53
  • @KeithThompson yea, sorry thats what I meant, i wanted to know what the exit() equivilant in ADA. –  Feb 20 '15 at 22:57
  • Just to be picky, it's "Ada", not "ADA". (It's not an acronym, it's named after a person.) – Keith Thompson Feb 20 '15 at 23:03
  • 1
    Also a duplicate of http://stackoverflow.com/questions/28487175/how-to-stop-execution-in-my-program – ajb Feb 21 '15 at 04:55
  • As with the duplicate question, one way is simply to raise an exception without a handler, `raise Program_Error;` will do. –  Feb 22 '15 at 10:25

2 Answers2

0

In C you should call exit() function;

But investigating your work, i found some information about.

if some_condition then Abort_Task (Current_Task); end if;

Ada Process Termination Code

CCebrian
  • 75
  • 8
  • I did the following Abort_Task(the name of my main procedure); and got the following error "expected private type "Ada.Task_Identification.Task_Id" and "found procedure name instead of function" –  Feb 20 '15 at 22:55
  • @Newbie: The you need to obtain a `Task_Id` value corresponding to the current task (which is not the same as the currently executing procedure). – Keith Thompson Feb 20 '15 at 23:03
  • Wait so you are saying I need to obatin a task id for my main procedure(program) and this will terminate it? Also do you know if there is an easy function or something to obtain that task id? –  Feb 20 '15 at 23:06
  • Ada's tasks are objects, so you need to pass a self-task(object) to terminate the process (?)... im not sure – CCebrian Feb 20 '15 at 23:19
  • Im not too familiar with Ada i just started learning it. I just want to know if there is anyway i can exit the program to terminal with a certain command. For example in C you can use exit(). –  Feb 20 '15 at 23:34
  • @Newbie If you are in the main procedure (the one that runs first when you start the program, after package initializations), just saying `return;` will terminate the program. Please also see the duplicate questions in Simon's and my comments. I would avoid using `Abort_Task` to accomplish this. It's usually the wrong approach. – ajb Feb 21 '15 at 04:58
0

if you can use GNAT, try GNAT.OS_Lib.OS_Exit(1); this terminates the program with the given exit code.

However, if you want some finalisation try GNAT.OS_Lib.OS_Abort;

Jules
  • 11
  • 2