
-----------------------------------
Mr. T
Sun Jun 04, 2006 1:34 pm

Procedures and Parameters
-----------------------------------
How would one go about passing the elements of an array through as parameters of a procedure?

-----------------------------------
TheOneTrueGod
Sun Jun 04, 2006 1:45 pm


-----------------------------------
There are two things you could be asking here, so i'll try and address them both.

Method 1:  Passing arrays through parameters

procedure OutputArray (ar : array 1..* of int) %Either use *, or have the number EXACTLY the same as your array.
    for i : 1..upper(ar)
      put ar(i)
   end for
end OutputArray
var q : array 1..10 of int
for i : 1..10
    q(i) := Rand.Int(1,10)
end for


Method 1: Passing elements into a procedure


procedure OutputSum(num1,num2 : int)
  put num1 + num2
end OutputSum
var q : array 1..10 of int
for i : 1..10
    q(i) := Rand.Int(1,10)
   if i > 2 then
     OutputSum(q(i),q(i-1))
   end if
end for


The code should be pretty self explanatory...[/code]

-----------------------------------
Mr. T
Sun Jun 04, 2006 1:51 pm

Alex's Opinion
-----------------------------------

Method 1:  Passing arrays through parameters

procedure OutputArray (ar : array 1..* of int) %Either use *, or have the number EXACTLY the same as your array.
    for i : 1..upper(ar)
      put ar(i)
   end for
end OutputArray
var q : array 1..10 of int
for i : 1..10
    q(i) := Rand.Int(1,10)
end for


In this example the procedure doesn't play a role in the second half of the code.  :?

-----------------------------------
TheOneTrueGod
Sun Jun 04, 2006 2:01 pm


-----------------------------------
lol, oops.

Add a:

OutputArray(q)


To the end of the code :P

-----------------------------------
Mr. T
Sun Jun 04, 2006 2:03 pm

Alex's Opinion
-----------------------------------
Would this work the same for flexible arrays? ie. a flexbile parameter  :?

-----------------------------------
TheOneTrueGod
Sun Jun 04, 2006 2:08 pm


-----------------------------------
As far as I know, and have tried to test, there is no way to have flexible parameters.  I've had to use global flexible arays in order to modify the upper bounds of them.  I've actually been meaning to ask about that, but never got around to it :P.

If you want to just pass a flexible array, you need to use the (lowerbound..*) approach, and inside of the procedure, use upper(arrayName), but then inside of the procedure, you cannot modify the upper bounds of the array.

sample code is same as above, but with a different bottom part:

var q : flexible array 1..10 of int 
for i : 1..10 
    q(i) := Rand.Int(1,10) 
end for
new q, 5 %For simplicities sake, i'm just gonna spontaneously shorten it here.
OutputArray(q)

Because I coded the procedure dynamically (I.E. not specific -- It can accept any size array, as long as that array's lower bound is 1)  I didn't have to change anything to it, and it can be called simply as is.

-----------------------------------
Mr. T
Sun Jun 04, 2006 2:15 pm

Alex's Opinion
-----------------------------------
Blech, it seems to be getting needlessly complicated, all for the sake of proceduralization.

-----------------------------------
TheOneTrueGod
Sun Jun 04, 2006 2:19 pm


-----------------------------------
lol, as was said by wtd, you shouldn't be using procedures anyways, you should be using functions :P

Yah, things start to get really complicated, but if you try learning classes, you'll realise EVERYTHING becomes placed in procedures :D

I've allways found it easier to "proceduralize" code when I plan to do it that way from the start.

-----------------------------------
Cervantes
Sun Jun 04, 2006 2:21 pm


-----------------------------------
As far as I know, and have tried to test, there is no way to have flexible parameters.  I've had to use global flexible arays in order to modify the upper bounds of them.

Another way to do it is to wrap the flexible array into an object. See 
class IntArray
    export at
    var this : flexible array 1 .. 0 of int
    
    function at (index : int) : int
        result this (index)
    end at
    
    % Define other methods: 
    #push, pop, shift, unshift, each, each_with_index, delete_if, map ...
end IntArray


Blech, it seems to be getting needlessly complicated, all for the sake of proceduralization.
Hold on to your pants. Nothing's gotten complicated yet. :)
