Computer Science Canada How to make buttons call a function? |
Author: | Decadence666 [ Tue Oct 24, 2006 5:56 pm ] | ||
Post subject: | How to make buttons call a function? | ||
Hey, im new here, and like alot of people, learning turing as a Gr. 10 course. I'm pretty good with php and new to turing, and im having some problems. I made a cash register program for an assignment, but now im making it with a GUI, and im running into some problems.
(yes i just modded the hello world program) This is what is supposed i want it to be: You press the buttons the numbers appear, then you press enter. The first sale amount is enter, etc etc, then it calculates the total cost + tax. The problem is, i can't get the numbers from the buttons to pass to a function, (when i try to call a function from inside the 'p_one' procedure, it says I can't do that) Sorry if i'm being unclear ( the program is still barley finished so it may be hard to tell what im trying to do) ... and thanks in advance for any help. |
Author: | Decadence666 [ Tue Oct 24, 2006 6:00 pm ] | ||
Post subject: | |||
appologies for double posting, but there is no edit button... heres a more clear version of the program! IGNORE THE FIRST SET OF CODE
|
Author: | Ultrahex [ Tue Oct 24, 2006 7:33 pm ] | ||
Post subject: | |||
Its Actually Rather Simple A Function Returns a Value For Example... put add(5,2) add(5,2) returns 7 so it outputs the number 7 A Procedure HOWEVER runs a set of actions without returning a value. so you can just do add(5,2) and in the procedure it does put a+b for example:
|
Author: | Cervantes [ Tue Oct 24, 2006 7:43 pm ] | ||||||||
Post subject: | |||||||||
Sorry, you can't do it. The reason is that the GUI widgets have associated with them an 'action procedure'. That is, a procedure that takes no parameters. That's what gets called when the widget is activated. The reason it has to be an action procedure is because Turing is statically typed. That is, we must declare what types of variables our subroutines take, and they have to take that type of value. The subroutine that creates widgets takes as one of its arguments an action procedure. Because Turing is statically typed, we're not allowed to give it a function, because that's a different type than is an action procedure. Even a procedure that takes one parameter is different from an action procedure. This wouldn't be too much of a problem if Turing supported anonymous functions/procedures. As it is, the best we can do is something like this:
With anonymous functions/procedures, we would be able to do something like this:
Where the lambda syntax creates an anonymous subroutine (or rather I wish it did -- lambda doesn't exist in Turing). For example,
or
The parameters to my lambda function/procedure are given inside pipes ( |...| ) and the types are given as usual. In conclusion, no, you can't give functions to GUI widgets. The best we can do is to define a bunch of action procs that call our functions or whatever you wanted with the appropriate arguments. This would be easier with anonymous functions/procedures, but alas Turing does not support this. If you're interested in anonymous functions/procedures, check out the Turing as a Functional Programming Language tutorial. |
Author: | Decadence666 [ Tue Oct 24, 2006 8:04 pm ] | ||
Post subject: | |||
Ok, thank you for your help I figured that you would say something like "Turing can't do that", so in the meantime I managed to work a different system out, heres the code for anybody who's interested.
|
Author: | ericfourfour [ Tue Oct 24, 2006 9:22 pm ] |
Post subject: | |
Quote: "Turing can't do that"
Well not necessarily. Turing can in fact do do it. It is Holt Soft's GUI library that cannot do it. If you really want a unique system make your own using OOP. I would recommend having a parent action class from which the programmer derives children classes and override a method that does the action wanted. This however, may seem like overkill for someone who just started programming class but you did say you are pretty good at PHP (I don't know anything about PHP) so you may be up for the challenge. |
Author: | Decadence666 [ Tue Oct 24, 2006 9:30 pm ] |
Post subject: | |
thats way to much for me, php was my first language, and i started about a year ago, i just got into and realized the power of OOP about a month.5 ago, but then i got grounded for a month and still am (cough) so havn't done much php in a while now i really don't plan to stick with turing, im going to learn it pretty good for this class, but we move on to Java next year so no need to obsess |
Author: | Clayton [ Wed Oct 25, 2006 7:43 am ] | ||
Post subject: | |||
This is why templates are so useful in C++. A template is basically a means to stop functions from being statically typed. EG.
Basically the above code will swap two pieces of data as long as they are the same type. This happens because whenever the program is compiled, there is a copy of the swap function for each type that can be passed to it (bool, int, double etc). Unfortunately turing doesn't support this, as it would need to be able to support function overloading (which it doesn't). All this really shows is that Turing is lacking in areas that it kind of needs not to be. In conclusion, if you wanted to be able to pass a function in GUI, the GUI syntax would have to include some sort of parameter that would allow GUI to not be statically typed. |
Author: | Decadence666 [ Wed Oct 25, 2006 6:54 pm ] |
Post subject: | |
ic, ok... Well thanks for the help |