|
|
Line 1: |
Line 1: |
| #REDIRECT [[Procedural Programming Example]] | | #REDIRECT [[Procedural Programming Example]] |
− | [[Category: Analytica User Guide]]
| |
− | <breadcrumbs> Analytica User Guide > {{PAGENAME}}</breadcrumbs><br />
| |
− |
| |
− | 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]]
| |
− |
| |
− | 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:
| |
− |
| |
− | # <code>VAR x := e</code> 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.
| |
− | # <code>While test Do body</code> construct tests condition <code>Test</code>, and, if True, evaluates <code>Body</code>, and repeats until condition <code>Test</code> is False. See “While(Test) Do Body” on page 381.
| |
− | # Begin <code>e1; e2; … End</code> 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.
| |
− | # <code>(e1; e2; …)</code> 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.
| |
− | # <code>x := e</code> lets you assign the value of an expression <code>e</code> to a local variable <code>x</code> 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 <code>Factors</code> returns the value of <code>result</code> — 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>
| |
Enable comment auto-refresher