Difference between revisions of "Procedural Programming Example"

m
Line 2: Line 2:
 
<breadcrumbs> Analytica User Guide > Procedural Programming > {{PAGENAME}}</breadcrumbs><br />
 
<breadcrumbs> Analytica User Guide > Procedural Programming > {{PAGENAME}}</breadcrumbs><br />
  
The following function, <code>Factors()</code>, computes the prime factors of an integer «x». It illustrates many of the key constructs of procedural programming.
+
This function, <code>Factors(x)</code>, computes the prime factors of an integer «x». It illustrates many of Analytica's key constructs for procedural programming:
  
 
:[[File:example1.png|400px]]
 
:[[File:example1.png|400px]]
  
See below for an explanation of each of these constructs, and cross references to where they are.
+
This table identifies key constructs from the function definition above, with a number
  
 
:{| class="wikitable"
 
:{| class="wikitable"
! '''Numbers identify<br />
+
! '''Numbers identify<br />features below'''<nowiki/>'''
features below'''
+
! '''Function Factors(x)<br />Definition:'''<nowiki/>'''
! '''Function Factors(x)<br />
 
Definition:'''
 
 
|-
 
|-
 
| 1
 
| 1
Line 33: Line 31:
 
| 5
 
| 5
 
|
 
|
::<code>(result := Concat(result, [n]);</code>
+
::<code>(result := Concat(result, [n]);</code><code>x := r)</code>
 
|-
 
|-
 
| 6
 
| 6
 
|  
 
|  
::<code>x := r)</code>
 
 
:<code>ELSE n := n + 1</code>
 
:<code>ELSE n := n + 1</code>
 
|-
 
|-
 
| 4, 7
 
| 4, 7
|<code>END; /* End While loop */</code>
+
|<code>END; </code>
 
|-
 
|-
 
| 7, 8
 
| 7, 8
Line 49: Line 46:
 
This definition illustrates these features:
 
This definition illustrates these features:
  
# <code>VAR x := e</code> construct defines a local variable <code>x</code>, and sets an initial value e. See [[Local_Variables#Defining_a_local_variable|Defining a Local Variable]].
+
# <code>VAR x := e</code> defines a local variable <code>x</code>, and sets its initial value to <code>e</code>. See [[Local_Variables#Defining_a_local_variable|Defining a Local Variable]].
 
# 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]].
 
# 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]].
# <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 [[For_and_While_Loops#While.28Test.29_Do_Body|While(Test) Do Body]].
+
# <code>While test Do body</code> tests condition <code>Test</code>, and, if True, evaluates <code>Body</code>, and repeats until condition <code>Test</code> is False. See [[For_and_While_Loops#While.28Test.29_Do_Body|While(Test) Do Body]].
# 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 for Grouping Expressions]].
+
# <code>BEGIN e1; e2; … END</code>groups a sequence of expressions separated by semicolons “;” — in this case as the body of a While loop. 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>(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> 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 [[Local_Variables#Assigning_to_a_local_variable|Assigning to a local variable]].
+
#  <code>x := e</code> assigns the value of expression <code>e</code> to a local variable <code>x</code> or, as in the first case, to a parameter of a function. See [[Local_Variables#Assigning_to_a_local_variable|Assigning to a local variable]].
A comment is enclosed between <code>/*</code> and <code>*/</code> as an alternative to <code>{</code> and <code>}</code>.
+
You can add a comment (text that Analytica ignores) between <code>{</code> and <code>}</code> or between <code>/*</code> and <code>*/</code>.
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 <code>Begin</code> and <code>End</code>, by parentheses marks <code>(</code> and <code>)</code>, or, as here, by nothing.
+
The value returned from a group of expressions is the value of the last expression — here the function <code>Factors</code> returns the value of the local variable<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:05, 27 October 2016


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 table identifies key constructs from the function definition above, with a number

Numbers identify
features below
Function Factors(x)
Definition:
1 VAR result := [1];
2 VAR n := 2;
3 WHILE n <= x DO
4 BEGIN
2
VAR r := Floor(x/n);
IF r*n = x THEN
5
(result := Concat(result, [n]);x := r)
6
ELSE n := n + 1
4, 7 END;
7, 8 result /* End Definition */

This definition illustrates these features:

  1. VAR x := e defines a local variable x, and sets its initial value to e. See Defining a Local Variable.
  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 variable x or, as in the first case, to a parameter of a function. See Assigning to a local variable.
  7. You can add a comment (text that Analytica ignores) between { and } or between /* and */.
  8. The value returned from a group of expressions is the value of the last expression — here the function Factors returns the value of the local variableresult — whether the group is delimited by Begin and End, by parentheses, ( and ), or, as here, by nothing.

See Also


Comments


You are not allowed to post comments.