Reputation: 59
I cant seem to find any information on how to do this. Does anyone know how I could pass an array to a procedure just like you would a character or integer?
And example would be amazing.
Upvotes: 1
Views: 6931
Reputation: 31689
As Jeffrey stated, you need a type name. There are some places in the Ada syntax where you need a simple type name with no embellishments, and other places where you can have anonymous array types or names with other constraints attached to them. Parameter declarations are one place where the type must be a simple name (except that for access types you can add not null
, which you may not have learned about yet, but that's the only exception). Thus, you can't say
procedure joiningTo(A: in integer; B: array(1..12, 1..12) of character) is
or
procedure Print_Field(Data : String(1..30)) is
since the name must be a simple type name in this context. There are a couple solutions, and you need to choose which one is more appropriate. If it makes sense that joiningTo
could work correctly with array of any length and width, define an unconstrained array type somewhere in your program:
type Two_Dimensional_Char_Array is array (positive range <>, positive range <>) of character;
procedure joiningTo(A : in integer; B : in Two_Dimensional_Char_Array) is
If, on the other hand, it's a requirement that the parameter be exactly 12-by-12, you can define a type or subtype name that includes the constraint:
type Twelve_Square is array (1..12, 1..12) of character;
procedure joiningTo(A : in integer; B : in Twelve_Square) is
or
type Two_Dimensional_Char_Array is array (positive range <>, positive range <>) of character;
subtype Twleve_Square is Two_Dimensional_Char_Array (1..12, 1..12);
procedure joiningTo(A : in integer; B : in Twelve_Square) is
(Side note: The standard naming convention in Ada, since Ada 95, is to upper-case the first letter of each word [usually] and separate words with _
characters; this is different from the Java or C communities where _
characters are frowned upon. Also, it's important to note that, unlike Java or C, Ada does not care about letter case in identifiers.)
Upvotes: 3
Reputation: 3358
First, you have to have a named array type. An anonymous type won't work (anonymous types are a bad idea in any case).
As an example, there is an array type named String declared in package Standard as:
type String is array (Positive range <>) of Character;
String is no different from any other array type. You pass a String (or any other array type) to a subprogram exactly the way you do any other type:
function Index (Source : in String; Pattern : in String) return Natural;
procedure To_Upper (Source : in out String);
Upvotes: 6