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.
+
The following function, <code>Factors()</code>, computes the prime factors of an integer «x». It illustrates many of the key constructs of procedural programming.
  
 
::[[File:example1.png|400px]]
 
::[[File:example1.png|400px]]
Line 46: Line 46:
 
This definition illustrates these features:
 
This definition illustrates these features:
  
# <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].
+
# <code>VAR x := e</code> construct defines a local variable x, and sets an initial value e. 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 [http://wiki.analytica.com/index.php?title=Begin-End_for_Grouping_Expressions 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 [http://wiki.analytica.com/index.php?title=For_and_While_Loops#While.28Test.29_Do_Body While(Test) Do Body].
+
# <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]].
 
# 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.
 
# 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 [http://wiki.analytica.com/index.php?title=Begin-End_for_Grouping_Expressions 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 [http://wiki.analytica.com/index.php?title=Local_Variables#Assigning_to_a_local_variable: Assigning to a local variable].
+
#  <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]].
 
#  A comment is enclosed between /* and */ as an alternative to { and }.
 
#  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 />
 
#  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 />
  
 +
==See Also==
  
 
<footer>Procedural Programming / {{PAGENAME}} / Summary of Programming Constructs</footer>
 
<footer>Procedural Programming / {{PAGENAME}} / Summary of Programming Constructs</footer>

Revision as of 03:15, 5 January 2016


The following function, Factors(), computes the prime factors of an integer «x». It illustrates many of the key constructs of procedural programming.

Example1.png

See below for an explanation of each of these constructs, and cross references to where they are.

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]);
6 x := r)

ELSE n := n + 1

4,7 END; /* End While loop */
7, 8 result /* End Definition */

This definition illustrates these features:

  1. VAR x := e construct defines a local variable x, and sets an initial value e. See Local_Variables#Defining_a_local_variable 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 construct tests condition Test, and, if True, evaluates Body, and repeats until condition Test is False. See While(Test) Do Body.
  4. 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.
  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 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 Local_Variables#Assigning_to_a_local_variable: Assigning to a local variable.
  7. A comment is enclosed between /* and */ as an alternative to { and }.
  8. 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.

See Also

Comments


You are not allowed to post comments.