Difference between revisions of "Procedural Programming"

m
m
 
(62 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
[[Category: Analytica User Guide]]
 
[[Category: Analytica User Guide]]
<breadcrumbs> Analytica User Guide > {{PAGENAME}}</breadcrumbs><br />
+
<breadcrumbs> Analytica User Guide > {{PAGENAME}}</breadcrumbs>
  
This section shows you how to use the procedural features of the Analytica modeling language, including:
+
Analytica is ''mostly'' a '''declarative''' or '''non-procedural''' language. You create a model by creating variables, each with a definition expressing how to calculate it from other variables and functions (or just a number or distribution).  Because it has no (global) assignments, you can understand each definition (or formula) on its own without worrying about what happened before or after. There is no required sequence to write or read the sequence of variables. Analytica figures out what sequence to evaluate them at runtime to maintain consistency and efficiency without you having to tell it. Plus Intelligent Arrays lets you work with single or multidimensional arrays without having to write for loops to iterate over each dimension. All this frees you, as a model builder or reader of someone else's model, from having to worry about the flow of control. That gives Analytica a huge advantage in clarity and reliability over most conventional computer languages that  are '''procedural''', like in Fortran, Visual Basic, C++, Javascript, and so on. Programming in such languages means spending a lot of effort designing (and then debugging) the flow of control using assignments, conditionals, and loops. That's why we say Analytica enables you to be a modeler without being a programmer.
* 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<br />
 
  
 +
However there are occasional situations where it's more convenient to express an algorithm in procedural form. For this reason, Analytica offers a set of programming constructs, described in this chapter, giving you a general procedural programming language for those who want or need it. But most modelers can blissfully just ignore it!
  
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.
+
You can use these procedural constructs to control the flow of evaluation ''only within the definition'' of a variable or function. Evaluating one variable or function cannot (usually) change the value of another global variables or functions. In this way, they do not affect the simplicity and clarity of the declarative relationships among global variables and functions. (There are some minor exceptions, like assignments in the [[OnChange]] of a user input or [[OnClick]] attribute of a [[Button]], and the [[ComputedBy]] function, but even these are carefully designed to maintain the simple declarative forms.)
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.
+
== Sections ==
 +
* [[Procedural Programming Example]]
 +
* [[Summary of Programming Constructs]]
 +
* [[Begin-End for Grouping Expressions]]
 +
* [[Local Values]]
 +
* [[For and While Loops]]
 +
* [[Recursion]]
 +
* [[Local Indexes]]
 +
* [[Ensuring Array Abstraction]]
 +
* [[References and Data Structures]]
 +
* [[Handles to Objects]]
 +
* [[Dialog Functions]]
 +
* [[Other Procedural Functions]]
  
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.
+
== See Also ==
 +
* [[Analytica.NET]]
 +
* [[Conventions for parameters and operands]]
 +
* [[Operators]]
 +
* [[Analytica for programmers]]
  
==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.
 
  
:::[[File:example1.png|400px]]
+
<footer>User-defined Functions and Libraries / {{PAGENAME}} / Reference sections</footer>
 
 
See below for an explanation of each of these constructs, and cross references to where they are.
 
 
 
{| class="wikitable"
 
! '''Numbers identify<br />
 
features below'''
 
! '''Function Factors(x)<br />
 
Definition:'''
 
|-
 
| 1
 
| <code>VAR result := [1];</code>
 
|-
 
| 2
 
| <code>VAR n := 2;</code>
 
|-
 
| 3
 
|<code>WHILE n <= x DO</code>
 
|-
 
| 4
 
|<code>BEGIN</code>
 
|-
 
| 2
 
| <code>VAR r := Floor(x/n);</code>
 
<code>IF r*n = x THEN</code>
 
|-
 
| 5
 
|<code>(result := Concat(result, [n]);</code>
 
|-
 
| 6
 
| <code>x := r)</code>
 
<code>ELSE n := n + 1</code>
 
|-
 
| 4,7
 
|<code>END; /* End While loop */</code>
 
|-
 
| 7, 8
 
|<code>result /* End Definition */</code>
 
|}
 
 
 
This definition illustrates these features:
 
 
 
# VAR x := e construct defines a local variable x, and sets an initial value e. See “Defining a local variable: Var v := e” on page 376 for more.
 
# You can group several expressions (statements) into a definition by separating them by “;” (semicolons). Expressions can be on the same line or successive lines. See “Begin-End, (), and “;” for grouping expressions” on page 376.
 
# While test Do body construct tests condition Test, and, if True, evaluates Body, and repeats until condition Test is False. See “While(Test) Do Body” on page 381.
 
# Begin e1; e2; … End groups several expressions separated by semicolons “;” — in this case as the body of a While loop. See “Begin-End, (), and “;” for grouping expressions” on page 376.
 
#  (e1; e2; …) is another way to group expressions — in this case, as the action to be taken in the Then case. See “Begin-End, (), and “;” for grouping expressions” on page 376.
 
#  x := e lets you assign the value of an expression e to a local variable x or, as in the first case, to a parameter of a function. See “Assigning to a local variable: v := e” on page 377.
 
#  A comment is enclosed between /* and */ as an alternative to { and }.
 
#  A group of expressions returns the value of the last expression — here the function Factors returns the value of result — whether the group is delimited by Begin and End, by parentheses marks ( and ), or, as here, by nothing.<br />
 
 
 
 
 
<footer>Analytica Enterprise / {{PAGENAME}} / Function List</footer>
 

Latest revision as of 04:06, 17 January 2020

Analytica is mostly a declarative or non-procedural language. You create a model by creating variables, each with a definition expressing how to calculate it from other variables and functions (or just a number or distribution). Because it has no (global) assignments, you can understand each definition (or formula) on its own without worrying about what happened before or after. There is no required sequence to write or read the sequence of variables. Analytica figures out what sequence to evaluate them at runtime to maintain consistency and efficiency without you having to tell it. Plus Intelligent Arrays lets you work with single or multidimensional arrays without having to write for loops to iterate over each dimension. All this frees you, as a model builder or reader of someone else's model, from having to worry about the flow of control. That gives Analytica a huge advantage in clarity and reliability over most conventional computer languages that are procedural, like in Fortran, Visual Basic, C++, Javascript, and so on. Programming in such languages means spending a lot of effort designing (and then debugging) the flow of control using assignments, conditionals, and loops. That's why we say Analytica enables you to be a modeler without being a programmer.

However there are occasional situations where it's more convenient to express an algorithm in procedural form. For this reason, Analytica offers a set of programming constructs, described in this chapter, giving you a general procedural programming language for those who want or need it. But most modelers can blissfully just ignore it!

You can use these procedural constructs to control the flow of evaluation only within the definition of a variable or function. Evaluating one variable or function cannot (usually) change the value of another global variables or functions. In this way, they do not affect the simplicity and clarity of the declarative relationships among global variables and functions. (There are some minor exceptions, like assignments in the OnChange of a user input or OnClick attribute of a Button, and the ComputedBy function, but even these are carefully designed to maintain the simple declarative forms.)

Sections

See Also


Comments


Lchrisman

30 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.