Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Using Arrays In Parimeters
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: 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)
CodeMonkey2000




PostPosted: 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.
[Gandalf]




PostPosted: Fri Dec 29, 2006 5:03 pm   Post subject: (No subject)

Yes, sort of. It's basically a standard convention for random naming. Razz

http://en.wikipedia.org/wiki/Metasyntactic_variable
Hackmaster




PostPosted: 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
[Gandalf]




PostPosted: 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

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




PostPosted: 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:
code:

Multiply(10,2,3)

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).
Cervantes




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Mon Jan 01, 2007 3:01 pm   Post subject: (No subject)

you can however do such a thing in Ruby Very Happy

code:

def multiply(*integers)
    total = 1
    integers.each_do |element|
        total *= element
    end
end

puts multiply(3, 2, 1)


Very Happy
Cervantes




PostPosted: 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
BenLi




PostPosted: Tue Jan 02, 2007 10:35 am   Post subject: (No subject)

perhaps, foobar = fubar = f$cked up beyond all recognition
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: