Here is one use of global variables....a list of data that you wish to use inside the function, but the declaration is made in a command that is not part of the function.
months[mth], day, year+1900;
Here is an example of a function that uses the if-then, for do, and RETURN.
Find a solution to the equations in x^3 + 3*x*y + y^2 = 17 mod n....where n is any integer > 1. We will do this by checking all possible values for x and y between 0 and n-1 (notice that this method for solving this equation may take n^2 operations).
> findsolns:=proc(n) local x,y;
for x from 0 to n-1 do
for y from 0 to n-1 do
if evalb(((x^3+3*x*y+y^2-17) mod n) = 0) then
> (5^3+3*5*19+19^2) mod 29;
Take a list and repeat everything in it 3 times. We want to write a function that accepts a list and returns another list. We will write this function in 4 different ways...ALL four do exactly the same thing.
> rep3times1:=proc(lst) local i;
> rep3times2:=proc(lst) local r3t,i;
> rep3times3:=proc(lst) local r3t,i,out,j;
for i from 1 to nops(lst) do
Here is a couple of examples of the function rotntimes at work. Notice what is the output when the definition is given with a semi-colon at the end instead of a colon. Maple's representation of the function is shown. No picture is produced until we actually use the function.
> rotntimes:=proc(pic, n) local i;
> rotntimes(plots[display](plottools[arrow]([4,4], [10,10], .4, .8, .1, color=blue),axes=none),55);
Return to the FAQ on functions and procedures