Turing/Functions and Subroutines
Functions[edit | edit source]
I never use these, so I can't really say anything useful about them, except that they are used to make calculations that you often use. Let's say you want to make a program that gets radii and outputs the area of the circle.
function calculatearea (x : real) : real result x ** 2 * 3.14159 end calculatearea var input : real loop get input put "The area of a circle with a radius ", input, " is ", calculatearea (input) end loop
Procedures[edit | edit source]
These are great for things that you do often or just to make your program look more organized. Let's say that you want to get the keyboard input, then the mouse input, then the keyboard input and then output both, get the mouse input and then output both. Instead of copying and pasting bits of code, you can have procedures and call them when needed.
var mx,my,mb : int var keyinput : string procedure getmouse Mouse.Where (mx,my,mb) end getmouse procedure getkey get keyinput end getkey procedure outputinfo put mx, my, mb put keyinput end outputinfo loop keyinput getmouse outputinfo getmouse outputinfo end loop
Exercises:[edit | edit source]
- Make a function that calculates the number times 6, plus 3 divided by 2
- Make a procedure that puts 'hi' and then one that gets a string. Then call on them at random. (Hint: Use Rand.Int: Rand.Int (1,5) randomly outputs a number between 1 and 5.)
|Previous: Control Structures and Logical Expressions — Turing/Functions and Subroutines — Next: Basic Graphical User Interfaces|