11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.3 Local Variables and Invoking Procedures • 172.3 Local Variables and Invoking ProceduresLocal variables are local to a procedure and to an invocation of thatprocedure. Calling a procedure creates and uses new local variables eachtime. If you invoke the same procedure twice, the local variables it usesthe second time are distinct from those it used the first time.Local variables do not necessarily disappear when the procedure exits.You can write procedures which return a local variable, either explicitly orimplicitly, to the interactive session, where it can exist indefinitely. Thesevariables are called escaped local variables. This concept can be confusing,particularly since they can have the same name as global variables, or localvariables which another procedure or a different call to the same procedurecreated. You can create many distinct variables with the same name.Example 1The following procedure creates a new local variable, a, and then returnsthis new variable.> make_a := proc()> local a;> a;> end proc;make_a := proc() local a; a end procBy using local variables, you can produce displays that Maple wouldotherwise simplify. For example, in Maple, a set contains unique elements.The following demonstrates that each variable a that make_a returns isunique.> test := { a, a, a };test := {a}> test := test union { make_a() };test := {a, a}> test := test union { ’make_a’()$5 };test := {a, a, a, a, a, a, a}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!