Difference between revisions of "Procedural Programming Example"
m |
(Local Variables --> Local Values (terminology change)) |
||
Line 46: | Line 46: | ||
This definition illustrates these features: | This definition illustrates these features: | ||
− | # <code>VAR x := e</code> defines a local | + | # <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]]. |
# 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> 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]]. | ||
# <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>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> assigns the value of expression <code>e</code> to a local | + | # <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>. | # 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 value | + | # 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== | ||
− | * [[ | + | * [[Local_Values#Defining_a_local_value|Defining a Local Value]] |
− | * [[ | + | * [[Local_Values#Assigning_to_a_local_value|Assigning to a local value]] |
* [[Begin-End for Grouping Expressions]] | * [[Begin-End for Grouping Expressions]] | ||
* [[For and While Loops]] | * [[For and While Loops]] |
Revision as of 00:15, 5 January 2017
This function, Factors(x)
, computes the prime factors of an integer «x». It illustrates many of Analytica's key constructs for procedural programming:
This table identifies key constructs from the function definition above, with a number
Numbers identify
features belowFunction 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:
VAR x := e
defines a local identifierx
that refers to the value obtained by evaluatinge
. See Defining 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 Begin-End for Grouping Expressions.
While test Do body
tests conditionTest
, and, if True, evaluatesBody
, and repeats until conditionTest
is False. See While(Test) Do Body.BEGIN e1; e2; … END
groups a sequence of expressions separated by semicolons “;” — in this case as the body of a While loop. See Begin-End for Grouping Expressions.(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.x := e
assigns the value of expressione
to a local valuex
or, as in the first case, to a parameter of a function. See Assigning to a local value.- You can add a comment (text that Analytica ignores) between
{
and}
or between/*
and*/
. - The value returned from a group of expressions is the value of the last expression — here the function
Factors
returns the local valueresult
— whether the group is delimited byBegin
andEnd
, by parentheses,(
and)
, or, as here, by nothing.
See Also
- Defining a Local Value
- Assigning to a local value
- Begin-End for Grouping Expressions
- For and While Loops
- Function calls and parameters
Comments
Enable comment auto-refresher