comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <OneWingedShark@gmail.com>
Subject: Re: calling function inside procedure call
Date: Fri, 03 Oct 2014 20:33:47 -0700
Date: 2014-10-03T20:33:47-07:00	[thread overview]
Message-ID: <l_IXv.259065$Ub6.133556@fx20.iad> (raw)
In-Reply-To: <e3869686-01e9-4eaa-a2fc-83b55f708bf9@googlegroups.com>

On 10/3/2014 6:07 PM, Stribor40 wrote:
> On Friday, 3 October 2014 16:42:43 UTC-4, Marius Amado-Alves  wrote:
>> Did you know you can declare local procedures as you do functions?
>
> no.....how would that be done and what would be the benefits?

Well consider something kinda complex, say parsing:
  You have a function Parse which takes a string and parses it into a 
type containing several components:

   subtype String_4 is String(1..4);

   Type Some_Record is record
    A, B : Integer;
    C    : Boolean;
    D    : String_4;
   end record;

  -- ...
  Parse_Error : Exception;
  Function Parse( Input : String ) return Some_Record;

  -- in the body...

  Function Parse( Input : String ) return Some_Record is
     Working	: Ada.Strings.Unbounded.Unbounded_String:=
                   Ada.Strings.Unbounded.To_Unbounded_String(Input);

     -- All Get_Segment functions consume the front-end of Working
     Function Get_Segment return Integer;
     Function Get_Segment return Boolean;
     Function Get_Segment return String_4;

     -- IMPLEMENTATIONS OF GET_SEGMENT GO HERE.

  begin
    return Some_Record'(
         A => Get_Segment,
         B => Get_Segment,
         C => Get_Segment,
         D => Get_Segment
       );
  end Parse;

As you can see, this turns the actual body portion of the parsing into 
something quite small: a single statement. (I could have put it all on 
one line.) This method allows you to compose the parse function of 
smaller functions, functions which are not visible [or accessible] 
outside of the scope of the function. This means you can contain things, 
and you will be able to change them freely as you are guaranteed that 
these nested functions will not be used elsewhere.



> i am still having troubles figuring out when to use function and when to use procedure

Simple rule: does it return a value? If so it's a function, if not 
procedure. (Yes, the IN OUT and OUT modes on procedures muddy things a 
little, as does allowing functions to have OUT and IN OUT modes; don't 
worry about these too much.)

> As of right now on my baby steps in ada i base it on if i need to return something or not.

That's generally the correct approach.
There are some subtleties, like constrained and unconstrained [sub]types 
that change things: if you're returning an unconstrained type it /must/ 
be a function. -- But that can, and should, wait until you get 
comfortable with constrained/unconstrained.

  reply	other threads:[~2014-10-04  3:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-03 19:50 calling function inside procedure call Stribor40
2014-10-03 19:56 ` Adam Beneschan
2014-10-03 19:57   ` Stribor40
2014-10-03 20:42 ` Marius Amado-Alves
2014-10-04  1:07   ` Stribor40
2014-10-04  3:33     ` Shark8 [this message]
2014-10-04  4:31       ` Jeffrey Carter
2014-10-04  6:12         ` Shark8
2014-10-04  1:08   ` Stribor40
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox