Functions, Procedures, and Packages# Introduction Functions and Procedures Functions function function func f := func< x₁, ..., xₙ, ... : parameters | expression >; Example: Recursive Functions Example: Function Parameters Example: Returning Undefined Values Example: Variadic Functions Procedures procedure procedure proc p := proc< x₁, ..., xₙ, ... : parameters | expression >; Example: Procedures The forward Declaration forward Example: Mutual Recursion with Forward Declarations Packages Introduction Intrinsics intrinsic Example: Defining an Intrinsic for GCDs Resolving Calls to Intrinsics Example: Intrinsic Lookup for Overloaded Signatures Attaching and Detaching Package Files Attach(F): MonStgElt Detach(F): MonStgElt freeze; Related Files Importing Constants import "filename": ident_list; Example: Importing Constants from a Package Argument Checking require condition: print_args; requirerange v, L, U; requirege v, L; Example: Argument Checking with require The Number of Results NumberOfResults() → RngIntElt, [ BoolElt ] Nresults() → RngIntElt, [ BoolElt ] Example: Number of Results Requested Package Specification Files AttachSpec(S): MonStgElt DetachSpec(S): MonStgElt Example: Package Specification File User Startup Specification Files Example: User Startup Specification Files Attributes Predefined System Attributes User-defined Attributes AddAttribute(C, F): Cat, MonStgElt → ; declare attributes C: F₁,...,Fₙ; Accessing Attributes S`fieldname: Str, Fieldname → Elt S`` N: Str, Str → Elt assigned S`fieldname: Str, Fieldname → BoolElt assigned S`` N: Str, Str → BoolElt S`fieldname := expression; S`` N := expression; delete S`fieldname; delete S`` N; GetAttributes(C): Cat → [ MonStgElt ] ListAttributes(C): Cat User-defined Verbose Flags declare verbose F, m; Examples Example: System Attributes Example: Interactive User Attributes Example: Package User Attributes User-Defined Types Declaring User-Defined Types declare type T; declare type T: P₁, ..., Pₙ; declare type T[E]; declare type T[E]: P₁, ..., Pₙ; Creating an Object New(T): Type → T Clone(X): Any → Any Special Intrinsics Provided by the User Examples Example: User-Defined Type for Rationals Example: User-Defined Type for Direct Products