comp.lang.ada
 help / color / mirror / Atom feed
* Passing arrays to procedures
@ 2004-12-22 14:34 xadian
  2004-12-22 15:09 ` Alex R. Mosteo
  2004-12-22 16:20 ` Martin Krischik
  0 siblings, 2 replies; 7+ messages in thread
From: xadian @ 2004-12-22 14:34 UTC (permalink / raw)


Hi,

I just wanted to pass an array to a procedure.
A procedure head like this:

procedure procname (n : in Integer; v1 : in array;) is ...

it causes the following error:

anonymous array definition not allowed here.
Same for array(Integer range 1..2) of Integer. The procedure is an extra 
file so there are no arraynames of the main procedure available for the 
parameter part.
Anyone knows why this error occurs and how to fix it?!

thx
Xadian



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Passing arrays to procedures
  2004-12-22 14:34 Passing arrays to procedures xadian
@ 2004-12-22 15:09 ` Alex R. Mosteo
  2004-12-22 18:43   ` xadian
  2004-12-22 16:20 ` Martin Krischik
  1 sibling, 1 reply; 7+ messages in thread
From: Alex R. Mosteo @ 2004-12-22 15:09 UTC (permalink / raw)


xadian wrote:
> Hi,
> 
> I just wanted to pass an array to a procedure.
> A procedure head like this:
> 
> procedure procname (n : in Integer; v1 : in array;) is ...
> 
> it causes the following error:
> 
> anonymous array definition not allowed here.
> Same for array(Integer range 1..2) of Integer. The procedure is an extra 
> file so there are no arraynames of the main procedure available for the 
> parameter part.
> Anyone knows why this error occurs and how to fix it?!

You must declare an array type and use it in the function declaration:

type Blah is array (Integer range <>) of Something;

procedure Procname (N : in Integer; v1 : in Blah) is...

of course you can declare the array type where it best fit your design.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Passing arrays to procedures
  2004-12-22 14:34 Passing arrays to procedures xadian
  2004-12-22 15:09 ` Alex R. Mosteo
@ 2004-12-22 16:20 ` Martin Krischik
  1 sibling, 0 replies; 7+ messages in thread
From: Martin Krischik @ 2004-12-22 16:20 UTC (permalink / raw)


xadian wrote:

> Hi,
> 
> I just wanted to pass an array to a procedure.
> A procedure head like this:
> 
> procedure procname (n : in Integer; v1 : in array;) is ...
> 
> it causes the following error:
> 
> anonymous array definition not allowed here.
> Same for array(Integer range 1..2) of Integer. The procedure is an extra
> file so there are no arraynames of the main procedure available for the
> parameter part.
> Anyone knows why this error occurs and how to fix it?!

You can only use named subtypes as procedure parameters:

Programming:Ada:Subtypes#named_subtype

You might also want to read:

Programming:Ada:Types:array

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Passing arrays to procedures
  2004-12-22 15:09 ` Alex R. Mosteo
@ 2004-12-22 18:43   ` xadian
  2004-12-22 19:34     ` tmoran
                       ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: xadian @ 2004-12-22 18:43 UTC (permalink / raw)


Alex R. Mosteo wrote:
> 
> 
> You must declare an array type and use it in the function declaration:
> 
> type Blah is array (Integer range <>) of Something;
> 
> procedure Procname (N : in Integer; v1 : in Blah) is...
> 
> of course you can declare the array type where it best fit your design.

Well actually I have no idea where to declare that type...
I've got two files. One with that procedure and one with the main 
procedure. In the main procedure I call that other one.
The declaration can't be in the main procedure because it wont be known 
in the procedure (thats what the compiler sais) and it can't be declared 
  in the seperate procedure-file, because there is no way to declare sth 
before the procedure starts.
I already tried to declare both proedures in one file but then it sais 
sth like "end of file expected" after the "end;" of the first of these 
two procedures...
So where now can I declare that type?!

xadian

