Author |
Message |
Prince Pwn
|
Posted: Fri Dec 29, 2006 4:31 pm Post subject: Using Arrays In Parimeters |
|
|
I'm kind of unsure how arrays in the parameters work, here's my code:
code: |
function MultiplyFcn (number : array 1 .. 2 of int) : int % Function
result number (1) * number (2)
end MultiplyFcn
procedure MultiplyProc (number : array 1 .. 2 of int) % Procedure
put number (1) * number (2)
end MultiplyProc
put MultiplyFcn (2, 3) % Puts the result of MultiplyFcn on the screen
MultiplyProc (7, 2) % Does what's inside MultiplyProc
|
Everything is ok except for when I try to call the proc's/fcn's. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Fri Dec 29, 2006 4:41 pm Post subject: (No subject) |
|
|
code: | var foo : array 1 .. 2 of int := init (1, 5)
put MultiplyFcn (foo)
MultiplyProc (foo) |
|
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Fri Dec 29, 2006 4:53 pm Post subject: (No subject) |
|
|
Why do you use the variable "Foo"? is that a standard convention or something? bcause I saw many people use it in the tutorials. |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
|
|
|
![](images/spacer.gif) |
Hackmaster
|
Posted: Fri Dec 29, 2006 7:47 pm Post subject: (No subject) |
|
|
foo is the first standard "I need a name for something" name. the second is bar. I think foobar is actually a word for something in another language... I dunno... wikipedia is you friend here. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Fri Dec 29, 2006 9:16 pm Post subject: (No subject) |
|
|
Hackmaster wrote: foo is the first standard "I need a name for something" name. the second is bar. I think foobar is actually a word for something in another language... I dunno... wikipedia is you friend here. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif)
Uhm... Did you even read my post? It's generally a good idea to read all the posts in a topic before replying. |
|
|
|
|
![](images/spacer.gif) |
Prince Pwn
|
Posted: Sun Dec 31, 2006 10:41 pm Post subject: (No subject) |
|
|
Quote: Uhm... Did you even read my post? It's generally a good idea to read all the posts in a topic before replying.
hehe i do that sometimes, yeah we should learn to read all pages of a thread before we post.
What I'm basically trying to do with this is actually make a procedure or function that will allow you to multiply as many numbers as you like just by calling:
or
code: |
Multiply(30,10,22,56,66,3)
|
So it wont matter how long my parameter is, it will still multiply all the values together without any errors. To do that I would think I'd have to have an array. Possibly a flexible array (which I am not 100% good with, but could learn). |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Sun Dec 31, 2006 10:45 pm Post subject: (No subject) |
|
|
You can let your procedure take an array of unknown upper bound, but that's as good as it gets. There's no way to do that, I'm afraid. Just like there's no way to do default parameters. And there's no way to create an array without going through the trouble of creating a variable. Sorry. ![Sad Sad](http://compsci.ca/v3/images/smiles/icon_sad.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Mon Jan 01, 2007 3:48 pm Post subject: (No subject) |
|
|
Better would be to use inject. It then becomes a one line function, and you aren't relying on that little bit of magic to get your value returned.
Ruby: |
def multiply(*nums)
nums.inject(1) {|a, b| a*b}
end
|
|
|
|
|
|
![](images/spacer.gif) |
BenLi
![](http://compsci.ca/v3/uploads/user_avatars/17704181124608ffcaeb421.gif)
|
Posted: Tue Jan 02, 2007 10:35 am Post subject: (No subject) |
|
|
perhaps, foobar = fubar = f$cked up beyond all recognition |
|
|
|
|
![](images/spacer.gif) |
|