Difference between revisions of "Procedural Programming Example"

m
m
 
(10 intermediate revisions by 3 users not shown)
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 illustrates many of Analytica's key constructs for procedural programming. It computes the prime factors of an integer «x»:
  
::[[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.
+
Here's more detail on the numbered procedural features in this function:
  
::{| border="0;" style="width:400px;"
+
# <code>VAR x := e</code> defines a local identifier <code>x</code> and sets its value to the result of evaluating expression <code>e</code>. See [[Local_Values#Defining_a_local_value|Defining a Local Value]].
! '''Numbers identify<br />
+
# Expressions (statements) in a sequence should be separated by “;” (semicolons). The expressions can be on the same line, successive lines, or may be broken over multiple lines. Newlines are just for clarity and have no effect on the syntax. See [[Begin-End for Grouping Expressions]].
features below'''
+
# WHILE <code>test DO body</code> tests condition <code>est</code>, and, if True, evaluates <code>Body</code>, and repeats until <code>Test</code> is False. See [[For_and_While_Loops#While.28Test.29_Do_Body|While(Test) Do Body]].
! '''Function Factors(x)<br />
+
# One way to group a sequence of expressions separated by semicolons “;”  is to enclose them as <code>BEGIN e1; e2; … END</code>— in this case as the body of a While loop. See  [[Begin-End for Grouping Expressions]].
Definition:'''
+
#  Another way to group a sequence of expressions is to enclose them between parentheses<code>(e1; e2; …)</code> — 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]].
| 1
+
#  The value returned from a sequence of expressions, in this case the Definition of the Function, is the value of the last expression. Here the function <code>Factors</code> returns the final expression, which is simple the local value <code>result</code>.
| <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:
+
==See Also==
 
+
* [[Local_Values#Defining_a_local_value|Defining a Local Value]]
# <code>VAR x := e</code> construct defines a local variable x, and sets an initial value e. See [http://wiki.analytica.com/index.php?title=Local_Variables#Defining_a_local_variable Defining a Local Variable].
+
* [[Local_Values#Assigning_to_a_local_value|Assigning to a local value]]
# 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 [http://wiki.analytica.com/index.php?title=Begin-End_for_Grouping_Expressions Begin-End for Grouping Expressions].
+
* [[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 [http://wiki.analytica.com/index.php?title=For_and_While_Loops#While.28Test.29_Do_Body While(Test) Do Body].
+
* [[For and While Loops]]
# 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.
+
* [[Function calls and parameters]]
#  <code>(e1; e2; …)</code> is another way to group expressions — in this case, as the action to be taken in the Then case. See [http://wiki.analytica.com/index.php?title=Begin-End_for_Grouping_Expressions 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 [http://wiki.analytica.com/index.php?title=Local_Variables#Assigning_to_a_local_variable: Assigning to a local variable].
 
#  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>Procedural Programming / {{PAGENAME}} / Summary of Programming Constructs</footer>
 
<footer>Procedural Programming / {{PAGENAME}} / Summary of Programming Constructs</footer>

Latest revision as of 01:21, 16 August 2017


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

Example1.png

Here's more detail on the numbered procedural features in this function:

  1. VAR x := e defines a local identifier x and sets its value to the result of evaluating expression e. See Defining a Local Value.
  2. Expressions (statements) in a sequence should be separated by “;” (semicolons). The expressions can be on the same line, successive lines, or may be broken over multiple lines. Newlines are just for clarity and have no effect on the syntax. See Begin-End for Grouping Expressions.
  3. WHILE test DO body tests condition est, and, if True, evaluates Body, and repeats until Test is False. See While(Test) Do Body.
  4. One way to group a sequence of expressions separated by semicolons “;” is to enclose them as BEGIN e1; e2; … END— in this case as the body of a While loop. See Begin-End for Grouping Expressions.
  5. Another way to group a sequence of expressions is to enclose them between parentheses(e1; e2; …) — 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 sequence of expressions, in this case the Definition of the Function, is the value of the last expression. Here the function Factors returns the final expression, which is simple the local value result.

See Also


Comments


You are not allowed to post comments.