Procedures and Parameters
Author |
Message |
Mr. T
|
Posted: Sun Jun 04, 2006 1:34 pm Post subject: Procedures and Parameters |
|
|
How would one go about passing the elements of an array through as parameters of a procedure? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheOneTrueGod
|
Posted: Sun Jun 04, 2006 1:45 pm Post subject: (No subject) |
|
|
There are two things you could be asking here, so i'll try and address them both.
Method 1: Passing arrays through parameters
code: |
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
code: |
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
|
Posted: Sun Jun 04, 2006 1:51 pm Post subject: Alex's Opinion |
|
|
TheOneTrueGod wrote:
Method 1: Passing arrays through parameters
code: |
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
|
Posted: Sun Jun 04, 2006 2:01 pm Post subject: (No subject) |
|
|
lol, oops.
Add a:
To the end of the code |
|
|
|
|
|
Mr. T
|
Posted: Sun Jun 04, 2006 2:03 pm Post subject: Alex's Opinion |
|
|
Would this work the same for flexible arrays? ie. a flexbile parameter |
|
|
|
|
|
TheOneTrueGod
|
Posted: Sun Jun 04, 2006 2:08 pm Post subject: (No subject) |
|
|
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 .
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:
code: | 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
|
Posted: Sun Jun 04, 2006 2:15 pm Post subject: Alex's Opinion |
|
|
Blech, it seems to be getting needlessly complicated, all for the sake of proceduralization. |
|
|
|
|
|
TheOneTrueGod
|
Posted: Sun Jun 04, 2006 2:19 pm Post subject: (No subject) |
|
|
lol, as was said by wtd, you shouldn't be using procedures anyways, you should be using functions
Yah, things start to get really complicated, but if you try learning classes, you'll realise EVERYTHING becomes placed in procedures
I've allways found it easier to "proceduralize" code when I plan to do it that way from the start. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Sun Jun 04, 2006 2:21 pm Post subject: (No subject) |
|
|
TheOneTrueGod wrote: 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 turning Turing into Ruby to see an example. However, you'll notice I used a huge honking static array rather than a flexibe array. The reason being, you can't export a flexible array. But there's an even better way (assuming this works...) that I should replace that post with.
code: |
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
|
Mr. T wrote: Blech, it seems to be getting needlessly complicated, all for the sake of proceduralization.
Hold on to your pants. Nothing's gotten complicated yet. |
|
|
|
|
|
|
|