In this post I explore the use of PROCedures and FUNCtions in Action!
Procedures and Functions
Action! gives the ability to create both procedures and functions. similar to BASIC subroutines, or C functions, or any number of other languages that support structured subroutine mechanisms. A very simple program may not need either type. The difference between a procedure and function is that functions return a value. Each can accept up to five parameters.
Procedures in Action! are created using the construct:
PROC name([parameter,...]) RETURN
Functions in Action! are created using the construct:
type FUNC name([parameter,...]) RETURN (value)
In writing these I learned that every program must contain a procedure. Though I’m not sure why the simple print’s didn’t require it. The programs presented here are in proper Action! form.
A simple program that uses a procedure. This passes two parameters to a procedure called MyCalc which in turn multiples the two together then divides the result by 2, then finally displays the result (which should be 25):
MODULE PROC MyCalc(INT a,b) INT r= r=(a*b)/2 Print("Result=") PrintF("%I",r) RETURN PROC Main() MyCalc(5,10) RETURN
This is a function implementation of the same calculation as the procedure above, but rather than output the result to the screen, it returns the result for assignment to a third variable, which in turn is output to the screen.
MODULE INT FUNC MyCalc(INT a,b) INT r= r=(a*b)/2 RETURN(r) PROC Main() INT c= c=MyCalc(5,10) Print("Result=") PrintIE(c) RETURN
Next I’ll look at a number guessing game to contrast with the BASIC and Assembly versions I wrote about before.