PS The array i want to pass has a variable size, so there is no way to 
use a contraint one (if that is important).



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Passing arrays to procedures
  2004-12-22 18:43   ` xadian
@ 2004-12-22 19:34     ` tmoran
  2004-12-22 21:03     ` Martin Krischik
  2004-12-23  0:50     ` Jeffrey Carter
  2 siblings, 0 replies; 7+ messages in thread
From: tmoran @ 2004-12-22 19:34 UTC (permalink / raw)


>  in the seperate procedure-file, because there is no way to declare sth
>before the procedure starts.
  You need to put the array and procedure declarations together in a
public package, eg

    package Processing is

      type Blah is array (Integer range <>) of ...

      procedure Procname (N : in Integer; v1 : in Blah);

    end Processing;

and then the (private) body of the callee goes in the package body:

    package body Processing is
      procedure Procname (N : in Integer; v1 : in Blah) is
      ...
      end Procname;
    end Processing;

Then the caller will look something like
  with Processing;
  procedure Main is
    X : Processing.Blah(1 .. 10);
  begin
    Processing.Procname(17, X);
    -- or
    Processing.Procname(N=>17, v1=>X);

>I already tried to declare both proedures in one file but then it sais
>sth like "end of file expected" after the "end;" of the first of these
  The Ada language is not defined in terms of files, but rather in terms
of (separately compilable) "compilation units" - eg packages, library
level procedures, and some other stuff.  Some compilers (eg, GNAT) impose
a restriction that a single file can contain only a single compilation
unit.  I'm guessing you're using GNAT and that's why you get that
error message.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Passing arrays to procedures
  2004-12-22 18:43   ` xadian
  2004-12-22 19:34     ` tmoran
@ 2004-12-22 21:03     ` Martin Krischik
  2004-12-23  0:50     ` Jeffrey Carter
  2 siblings, 0 replies; 7+ messages in thread
From: Martin Krischik @ 2004-12-22 21:03 UTC (permalink / raw)


xadian wrote:

> Alex R. Mosteo wrote:
>> 
>> 
>> You must declare an array type and use it in the function declaration:
>> 
>> type Blah is array (Integer range <>) of Something;
>> 
>> procedure Procname (N : in Integer; v1 : in Blah) is...
>> 
>> of course you can declare the array type where it best fit your design.
> 
> Well actually I have no idea where to declare that type...
> I've got two files. One with that procedure and one with the main
> procedure. In the main procedure I call that other one.
> The declaration can't be in the main procedure because it wont be known
> in the procedure (thats what the compiler sais) and it can't be declared
>   in the seperate procedure-file, because there is no way to declare sth
> before the procedure starts.
> I already tried to declare both proedures in one file but then it sais
> sth like "end of file expected" after the "end;" of the first of these
> two procedures...
> So where now can I declare that type?!

You need a package. Sad that I have no wiki page for packages handy. But
basicly:

package Name
is
  type ....
end package;

will do

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Passing arrays to procedures
  2004-12-22 18:43   ` xadian
  2004-12-22 19:34     ` tmoran
  2004-12-22 21:03     ` Martin Krischik
@ 2004-12-23  0:50     ` Jeffrey Carter
  2 siblings, 0 replies; 7+ messages in thread
From: Jeffrey Carter @ 2004-12-23  0:50 UTC (permalink / raw)


Ada allows nested scopes:

procedure Outer is
    type Blah is array (Something range <>) of Something_Else;

    procedure Inner (X : in Blah) is
    ...
    end Blah;

    V : Blah := ...;
begin -- Outer
    Inner (X => V);
end Outer;

However, the primary form of encapsulation in Ada is the package.

-- 
Jeff Carter
"If I could find a sheriff who so offends the citizens of Rock
Ridge that his very appearance would drive them out of town ...
but where would I find such a man? Why am I asking you?"
Blazing Saddles
37



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-12-23  0:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-22 14:34 Passing arrays to procedures xadian
2004-12-22 15:09 ` Alex R. Mosteo
2004-12-22 18:43   ` xadian
2004-12-22 19:34     ` tmoran
2004-12-22 21:03     ` Martin Krischik
2004-12-23  0:50     ` Jeffrey Carter
2004-12-22 16:20 ` Martin Krischik

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