
-----------------------------------
Prince Pwn
Fri Dec 29, 2006 4:31 pm

Using Arrays In Parimeters
-----------------------------------
I'm kind of unsure how arrays in the parameters work, here's my 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.

-----------------------------------
[Gandalf]
Fri Dec 29, 2006 4:41 pm


-----------------------------------
var foo : array 1 .. 2 of int := init (1, 5)
put MultiplyFcn (foo)
MultiplyProc (foo)

-----------------------------------
CodeMonkey2000
Fri Dec 29, 2006 4:53 pm


-----------------------------------
Why do you use the variable "Foo"? is that a standard convention or something? bcause I saw many people use it in the tutorials.

-----------------------------------
[Gandalf]
Fri Dec 29, 2006 5:03 pm


-----------------------------------
Yes, sort of.  It's basically a standard convention for random naming.  :P 

http://en.wikipedia.org/wiki/Metasyntactic_variable

-----------------------------------
Hackmaster
Fri Dec 29, 2006 7:47 pm


-----------------------------------
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. :)

-----------------------------------
[Gandalf]
Fri Dec 29, 2006 9:16 pm


-----------------------------------
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. :)
Uhm... Did you even read my post?  It's generally a good idea to read all the posts in a topic before replying.

-----------------------------------
Prince Pwn
Sun Dec 31, 2006 10:41 pm


-----------------------------------
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:

Multiply(10,2,3)

or

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).

-----------------------------------
Cervantes
Sun Dec 31, 2006 10:45 pm


-----------------------------------
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. :(

-----------------------------------
Clayton
Mon Jan 01, 2007 3:01 pm


-----------------------------------
you can however do such a thing in Ruby :D


def multiply(*integers)
    total = 1
    integers.each_do |element|
        total *= element
    end
end

puts multiply(3, 2, 1)


:D

-----------------------------------
Cervantes
Mon Jan 01, 2007 3:48 pm


-----------------------------------
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.

def multiply(*nums)
  nums.inject(1) {|a, b| a*b}
end


-----------------------------------
BenLi
Tue Jan 02, 2007 10:35 am


-----------------------------------
perhaps, foobar = fubar = f$cked up beyond all recognition
