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

Username:   Password: 
 RegisterRegister   
 JUST now learning functions
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Paul




PostPosted: Tue Nov 30, 2004 9:18 pm   Post subject: JUST now learning functions

Hehe... I'm just now using functions, having not looked that way b4, I'm a little embarrassed. But I had to make something with functions, am I doing it properly?

(we were suppose to only use 2 global variables maximum)
code:

const grav := 9.8
var speed, angle : real
put "Enter Speed: " ..
get speed
put "Enter Angle: " ..
get angle

function Vhor (speed, angle : real) : real
    result speed * (cosd (angle)) %Vhor = speed*(cosd(angle))
end Vhor

function Vvert (speed, angle : real) : real
    result speed * (sind (angle)) %Vvert = speed*(sind(angle))
end Vvert

function TimeMaxHeight (yspeed : real) : real
    result yspeed / grav %Time to max height = Vvert/gravity
end TimeMaxHeight

function FlightTime (tim : real) : real
    result tim * 2 %Flight time = TimeMaxHeight * 2
end FlightTime

function Range (xspeed, flight : real) : real
    result xspeed * flight %Range = Vhor * flight time
end Range

function MaxHeight (yspeed, MaxHTime : real) : real
    result yspeed * MaxHTime - 0.5 * grav * MaxHTime ** 2 %MaxHeight = Vvert*Tmaxheight - 0.5(grav)(maxHTime**2)
end MaxHeight

put "The Time of Flight of the projectile is: " ..
put FlightTime (TimeMaxHeight (Vvert (speed, angle))) : 0 : 2, " seconds"
put "The range of the projectile is: " ..
put Range (Vhor (speed, angle), TimeMaxHeight (Vvert (speed, angle))) : 0 : 2, " metres"
put "The Maximum height of the projectile is: " ..
put MaxHeight (Vvert (speed, angle), TimeMaxHeight (Vvert (speed, angle))) : 0 : 2, " Metres"
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 30, 2004 9:36 pm   Post subject: (No subject)

Yes and no. You're trying to save typing time by using things like "Vvert", but you're just costing yourself far more time down the road figuring out what the heck you meant. Smile

Try "verticalVelocity" and "horizontalVelocity".
Paul




PostPosted: Tue Nov 30, 2004 10:50 pm   Post subject: (No subject)

Those were just fromt he formulas, everything else ok?
Delos




PostPosted: Tue Nov 30, 2004 10:55 pm   Post subject: (No subject)

They look like they serve their purpose. Here's a little something that people often overlook about functions:

code:

function world : string
result "hello world"
end world

put world


Functions don't need parameters - kinda obvious, but as I said often overlooked.
And why is this useful? Well, it's circumstantial to be sure. I found it particularly useful when dealing with arrays of custom types, where multiple constant assignments need to be made.
wtd




PostPosted: Wed Dec 01, 2004 1:09 am   Post subject: (No subject)

Delos wrote:
They look like they serve their purpose. Here's a little something that people often overlook about functions:

code:

function world : string
result "hello world"
end world

put world


Functions don't need parameters - kinda obvious, but as I said often overlooked.
And why is this useful? Well, it's circumstantial to be sure. I found it particularly useful when dealing with arrays of custom types, where multiple constant assignments need to be made.


Turing does provide for constants.

code:
const world := "hello world"


Less code and it says what you meant when you wrote the function above.
Delos




PostPosted: Wed Dec 01, 2004 12:14 pm   Post subject: (No subject)

What! Constants in Turing! OMG! That's so freakin' awsome!

Wink

Come on wtd, you know as well as I, actually a lot better than I, how useful fcns can be. And yes I admit the above example was plain and could have been done using the whole constants approach, but the concept is what I was after.
Think about it, you have a record-type, with say 20 different fields in it. And then you make a nice array of those, and you want to set those up, all to the same value. Use a parameterless fcn.
I have this nagging feeling that you're going to post something that is several times more efficient that this method...I just know it. In that case, there are *other* places where parameterless fcns make for good usage!
wtd




PostPosted: Wed Dec 01, 2004 1:36 pm   Post subject: (No subject)

Yes, I do know the value of functions, but use what's most expressive.

I wish I could offer something more efficient, but Turing doesn't provide for record literals.

One possible solution is simply to prove good defaults.

code:
record
   hello : int := 42
   world : string := "foo"
end record
Cervantes




PostPosted: Wed Dec 01, 2004 5:24 pm   Post subject: (No subject)

I don't think turing will let you assign values to hello and world, the elements within your record. I think you must assign your values outside the record.
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Wed Dec 01, 2004 6:19 pm   Post subject: (No subject)

Quite right Cervantes.

Once again, the limitations of OOT are exposed...
Paul




PostPosted: Wed Dec 01, 2004 6:20 pm   Post subject: (No subject)

Well, the point of the program is actually parameter passing. We were just suppose to do something with less than 2 global variables.
You can find it here actually
http://www34.brinkster.com/paulbian/projectile.doc
wtd




PostPosted: Wed Dec 01, 2004 6:27 pm   Post subject: (No subject)

Huh... could have sworn you could do that.
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: