summaryrefslogtreecommitdiffhomepage
path: root/pascal.html.markdown
diff options
context:
space:
mode:
authorKeith Miyake <keith.miyake@gmail.com>2018-10-02 13:55:35 -0700
committerKeith Miyake <keith.miyake@gmail.com>2018-10-02 13:55:35 -0700
commite533369df895fc5f2fd3a034b0fb53d84672f877 (patch)
tree4c656f34f82540122f26fd5d8e7cd2a754da85f9 /pascal.html.markdown
parentd34e296f041c1d974cd9a66337796461a846af5a (diff)
[pascal/en] add functions/procedures
Diffstat (limited to 'pascal.html.markdown')
-rw-r--r--pascal.html.markdown44
1 files changed, 43 insertions, 1 deletions
diff --git a/pascal.html.markdown b/pascal.html.markdown
index 6877afef..4144f900 100644
--- a/pascal.html.markdown
+++ b/pascal.html.markdown
@@ -3,6 +3,7 @@ language: Pascal
filename: learnpascal.pas
contributors:
- ["Ganesha Danu", "http://github.com/blinfoldking"]
+ - ["Keith Miyake", "https://github.com/kaymmm"]
---
@@ -92,5 +93,46 @@ End.
```
```pascal
+program Functional_Programming;
-``` \ No newline at end of file
+Var
+ i, dummy : integer;
+
+function factorial_recursion(const a: integer) : integer;
+{ recursively calculates the factorial of integer parameter a }
+
+// Declare local variables within the function
+// e.g.:
+// Var
+// local_a : integer;
+
+Begin
+ If a >= 1 Then
+ // return values from functions by assigning a value to the function name
+ factorial_recursion := a * factorial_recursion(a-1)
+ Else
+ factorial_recursion := 1;
+End; // terminate a function using a semicolon after the End statement.
+
+procedure get_integer(var i : integer; dummy : integer);
+{ get user input and store it in the integer parameter i.
+ parameters prefaced with 'var' are variable, meaning their value can change
+ outside of the parameter. Value parameters (without 'var') like 'dummy' are
+ static and changes made within the scope of the function/procedure do not
+ affect the variable passed as a parameter }
+
+Begin
+ write('Enter an integer: ');
+ readln(i);
+ dummy := 4; // dummy will not change value outside of the procedure
+End;
+
+Begin // main program block
+ dummy := 3;
+ get_integer(i, dummy);
+ writeln(i, '! = ', factorial_recursion(i));
+ // outputs i!
+ writeln('dummy = ', dummy); // always outputs '3' since dummy is unchanged.
+End.
+
+```