Question: In Section 9.3.1 we noted that Ada 83 does not permit subroutines to be passed as parameters, but that some of the same effect can

In Section 9.3.1 we noted that Ada 83 does not permit subroutines to be passed as parameters, but that some of the same effect can be achieved with generics. Suppose we want to apply a function to everymember of an array.
We might write the following in Ada 83:

generic type item is private; type item_array is array (integer range ) of item; with function F(it : in item) return item; procedure apply_to_array (A in out item array); procedure apply_to_array (A : in out item_array) is begin for i in A'first..A'last loop A(i) := F(A(i)); end loop; end apply_to_array;

Given an array of integers, scores, and a function on integers, foo, we can write:

generic type item is private; type item_array is array (integer range )

How general is this mechanism? What are its limitations? Is it a reasonable substitute for formal (i.e., second-class, as opposed to third class) subroutines?

generic type item is private; type item_array is array (integer range ) of item; with function F(it : in item) return item; procedure apply_to_array (A in out item array); procedure apply_to_array (A : in out item_array) is begin for i in A'first..A'last loop A(i) := F(A(i)); end loop; end apply_to_array;

Step by Step Solution

3.38 Rating (173 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The limitation of this programming idiom is that the generic subroutine ap... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!