Return to the home page for Math
Maple FAQ (Frequently Asked Questions) about
functions and procedures
An example of a function:
# Accepts a picture and an integer n and repeats the picture n times on
the same display.
> rotntimes:=proc(pic, n) local i;
The parameters of this function are pic and n. There is one local
variable. The last command in this procedure is the value returned
(the thing that is returned from a 'display' command is another picture).
When you type this command in and hit ENTER, nothing seems to happen.
You have just entered the defintion of the function. From now on,
any time in Maple that you type rotntimes(pic,5); (where pic is a picture),
you will see the picture rotated 5 times. See here
for some examples of this procedure at work.
I hit enter after I typed in the function and nothing happened. What
When you hit enter, the function definition is now in Maple. You
have not told Maple to execute the commands in the defintion.
This is done with a call to the function.
What is a function in Maple?
A function is a collection of instructions. This set of instructions
is accessed each time a call to the function is made. Most of the
functions that you use are built into Maple, but there is a way to write
When should I write my own functions?
You should write your own functions whenever you are repeating lines of
Maple with cut and paste. It is probably more efficient to
write a function if you have to do the same operation over and over.
Whenever you are performing an operation to a list of objects, you should
almost definitely write a function. It is good programming form to
use functions even when it is not necessary (so...just about always).
What is a local variable?
A local variable is one that has values while the procedure is running,
but does not have a value before the procedure starts nor after it ends.
Local variables are declared on the first or second line of the definition
of the function.
What is a global variable?
A global variable is a variable that is set outside the function.
You might use a global variable for lookup values or to keep track of a
value that changes each time you call the function. In general you
should avoid the use of global variables because it is considered bad programming
style to use them. Here are some
examples of functions that use global variables.
Why do I sometimes get the messages like?:
Warning, `varname` in call to `seq` is not local
Warning, `varname` is implicitly declared local
Maple is informing you that you have failed to declare 'varname' as a local
or global variable and it is taking a guess that the variable is local
or global. Many times Maple is wrong, but even when it is correct,
you should not ignore these messages. You should go back and correct
your function definition so that the variables are all declared either
local or global (or a parameter).
When do I use the command 'RETURN' in my procedure?
A function in Maple usually returns the value of the last command executed
in the definition. If you wish to have the function stop halfway
through a calculation (say, if some condition is met), then you may use
the command 'RETURN.' 'RETURN' forces the function to stop executing
and return the value that is next to the command. Look here
for an example of this function at work.
What is with that really short notation with the -> arrow?
This is another way of declaring a function. If the definition of
your function can be made in one line (is one simple command) and does
not require the use of any local variables, then this notation is much
more convenient, shorter, but also terser. The short notation consists
of the parameters enclosed in parentheses and separated by commas, followed
by an arrow ->, followed by the command. Here
are some examples. This notation is especially useful when you are
using the map command to operate on a list of values. You do not
need an 'end' command at the end of one of these defintions, becuase it
is automatically assumed that the end of the function is the first semicolon
or colon that follows the arrow.
What does od and fi
Notice that od and fi
are do and if
spelled backwards. Every time that you have a for loop or a while
loop the format is given as
for blah from blah to blah do blah-blah-blah od;
while blah do blah-blah-blah od;
To tell Maple what commands you want to perform during the loop you list
the commands in the blah-blah-blah spot. The last command in the
loop is followed by the command od
and it tells Maple to group all of commands in blah-blah-blah together.
Basically, the command od means the
same as 'end the do loop.' In Maple 6 and higher you can use end
do in place of od.
Each time that you have an if-then-else statement the form of the command
if blah then blah-blah-blah fi;
if blah then blah else blah-blah-blah fi;
The fi at the end of the statement
says 'end the clause in the if statement' and tells Maple to group all
of the commands in the blah-blah-blah part of the if statement together.
In Maple 6 and higher you may use the command end
if in place of fi.
Check here for examples of functions
that use the for-do-od, while-do-od and if-then-else-fi statements.
Return to the home page for Math 2042