Procedural Programming

Revision as of 13:53, 12 December 2015 by Jhernandez3 (talk | contribs) (Created page with "Category: Analytica User Guide <breadcrumbs> Analytica User Guide > {{PAGENAME}}</breadcrumbs><br /> This section shows you how to use the procedural features of the Anal...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


This section shows you how to use the procedural features of the Analytica modeling language, including:

  • Begin-End, (), and “;” for grouping expressions
  • Declaring local variables and assigning to them
  • For and While loops and recursion
  • Local indexes
  • References and data structures
  • Handles to objects
  • Dialog functions
  • Miscellaneous functions

A procedural program is list of instructions to a computer. Each instruction tells the computer what to do, or it might change the sequence to execute the instructions. Most Analytica models are non-procedural — that is, they consist of an unsequenced set of definitions of variables. Each definition is a simple expression that contain functions, operators, constants, and other variables, but no procedural constructs controlling the sequence of execution. In this way, Analytica is like a standard spreadsheet application, in which each cell contains a simple formula with no procedural constructs. Analytica selects the sequence in which to evaluate variables based on the dependencies among them, somewhat in the same way spreadsheets determine the sequence to evaluate their cells. Controlling the evaluation sequence via conditional statements and loops is a large part of programming in a language like in Fortran, Visual Basic, or C++. Non-procedural languages like Analytica free you from having to worry about sequencing. Non-procedural models or programs are usually much easier to write and understand than procedural programs because you can understand each definition (or formula) without worrying about the sequence of execution.

However, procedural languages enable you to write more powerful functions that are hard or impossible without their procedural constructs. For this reason, Analytica offers a set of programming constructs, described in this chapter, providing a general procedural programming language for those who need it.

You can use these constructs to control the flow of execution only within the definition of a variable or function. Evaluating one variable or function cannot (usually) change the value of another variables or functions. Thus, these procedural constructs do not affect the simple non-procedural relationship among variables and functions. The only exception is that a function called from an event handler such as OnChange or a button OnClick attribute can change the definition of a global variable. See “Creating buttons” on page 128.

An example of procedural programming

The following function, Factors(), computes the prime factors of an integer x. It illustrates many of the key constructs of procedural programming.

Example1.png

See below for an explanation of each of these constructs, and cross-reference to where they are.

Numbers identify features below

Function Factors(x) Definition:

  1. VAR result := [1];
  2. VAR n := 2;
  3. WHILE n <= x DO
  4. BEGIN
  5. VAR r := Floor(x/n);

IF r*n = x THEN

  1. (result := Concat(result, [n]);
  2. x := r)

ELSE n := n + 1 4, 7. END; /* End While loop */ 7, 8. result /* End Definition */

Comments


Lchrisman

29 months ago
Score 0
It says you can use these only within the definition of a variable or function, but in fact you can use procedural code in the OnClick of a button, which is a place where procedural style is often most likely to be useful.

You are not allowed to post comments.