Procedural Programming Example
This function, Factors(x)
, computes the prime factors of an integer «x». It illustrates many of Analytica's key constructs for procedural programming:
This function 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.- 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 betweenBegin
andEnd
, parentheses(
and)
, or, as here, not enclosed by anything.
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