diff options
author | Divay Prakash <divayprakash@users.noreply.github.com> | 2018-10-03 09:49:09 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-03 09:49:09 +0530 |
commit | 8c4c3968a21bcbe3e736bb5de1df7e2477813c08 (patch) | |
tree | 0de04e596df044fe6e4ce0eedc49dbdb42b8e7bc | |
parent | 67f785f10212ca827f25c83cfaa9dddc607a35c6 (diff) | |
parent | b2e43b1b76fef568d13531a04b88369de20fe6b3 (diff) |
Merge pull request #3246 from kaymmm/master
[pascal/en] add functions/procedures
-rw-r--r-- | pascal.html.markdown | 44 |
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. + +``` |