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

Username:   Password: 
 RegisterRegister   
 Is it possible to assign a procedure to a variable?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sam21292




PostPosted: Tue Jan 01, 2008 12:43 pm   Post subject: Is it possible to assign a procedure to a variable?

I'm doing a car race and I want the user to pick which car they want. In order to do this, I need a variable to assign the procedure of each car to. I just wanted to know if it was possible to assign the procedure to a variable. My program is in my section where I'm asking for help. Here is a link to it:
http://compsci.ca/v3/viewtopic.php?t=16810(its the one all the way at the bottom because I had earlier updates)
Sponsor
Sponsor
Sponsor
sponsor
Saad




PostPosted: Tue Jan 01, 2008 12:59 pm   Post subject: RE:Is it possible to assign a procedure to a variable?

Yes it is, however it is a really bad idea.
Why would you want to do it, you could just call the procedure?
Also, what your trying to achieve can be done through parameter passing.

Edit: Also, suggestion, stick to one topic for asking the questions Rolling Eyes
Tony




PostPosted: Tue Jan 01, 2008 5:54 pm   Post subject: RE:Is it possible to assign a procedure to a variable?

This is indeed possible. It's poorly documented, but Turing GUI module relies on this ability (where each button links to a procedure, through a variable).

Now, if you want to represent different cars by different procedures, that would be a bad idea.

A car is more of an object, and would ideally be represented by such. A record of car's paramers would be an acceptable substitute, if you are not familiar with OOP.

Though if you want your car to be able to call upon variable procedures (lets say powerups or special moves/effects), then it would make sense to organize procedures in an array of variables.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
sam21292




PostPosted: Tue Jan 01, 2008 7:12 pm   Post subject: RE:Is it possible to assign a procedure to a variable?

But what if I have two different types of cars for the user to choose, lets say an SUV and a sports car (which are the selections that I have). In order for the user to choose either one of these, should I use boolean? If not, then what is the easiest way to do so?
Tony




PostPosted: Tue Jan 01, 2008 10:14 pm   Post subject: RE:Is it possible to assign a procedure to a variable?

what you should have is a concept of an abstract car. The user has "a car", we don't yet know which one it is, but it has everything that any car would have -- acceleration, top speed, etc.

The differences between SUV and a sport car are just those parameters.

The point is, a car accelerates just the same weither it's an SUV, a sport car, or a tank. The only difference is the value of the acceleration. It would be silly to write the same procedure for every type of a car, and just one value.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
sam21292




PostPosted: Wed Jan 02, 2008 7:04 pm   Post subject: RE:Is it possible to assign a procedure to a variable?

i see...
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  [ 6 Posts ]
Jump to:   


Style:  
Search: