Difference between revisions of "Procedural Programming Example"

(Local Variables --> Local Values (terminology change))
m (replaced screenshot and table by a screenshot with annotations)
Line 6: Line 6:
 
:[[File:example1.png|400px]]
 
:[[File:example1.png|400px]]
  
This table identifies key constructs from the function definition above, with a number
+
This function definition illustrates these features:
 
 
:{| class="wikitable"
 
! '''Numbers identify<br />features below'''<nowiki/>'''
 
! '''Function Factors(x)<br />Definition:'''<nowiki/>'''
 
|-
 
| 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><code>x := r)</code>
 
|-
 
| 6
 
|
 
:<code>ELSE n := n + 1</code>
 
|-
 
| 4, 7
 
|<code>END; </code>
 
|-
 
| 7, 8
 
|<code>result /* End Definition */</code>
 
|}
 
 
 
This definition illustrates these features:
 
  
 
# <code>VAR x := e</code> defines a local identifier <code>x</code> that refers to the value obtained by evaluating <code>e</code>. See [[Local_Values#Defining_a_local_value|Defining a Local Value]].
 
# <code>VAR x := e</code> defines a local identifier <code>x</code> that refers to the value obtained by evaluating <code>e</code>. See [[Local_Values#Defining_a_local_value|Defining a Local Value]].
Line 52: Line 14:
 
#  <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 for Grouping Expressions]].
 
#  <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 for Grouping Expressions]].
 
#  <code>x := e</code> assigns the value of expression <code>e</code> to a local value <code>x</code> or, as in the first case, to a parameter of a function. See [[Local_Values#Assigning_to_a_local_value|Assigning to a local value]].
 
#  <code>x := e</code> assigns the value of expression <code>e</code> to a local value <code>x</code> or, as in the first case, to a parameter of a function. See [[Local_Values#Assigning_to_a_local_value|Assigning to a local value]].
#  You can add a comment (text that Analytica ignores) between <code>{</code> and <code>}</code> or between <code>/*</code> and <code>*/</code>.
+
#  The value returned from a group of expressions is the value of the last expression — here the function <code>Factors</code> returns the local value <code>result</code> — whether the group is between <code>Begin</code> and <code>End</code>, parentheses <code>(</code> and <code>)</code>, or, as here, not enclosed by anything.
#  The value returned from a group of expressions is the value of the last expression — here the function <code>Factors</code> returns the local value <code>result</code> — whether the group is delimited by <code>Begin</code> and <code>End</code>, by parentheses, <code>(</code> and <code>)</code>, or, as here, by nothing.
 
  
 
==See Also==
 
==See Also==

Revision as of 22:30, 13 August 2017


This function, Factors(x), computes the prime factors of an integer «x». It illustrates many of Analytica's key constructs for procedural programming:

Example1.png

This function definition illustrates these features:

  1. VAR x := e defines a local identifier x that refers to the value obtained by evaluating e. See Defining a Local Value.
  2. You can group several expressions (statements) into a definition by separating them using “;” (semicolons). Expressions can be on the same line or successive lines. See Begin-End for Grouping Expressions.
  3. While test Do body tests condition Test, and, if True, evaluates Body, and repeats until condition Test is False. See While(Test) Do Body.
  4. BEGIN e1; e2; … ENDgroups a sequence of expressions separated by semicolons “;” — in this case as the body of a While loop. See Begin-End for Grouping Expressions.
  5. (e1; e2; …) is another way to group expressions — in this case, as the action to be taken in the Then case. See Begin-End for Grouping Expressions.
  6. x := e assigns the value of expression e to a local value x or, as in the first case, to a parameter of a function. See Assigning to a local value.
  7. The value returned from a group of expressions is the value of the last expression — here the function Factors returns the local value result — whether the group is between Begin and End, parentheses ( and ), or, as here, not enclosed by anything.

See Also


Comments


You are not allowed to post comments.